Jeanette Durham
asked on
How Do I Suspend A Thread From Inside Itself? vb.net 2005
Dear Experts:
I am making a loading screen that flashes while the rest of my application loads. So, I was thinking that I would use system.threading.thread to accomplish this, as I've successfully used this before to handle a file download meter when asyncrously downloading a file. Well, since I had a thread for the meter that part was running syncrously... So. This is the code I've got so far.
My issue is that I want the thread to put itself to sleep or pause for some length of time between blinks. Obviously I need to tell the thread to sleep from itself, not my program, because it must continue to execute code while the meter is frozen. If I use my.application.doevents inside the loop which is the sub which is the thread I'm afraid that the application will cease execution during that time and not my other thread.
'Global
Dim myThread As System.Threading.Thread
'Main Loading Sub
Private Sub Main_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
flagFormLoading = True
Me.Cursor = System.Windows.Forms.Curso rs.WaitCur sor
Me.fldDoing.Text = "Loading.."
'Handle Loading Button
Me.btnLoading.Visible = True
myThread = New System.Threading.Thread(Ad dressOf Flash_BtnLoading)
myThread.Start()
'...Other things load...
End Sub
'This is my Thread, it runs seperately from the rest of the program
Private Sub Flash_BtnLoading()
With Me.btnLoading
Do
If .Visible = False Then Exit Sub
.BackColor = IIf(.BackColor = Color.MintCream, Color.MediumSpringGreen, Color.MintCream)
Dim timeThen = DateAdd(DateInterval.Secon d, 1, Now())
Do
myThread.sleep(200) 'Doesn't Work! Gives me an error
Loop Until Now() > timeThen
Loop While .Visible = True
End With
End Sub
So the other thing I was thinking was to use: My.Application.DoEvents instead of myThread.Sleep(200) but I don't think this is the right approach either. What is the best way to pause a thread in the manner I'm trying to do?
THANKS Experts! ~Michael
I am making a loading screen that flashes while the rest of my application loads. So, I was thinking that I would use system.threading.thread to accomplish this, as I've successfully used this before to handle a file download meter when asyncrously downloading a file. Well, since I had a thread for the meter that part was running syncrously... So. This is the code I've got so far.
My issue is that I want the thread to put itself to sleep or pause for some length of time between blinks. Obviously I need to tell the thread to sleep from itself, not my program, because it must continue to execute code while the meter is frozen. If I use my.application.doevents inside the loop which is the sub which is the thread I'm afraid that the application will cease execution during that time and not my other thread.
'Global
Dim myThread As System.Threading.Thread
'Main Loading Sub
Private Sub Main_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
flagFormLoading = True
Me.Cursor = System.Windows.Forms.Curso
Me.fldDoing.Text = "Loading.."
'Handle Loading Button
Me.btnLoading.Visible = True
myThread = New System.Threading.Thread(Ad
myThread.Start()
'...Other things load...
End Sub
'This is my Thread, it runs seperately from the rest of the program
Private Sub Flash_BtnLoading()
With Me.btnLoading
Do
If .Visible = False Then Exit Sub
.BackColor = IIf(.BackColor = Color.MintCream, Color.MediumSpringGreen, Color.MintCream)
Dim timeThen = DateAdd(DateInterval.Secon
Do
myThread.sleep(200) 'Doesn't Work! Gives me an error
Loop Until Now() > timeThen
Loop While .Visible = True
End With
End Sub
So the other thing I was thinking was to use: My.Application.DoEvents instead of myThread.Sleep(200) but I don't think this is the right approach either. What is the best way to pause a thread in the manner I'm trying to do?
THANKS Experts! ~Michael
You can sleep a thread like this:
System.Threading.Thread.Sl eep(1000)
System.Threading.Thread.Sl
ASKER
VBRocks:
>> You can sleep a thread like this:
>> System.Threading.Thread.Sl eep(1000)
So If I do that inside my Flash_BtnLoading() sub (which is running on my other thread), will it suspend the new thread or will it suspend my program? Like how does Theading know which is what?
~Michael
>> You can sleep a thread like this:
>> System.Threading.Thread.Sl
So If I do that inside my Flash_BtnLoading() sub (which is running on my other thread), will it suspend the new thread or will it suspend my program? Like how does Theading know which is what?
~Michael
The VS Help says, It suspends the current thread for a specified time.
The current thread would be the thread that the sub is being executed on.
The current thread would be the thread that the sub is being executed on.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
When you run that example, you will find the following Output created:
Main Thread Finished Executing
Thread 2 executed.
Thread 1 executed.
So, in code, Thread 1 was first, then Thread 2, and finally the Main Thread. However, in the Output,
It's backwards.
Main Thread Finished Executing
Thread 2 executed.
Thread 1 executed.
So, in code, Thread 1 was first, then Thread 2, and finally the Main Thread. However, in the Output,
It's backwards.
ASKER
VBRocks:
I totally get what you're saying, and I believe that's my answer. Thanks for your help!
~Michael
I totally get what you're saying, and I believe that's my answer. Thanks for your help!
~Michael
You're welcome! Glad I could help.
ASKER
I didn't know you could send paramters to a thread function like that either. That's really cool..
ASKER
Cross-thread operation not valid: Control 'btnLoading' accessed from a thread other than the thread it was created on.
This was the code I was trying to do:
Private Sub Flash_BtnLoading()
With Me.btnLoading
Do
If .Visible = False Then Exit Sub
.BackColor = IIf(.BackColor = Color.MintCream, Color.MediumSpringGreen, Color.MintCream)
.Refresh()
So anyways, I guess my question is really how do I resolve these two problems so I can properly make a flashing loading screen while the rest of my program is running?
Thanks! ~Michael