|
-
Jan 6th, 2008, 02:53 PM
#1
Thread Starter
Hyperactive Member
[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 
-
Jan 6th, 2008, 02:58 PM
#2
Frenzied Member
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:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim workerThread As New System.Threading.Thread(AddressOf LongLoop)
workerThread.Start()
End Sub
Private Sub LongLoop()
While True
Console.WriteLine("Seperate Thread")
End While
End Sub
-
Jan 6th, 2008, 02:59 PM
#3
Frenzied Member
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.
-
Jan 6th, 2008, 03:35 PM
#4
Thread Starter
Hyperactive Member
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 
-
Jan 6th, 2008, 03:58 PM
#5
Re: [2005] very strange refresh problem
 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.
-
Jan 6th, 2008, 08:10 PM
#6
Thread Starter
Hyperactive Member
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 
-
Jan 6th, 2008, 08:15 PM
#7
Frenzied Member
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.
-
Jan 6th, 2008, 09:44 PM
#8
Thread Starter
Hyperactive Member
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 
-
Jan 6th, 2008, 10:04 PM
#9
Frenzied Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|