Results 1 to 9 of 9

Thread: [2005] very strange refresh problem

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2005
    Posts
    257

    [2005] very strange refresh problem

    I've got three loops in my program and inside the loops I do some database interaction and some other local stuff like formatting the data. One loop about 100 times, the next 175, and the next 1550 times. At the very beginning of each loops I've got something to the effect of:


    Code:
    lblStatus.Text = "Importing Run " & (d + 1).ToString & " of " & dsXML.Tables("run").Rows.Count.ToString
    Me.Refresh()
    so every time through the loop it says like importing run 1 of 1550 then the next time it's 2 and yeah, it works great...until I change focus to another program. While it's running if I go to any other program at all and come back, the label is stuck and no longer refreshes until the whole thing is done. So I guess that's not a very good way to code it or I need an extra command or something. Anyone know how to make that work?
    I tried to end process on Visual Studio 2005
    but PETA stopped me saying it's smart enough
    to be a living creature

  2. #2
    Frenzied Member
    Join Date
    Mar 2006
    Location
    Pennsylvania
    Posts
    1,069

    Re: [2005] very strange refresh problem

    Ahh, a classic example on why threading is so vital. It's quite simple. Create a method that does all this looping, and then create a thread that processes that method. Because it's on a separate thread than the rest of your application, and more precisely, your controls, it will allow the user to continue interaction with the program and view it. Here's some example code to get you started:

    vb.net Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.     Dim workerThread As New System.Threading.Thread(AddressOf LongLoop)
    3.  
    4.     workerThread.Start()
    5. End Sub
    6.  
    7. Private Sub LongLoop()
    8.     While True
    9.         Console.WriteLine("Seperate Thread")
    10.     End While
    11. End Sub

  3. #3
    Frenzied Member
    Join Date
    Mar 2006
    Location
    Pennsylvania
    Posts
    1,069

    Re: [2005] very strange refresh problem

    Also, you can try looking into a BackgroundWorker. It creates a thread on it's own and basically encapsulates the messiness of threads and threading.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2005
    Posts
    257

    Re: [2005] very strange refresh problem

    well how about that, I was about to put most of my code into subs cuz I wrote it all in a hurry inside the button click sub lol. Most of my used stuff is module level anyway so I can probably just copy and paste and it will work. I'll look into the background worker too cuz I've heard of that. I dunno if it can get any simpler than "this sub = this thread, start it" though. I actually had to do that once to play a sound cuz apparently Vb is so dumb it stops all code execution until the sound is done playing.
    I tried to end process on Visual Studio 2005
    but PETA stopped me saying it's smart enough
    to be a living creature

  5. #5
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2005] very strange refresh problem

    Quote Originally Posted by Desolator144
    ... I actually had to do that once to play a sound cuz apparently Vb is so dumb it stops all code execution until the sound is done playing.
    Why wouldn't it stop all code execution until the sound finishes?
    Perhaps someone has missed reading the documentation? If you're using My.Computer.Audio.Play then you should know that there is a AudioPlayMode argument that you can set to Background.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2005
    Posts
    257

    Re: [2005] very strange refresh problem

    I was using some kind of strange paragraph of code that didn't make an ounce of sense to me to play it. I think it was a call to the media player library. Anyway...
    Why wouldn't it stop all code execution until the sound finishes
    and the survey says....because that's retarded who would ever their program to completely freeze up and ignore all user input and not refresh the form and stop processing all commands while a sound plays? I can't even think of one application that you'd want that to happen in.
    I tried to end process on Visual Studio 2005
    but PETA stopped me saying it's smart enough
    to be a living creature

  7. #7
    Frenzied Member
    Join Date
    Mar 2006
    Location
    Pennsylvania
    Posts
    1,069

    Re: [2005] very strange refresh problem

    I think he meant that because playing a sound must interrupt the current execution of the program. Specifying the Background parameter creates a new thread and plays the sound, I believe, so the execution of the application is not messed up. It makes perfect sense, as Atheist said.

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2005
    Posts
    257

    Re: [2005] very strange refresh problem

    yeah except I wasn't using that method to play it so there was no option for that. In fact that's a 2.0 command and 2.0 didn't exist when I was writing that program.
    If you're curious, I used:
    Code:
     Private Declare Auto Function PlaySound Lib "winmm.dll" _
          (ByVal lpszSoundName As String, ByVal hModule As Integer, _
           ByVal dwFlags As Integer) As Integer
        Private Const SND_FILENAME As Integer = &H20000
    and then
    Code:
    PlaySound("shock.wav", 0, SND_FILENAME)
    I tried to end process on Visual Studio 2005
    but PETA stopped me saying it's smart enough
    to be a living creature

  9. #9
    Frenzied Member
    Join Date
    Mar 2006
    Location
    Pennsylvania
    Posts
    1,069

    Re: [2005] very strange refresh problem

    It can still be done with that. Check out the other flags on the MSDN page:

    http://msdn2.microsoft.com/en-us/library/ms712879.aspx

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width