Results 1 to 24 of 24

Thread: Question about threading.

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2014
    Posts
    7

    Question about threading.

    i am new to visual basic.
    does anyone know how to add a button that will query the state of the thread.
    check to see if the thread is running or not

  2. #2
    Frenzied Member
    Join Date
    May 2014
    Location
    Central Europe
    Posts
    1,372

    Re: Question about threading.

    how did you create the thread? a System.Threading.Thread object for example has got properties to query the thread state

  3. #3

    Thread Starter
    New Member
    Join Date
    Jul 2014
    Posts
    7

    Re: Question about threading.

    Quote Originally Posted by digitalShaman View Post
    how did you create the thread? a System.Threading.Thread object for example has got properties to query the thread state
    here is my code, i don't know how to check if the first thread is running or not.
    thank you

    Public Class Form1

    Dim i As Integer
    Dim i2 As Integer

    Dim thread As System.Threading.Thread

    Dim thread2 As System.Threading.Thread



    Private Sub countup()

    Do Until i = 10000
    i = i + 1
    Label1.Text = i
    Me.Refresh()

    Loop
    End Sub

    Private Sub countup2()

    Do Until i2 = 10000
    i2 = i2 + 1
    Label2.Text = i2
    Me.Refresh()

    Loop
    End Sub


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    thread = New System.Threading.Thread(AddressOf countup)
    thread.Start()

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

    thread2 = New System.Threading.Thread(AddressOf countup2)
    thread2.Start()

    End Sub



    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Me.CheckForIllegalCrossThreadCalls = False

    End Sub

  4. #4
    Frenzied Member
    Join Date
    May 2014
    Location
    Central Europe
    Posts
    1,372

    Re: Question about threading.

    Me.CheckForIllegalCrossThreadCalls = False

    ouch, this hurts. please remove this and use me.invoke to update the labels. your thread and thread2 Objects have a ThreadState property

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: Question about threading.

    CheckForIllegalCrossThreads isn't set to True just to make your life more difficult, it is there for a reason. Threading allows you to introduce two whole new classes of bugs that aren't (normally) there in single thread programming. The first one is the deadlock, which you can actually get in single thread programming, but it is highly unlikely. Deadlocks are relatively simple bugs, because your app will simply stop running, which is blatantly obvious. The other type of bug is the race condition, and this one is far nastier. Race conditions occur when the ultimate result of an action depends on which thread got there first. Because there will be some result depending on the winner of the race, these bugs are particularly nasty because they can cause different results each time you run something, or they might cause the program to work differently on different computers, differently between debug and production, or even allow them to work fine for years...then never work again. Because of this unpredictability, you really don't want to allow race conditions to get into your code.

    CheckForIllegalCrossThreads can be set to False in many cases. You are creating a race condition when you do so, because the UI and the other thread can both update the control almost simultaneously, and unpredictably. So, it may work fine for a long time and lull you into believing that the code is right...and then it will fail, or fail one time then not again for months, or pretty nearly anything else. So, CheckForIllegalCrossThreads = False makes life simpler in the short run, and FAR more difficult in the long run. Doing it right the first time is easier in the long run.
    My usual boring signature: Nothing

  6. #6

    Thread Starter
    New Member
    Join Date
    Jul 2014
    Posts
    7

    threading

    i want to create a button that will query the state of the thread.
    I don't know what code I need to use to show whether the thread is running or not
    Please help.

  7. #7
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: threading

    That's not exactly right. The correct way to go about this is for you to define when the thread is busy, usually by setting a field in whatever class the thread's method resides and you check the value of that field to determine whether your thread is busy or not. You can use a simple boolean variable. Here's an example of what I mean:-
    vbnet Code:
    1. Public Class Form1
    2.  
    3.     Private g_bThreadBusy As Boolean = False
    4.  
    5.     Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    6.  
    7.         Threading.ThreadPool.QueueUserWorkItem(AddressOf ThreadProc)
    8.  
    9.  
    10.     End Sub
    11.  
    12.     Private Sub ThreadProc()
    13.         g_bThreadBusy = True
    14.         For i = 1 To 100
    15.             Threading.Thread.Sleep(100)
    16.         Next
    17.         g_bThreadBusy = False
    18.     End Sub
    19.  
    20.     Private Sub btnQryThread_Click(sender As System.Object, e As System.EventArgs) Handles btnQryThread.Click
    21.         If g_bThreadBusy Then
    22.             MessageBox.Show("Thread is busy")
    23.         Else
    24.             MessageBox.Show("Thread is not running")
    25.         End If
    26.  
    27.     End Sub
    28. End Class
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  8. #8

    Thread Starter
    New Member
    Join Date
    Jul 2014
    Posts
    7

    Re: threading

    can you show me how the form look like?
    what is the g_bThreadBusy? is it a label?
    thank you

  9. #9

    Thread Starter
    New Member
    Join Date
    Jul 2014
    Posts
    7

    Re: threading

    I keep getting an error saying " Method 'Private Sub ThreadProc()'does not have the same signature as delegate 'Delegate Sub WaitCallback(state As Object)'.

  10. #10
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,398

    Re: threading

    How is it even close to being a label? The data type is declared before your eyes.

  11. #11
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: threading

    Quote Originally Posted by jok046 View Post
    what is the g_bThreadBusy? is it a label?
    Are you serious? I clearly defined it:-
    vbnet Code:
    1. Private g_bThreadBusy As Boolean = False

    Does that look like a Label ?
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  12. #12

    Thread Starter
    New Member
    Join Date
    Jul 2014
    Posts
    7

    Re: threading

    sorry i am new to vb.
    then why do I keep getting error? what am I missing on the form?

  13. #13

    Thread Starter
    New Member
    Join Date
    Jul 2014
    Posts
    7

    Re: threading

    sorry i am new to vb.
    then why do I keep getting error? what am I missing on the form?
    and what does ThreadProc represent?
    thank you

  14. #14
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,398

    Re: threading

    If you are new then you should not even be thinking about threading any thing.

  15. #15
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: threading

    Quote Originally Posted by jok046 View Post
    I keep getting an error saying " Method 'Private Sub ThreadProc()'does not have the same signature as delegate 'Delegate Sub WaitCallback(state As Object)'.
    Yea, I wrote that with Option Strict Off. Here's the fixed code including the button and I put a label so you can see the thread counting:-
    vbnet Code:
    1. Option Strict On
    2.  
    3. Public Class Form1
    4.  
    5.     Private g_bThreadBusy As Boolean = False
    6.  
    7.     Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    8.  
    9.         Threading.ThreadPool.QueueUserWorkItem(Sub() ThreadProc())
    10.  
    11.  
    12.     End Sub
    13.  
    14.     Private Sub ThreadProc()
    15.         g_bThreadBusy = True
    16.         For i = 1 To 100
    17.             Threading.Thread.Sleep(100)
    18.             Me.BeginInvoke(Sub()
    19.                                Label1.Text = i.ToString
    20.  
    21.                            End Sub)
    22.         Next
    23.         g_bThreadBusy = False
    24.     End Sub
    25.  
    26.     Private Sub btnQryThread_Click(sender As System.Object, e As System.EventArgs) Handles btnQryThread.Click
    27.         If g_bThreadBusy Then
    28.             MessageBox.Show("Thread is busy")
    29.         Else
    30.             MessageBox.Show("Thread is not running")
    31.         End If
    32.  
    33.     End Sub
    34.  
    35.  
    36.  
    37.     'NOTE: The following procedure is required by the Windows Form Designer
    38.     'It can be modified using the Windows Form Designer.  
    39.     'Do not modify it using the code editor.
    40.     <System.Diagnostics.DebuggerStepThrough()> _
    41.     Private Sub InitializeComponent()
    42.         Me.btnQryThread = New System.Windows.Forms.Button()
    43.         Me.Label1 = New System.Windows.Forms.Label()
    44.         Me.SuspendLayout()
    45.         '
    46.         'btnQryThread
    47.         '
    48.         Me.btnQryThread.Location = New System.Drawing.Point(15, 227)
    49.         Me.btnQryThread.Name = "btnQryThread"
    50.         Me.btnQryThread.Size = New System.Drawing.Size(137, 23)
    51.         Me.btnQryThread.TabIndex = 0
    52.         Me.btnQryThread.Text = "Query Thread"
    53.         Me.btnQryThread.UseVisualStyleBackColor = True
    54.         '
    55.         'Label1
    56.         '
    57.         Me.Label1.AutoSize = True
    58.         Me.Label1.Location = New System.Drawing.Point(12, 9)
    59.         Me.Label1.Name = "Label1"
    60.         Me.Label1.Size = New System.Drawing.Size(39, 13)
    61.         Me.Label1.TabIndex = 1
    62.         Me.Label1.Text = "Label1"
    63.         '
    64.         'Form1
    65.         '
    66.         Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
    67.         Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
    68.         Me.ClientSize = New System.Drawing.Size(284, 262)
    69.         Me.Controls.Add(Me.Label1)
    70.         Me.Controls.Add(Me.btnQryThread)
    71.         Me.Name = "Form1"
    72.         Me.Text = "Form1"
    73.         Me.ResumeLayout(False)
    74.         Me.PerformLayout()
    75.  
    76.     End Sub
    77.     Friend WithEvents btnQryThread As System.Windows.Forms.Button
    78.     Friend WithEvents Label1 As System.Windows.Forms.Label
    79.  
    80. End Class

    Just create a new WinForms app and overwrite the default Form code with the above code and run the program.
    Last edited by Niya; Jul 24th, 2014 at 05:52 PM.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  16. #16
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,398

    Re: Question about threading.

    Please do not create multiple threads. Especially as members are answering your questions'

  17. #17
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: Question about threading.

    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  18. #18
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,900

    Re: Question about threading.

    I merged the two threads. Try to avoid creating two threads for the same issue. If you have a second, distinct issue, on hte other hand, go ahead and create a new thread for it.

    Welcome to the forums and welcome to VB programming. As Ident hinted, you've picked a pretty tough problem to cut your teeth on so be prepared for a steep learning curve.

    To everyone else, don't forget we were all beginners once. A little encouragement goes a long way
    The best argument against democracy is a five minute conversation with the average voter - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

  19. #19
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: Question about threading.

    I don't think anyone's asked this yet: Why do you want to check the status of a thread? That suggests a wrong approach to threading if you want to ask that question.

  20. #20
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: Question about threading.

    Quote Originally Posted by Evil_Giraffe View Post
    I don't think anyone's asked this yet: Why do you want to check the status of a thread? That suggests a wrong approach to threading if you want to ask that question.
    Actually its perfectly valid. There are times when you need to know if an asynchronous operation is still running hence I don't believe he really needs to know the status of the thread in the strictest terms but first time "threaders" always think of that first, that they should check the actual thread and not the operation itself.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  21. #21
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: Question about threading.

    Errr, that's what I meant Niya. You're not thinking about it right if you're checking the status of the thread rather than the higher level operation. But even then, it's often that you really just want to get notified by the operation when certain things happen, rather than needing to know the current status.

    [Edit: and even then, knowing why the OP wants to know the status of the operation would determine the best approach to get that status.]

  22. #22
    Frenzied Member
    Join Date
    May 2014
    Location
    Central Europe
    Posts
    1,372

    Re: Question about threading.

    i have just posted this:
    http://www.vbforums.com/showthread.p...ive-question#7

    maybe you can help me understanding this need for sexy threading a bit better :P

  23. #23
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: Question about threading.

    I agree with both DS and EG, having read the other thread. Threading can be cool, but it isn't a panacea. It is entirely possible, even likely, to write a multi-threaded app that has worse performance than a single threaded app if you don't think it through carefully. If you want to play around with threading, that's fine, but I fear you are expecting more than it can deliver.

    I, too, would be interested in hearing what the ultimate goal was. I do think there is an ultimate goal, from what I've read, but I really doubt that threading is going to do what you are thinking it will do.
    My usual boring signature: Nothing

  24. #24
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: Question about threading.

    Quote Originally Posted by Shaggy Hiker View Post
    I do think there is an ultimate goal, from what I've read,
    The more I read, the more I suspect it's an even-more-inappropriate-than-usual course assignment.

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