• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 268
  • Last Modified:

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.Cursors.WaitCursor
        Me.fldDoing.Text = "Loading.."

        'Handle Loading Button
        Me.btnLoading.Visible = True
        myThread = New System.Threading.Thread(AddressOf 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.Second, 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
0
JeffreyDurham
Asked:
JeffreyDurham
  • 5
  • 4
1 Solution
 
JeffreyDurhamAuthor Commented:
I must be doing this totally wrong, because even tho my other thread is running it's not visible doing anything when it loads (until the form) is finished. So I tried adding the .Refresh line to it and it produced the following error:
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
0
 
VBRocksCommented:
You can sleep a thread like this:

    System.Threading.Thread.Sleep(1000)

0
 
JeffreyDurhamAuthor Commented:
VBRocks:

>> You can sleep a thread like this:
>>    System.Threading.Thread.Sleep(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

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
VBRocksCommented:
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.

0
 
VBRocksCommented:
Here's an example.  Just call the ThreadTest() sub and check the Output window.

    Private Sub ThreadTest()

        Dim t As Threading.Thread

        t = New Threading.Thread( _
            New Threading.ParameterizedThreadStart(AddressOf DisplayMessage))

        t.Start(New Object() {1, True})


        t = New Threading.Thread( _
             New Threading.ParameterizedThreadStart(AddressOf DisplayMessage))

        t.Start(New Object() {2, False})

        Debug.WriteLine("Main Thread Finished Executing")

    End Sub

    Private Sub DisplayMessage(ByVal o As Object)

        Dim threadID As Int16 = o(0)
        Dim suspend As Boolean = o(1)

        If suspend = True Then
            'sleep 5 seconds
            System.Threading.Thread.Sleep(5000)

        End If

        Debug.WriteLine("Thread " & threadID & " executed.")


    End Sub

0
 
VBRocksCommented:
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.




0
 
JeffreyDurhamAuthor Commented:
VBRocks:

I totally get what you're saying, and I believe that's my answer. Thanks for your help!
~Michael
0
 
VBRocksCommented:
You're welcome!  Glad I could help.

0
 
JeffreyDurhamAuthor Commented:
I didn't know you could send paramters to a thread function like that either. That's really cool..
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now