-
Jul 24th, 2014, 10:41 AM
#1
Thread Starter
New Member
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
-
Jul 24th, 2014, 11:37 AM
#2
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
-
Jul 24th, 2014, 11:54 AM
#3
Thread Starter
New Member
Re: Question about threading.
Originally Posted by digitalShaman
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
-
Jul 24th, 2014, 12:14 PM
#4
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
-
Jul 24th, 2014, 12:47 PM
#5
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
-
Jul 24th, 2014, 04:43 PM
#6
Thread Starter
New Member
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.
-
Jul 24th, 2014, 04:56 PM
#7
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:
Public Class Form1
Private g_bThreadBusy As Boolean = False
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Threading.ThreadPool.QueueUserWorkItem(AddressOf ThreadProc)
End Sub
Private Sub ThreadProc()
g_bThreadBusy = True
For i = 1 To 100
Threading.Thread.Sleep(100)
Next
g_bThreadBusy = False
End Sub
Private Sub btnQryThread_Click(sender As System.Object, e As System.EventArgs) Handles btnQryThread.Click
If g_bThreadBusy Then
MessageBox.Show("Thread is busy")
Else
MessageBox.Show("Thread is not running")
End If
End Sub
End Class
-
Jul 24th, 2014, 05:28 PM
#8
Thread Starter
New Member
Re: threading
can you show me how the form look like?
what is the g_bThreadBusy? is it a label?
thank you
-
Jul 24th, 2014, 05:38 PM
#9
Thread Starter
New Member
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)'.
-
Jul 24th, 2014, 05:40 PM
#10
Re: threading
How is it even close to being a label? The data type is declared before your eyes.
-
Jul 24th, 2014, 05:40 PM
#11
Re: threading
Originally Posted by jok046
what is the g_bThreadBusy? is it a label?
Are you serious? I clearly defined it:-
vbnet Code:
Private g_bThreadBusy As Boolean = False
Does that look like a Label ?
-
Jul 24th, 2014, 05:45 PM
#12
Thread Starter
New Member
Re: threading
sorry i am new to vb.
then why do I keep getting error? what am I missing on the form?
-
Jul 24th, 2014, 05:46 PM
#13
Thread Starter
New Member
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
-
Jul 24th, 2014, 05:47 PM
#14
Re: threading
If you are new then you should not even be thinking about threading any thing.
-
Jul 24th, 2014, 05:47 PM
#15
Re: threading
Originally Posted by jok046
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:
Option Strict On
Public Class Form1
Private g_bThreadBusy As Boolean = False
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Threading.ThreadPool.QueueUserWorkItem(Sub() ThreadProc())
End Sub
Private Sub ThreadProc()
g_bThreadBusy = True
For i = 1 To 100
Threading.Thread.Sleep(100)
Me.BeginInvoke(Sub()
Label1.Text = i.ToString
End Sub)
Next
g_bThreadBusy = False
End Sub
Private Sub btnQryThread_Click(sender As System.Object, e As System.EventArgs) Handles btnQryThread.Click
If g_bThreadBusy Then
MessageBox.Show("Thread is busy")
Else
MessageBox.Show("Thread is not running")
End If
End Sub
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.btnQryThread = New System.Windows.Forms.Button()
Me.Label1 = New System.Windows.Forms.Label()
Me.SuspendLayout()
'
'btnQryThread
'
Me.btnQryThread.Location = New System.Drawing.Point(15, 227)
Me.btnQryThread.Name = "btnQryThread"
Me.btnQryThread.Size = New System.Drawing.Size(137, 23)
Me.btnQryThread.TabIndex = 0
Me.btnQryThread.Text = "Query Thread"
Me.btnQryThread.UseVisualStyleBackColor = True
'
'Label1
'
Me.Label1.AutoSize = True
Me.Label1.Location = New System.Drawing.Point(12, 9)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(39, 13)
Me.Label1.TabIndex = 1
Me.Label1.Text = "Label1"
'
'Form1
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(284, 262)
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.btnQryThread)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
Me.PerformLayout()
End Sub
Friend WithEvents btnQryThread As System.Windows.Forms.Button
Friend WithEvents Label1 As System.Windows.Forms.Label
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.
-
Jul 24th, 2014, 05:48 PM
#16
Re: Question about threading.
Please do not create multiple threads. Especially as members are answering your questions'
-
Jul 25th, 2014, 01:26 AM
#17
Re: Question about threading.
-
Jul 25th, 2014, 04:22 AM
#18
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
-
Jul 25th, 2014, 06:09 AM
#19
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.
-
Jul 25th, 2014, 06:51 AM
#20
Re: Question about threading.
Originally Posted by Evil_Giraffe
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.
-
Jul 25th, 2014, 07:51 AM
#21
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.]
-
Jul 25th, 2014, 02:39 PM
#22
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
-
Jul 25th, 2014, 07:11 PM
#23
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
-
Jul 26th, 2014, 03:39 AM
#24
Re: Question about threading.
Originally Posted by Shaggy Hiker
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|