Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Thread

Status
Not open for further replies.

hariram55

Programmer
Jul 28, 2006
10
US
hello,
I am new with vb.net, i tried to code a simple program to understand threading. I have enclosed code here by, when i run the program it stucks after some time. I guess its a problem with timer.
if somebody know please help me sort the problem..thanks

Public try1 As Thread
Public try2 As Thread
Public array1(1000) As Short
Public i, j As Integer


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Timer1.Enabled = True
End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

For i = 0 To 1000
array1(i) = CInt(Int((6 * Rnd()) + 1))
Next
calc()
End Sub

Sub calc()

try1 = New Thread(AddressOf compu)
try2 = New Thread(AddressOf compu1)

try1.Start()
try2.Start()

try2.Join()

End Sub

Sub compu()
ListView1.Items.Clear()
For i = 0 To 1000
ListView1.Items.Add(array1(i))
Next i
End Sub

Sub compu1()
ListView2.Items.Clear()
For i = 0 To 1000
j = array1(i) + j
Next i
ListView2.Items.Add(j)
End Sub

 
What do you mean by "stucks"? If you mean the GUI is locking up, remove the try2.join.

Also, avoid updating the GUI from a thread other than the primary thread. For the ListViews that you are adding items to I would recommend setting up a private method for each add on the form class. Also on the form class add a public method that your threads will call. In that method use Me.InvokeRequired to determine if the incoming call is from a thread other than the form's primary thread. If it is, use Me.BeginInvoke to call the Add methods that actual add the item to the ListView. If it isn't, just call the Add method normally.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
hello..ThatRickGuy !! Thanks a lot I really appreciate.

What I want to do is get continuous random data from the timer function [ to array1() ] and then one process will display data of array1 continuously in listview1 and another process will add content of array1 and display the sum in listview2. The two process (thread) should wait and start again after data1() is updated.

As the program is a continuous loop, wait for array1 to update new data, when it is updated, thread (try1) and thread (try2) should start, process and wait until the array1 is not updated again.

As I mentioned earlier, I am quite new to this language, the suggestion you wrote is quite complicated for me to understand, if you will write a simple code then it will be much easier for me to understand or you can make changes in my posted code.

Hope to hear from you. Thank you again for your reply..
 
Weird, I posted a large reply to this a few days ago...

What I would recommend is using a delegate for the first method you call. set up a "call back" method for that delegate, so that when it finishes it calls the second method in it's own thread.

Also, remember that you should not update and GUI control from any thread other than the GUI control's primary thread. What that means is that you should have a method on the form that acts as an intermediary. When called it should check Me.InvokeRequired and determine whether the call is coming in on its primary thread, or another thread. If it's on another thread use Me.BeginInvoke to call another method that actual updates the GUI control. If it's not on another thread, just call that method directly.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
In order to safely update a UI from another thread, you need to write code that looks something like this.
[ol][li]Create a delegate type for your method[/li]
[li]In a method that implements that delegate, first thing you do is check the InvokeRequired property of your form.[/li]
[li]If it returns true, that means you're not on the correct thread. To get on the correct thread, you call the BeginInvoke method on the Form, passing the same paramters as you were called with.[/li]
[li]Return from within the InvokeRequired test.[/li]
[li]The .NET runtime will call you back (you'll execute this method a second time), but on the correct thread.[/li]
[li]This time, the InvokeRequired test will fail, and you skip over the BeginInvoke, and can proceed to update your label or textbox.[/li][/ol]
Not doing it this way will result in the UI behaving fine about 99% of the time. The other 1% of the time it'll do strange things.

Chip H.

____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
That's a neat idea on running the code all in one method, for some reason I had always done it as two seperate methods (like a public AddListItem(item) method, and a private _AddListItem(item) method) Same outcome, but yours does seem a bit cleaner.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top