| | |
Threading Problem
Please support our VB.NET advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Dec 2008
Posts: 18
Reputation:
Solved Threads: 0
Greetings all,
I have some difficulties concerning threading and hope you guys could be of help.
Let's say I have the following class:
Let's say I use the above class in the following way:
I've included line numbers so I can just refer the lines by their numbers.
My problem is, when the program executes, line 4 seems to be executed earlier than I want it to. It gets executed while ThreadX is still running in line 3. How can I force the program to wait until ThreadX has finished executing, before it executes line 4?
I have some difficulties concerning threading and hope you guys could be of help.
Let's say I have the following class:
VB.NET Syntax (Toggle Plain Text)
Public Class myClass Public Sub closeSomething() End Sub Public Sub doSomethingElse() End Sub Public Function doSomething() As String Dim ret As String = "Blablabla" Dim ThreadX As New System.Threading.Thread(AddressOf doSomethingElse) ThreadX.IsBackground = True ThreadX.Start() While ThreadX.ThreadState <> System.Threading.ThreadState.Aborted AndAlso ThreadX.ThreadState <> System.Threading.ThreadState.Stopped System.Threading.Thread.Sleep(1) End While Return ret End Function End Class
Let's say I use the above class in the following way:
VB.NET Syntax (Toggle Plain Text)
1- Dim str As String 2- Dim x As New myClass 3- str = x.doSomething() 4- x.closeSomething()
I've included line numbers so I can just refer the lines by their numbers.
My problem is, when the program executes, line 4 seems to be executed earlier than I want it to. It gets executed while ThreadX is still running in line 3. How can I force the program to wait until ThreadX has finished executing, before it executes line 4?
Last edited by TriceD; Feb 24th, 2009 at 6:31 am.
That's normal because you don't stop the main thread, your process or application in line 3 has 2 threads working in parallel..
BI Developer | LINKdotNET
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
•
•
Join Date: Dec 2008
Posts: 18
Reputation:
Solved Threads: 0
I was thinking that the following section:
would ensure that the function doSomething() doesn't return until ThreadX finishes running, and that line 4 therefore would not be executed before. That's obviously not the case, but what am I doing wrong? And more importantly, how do I do it right?
VB.NET Syntax (Toggle Plain Text)
While ThreadX.ThreadState <> System.Threading.ThreadState.Aborted AndAlso ThreadX.ThreadState <> System.Threading.ThreadState.Stopped System.Threading.Thread.Sleep(1) End While
would ensure that the function doSomething() doesn't return until ThreadX finishes running, and that line 4 therefore would not be executed before. That's obviously not the case, but what am I doing wrong? And more importantly, how do I do it right?
Last edited by TriceD; Feb 24th, 2009 at 9:15 am.
My opinion is to remove the initiation of the new thread in the class
rather creating this new thread in the class you call DoSomething
Or you can read more about threading, talk to thread from another, etc.. it may help http://msdn.microsoft.com/en-us/libr...d_members.aspx
vb.net Syntax (Toggle Plain Text)
Dim ThreadX As New System.Threading.Thread(AddressOf doSomethingElse) ThreadX.IsBackground = True ThreadX.Start() While ThreadX.ThreadState <> System.Threading.ThreadState.Aborted AndAlso ThreadX.ThreadState <> System.Threading.ThreadState.Stopped
vb.net Syntax (Toggle Plain Text)
1- Dim str As String 2- Dim x As New myClass 3- Dim thrd As new Thread(new ThreadStart(x.DoSomthing) ' Try to modify DoSomthing to be void to work in this wasy '4- x.closeSomething()
BI Developer | LINKdotNET
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
Let me see to learn from you, and if it solved please mark it as solved to be used as reference for people facing this problem. Thanks
BI Developer | LINKdotNET
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
![]() |
Similar Threads
- how would you apply threading to a web server application (C++)
- problem on threading (C#)
- Multi threading problem. (C++)
- Threading, Delegates and the Invoke method. (VB.NET)
- Problem With 915GAV Motherboard (Motherboards, CPUs and RAM)
- A problem with memory (Motherboards, CPUs and RAM)
- Problem when handling with processes! (C)
- Help regarding multi-threading (C)
- Thermaltake Heatsink Installation Problem!? (Cases, Fans and Power Supplies)
Other Threads in the VB.NET Forum
- Previous Thread: datagridviewcomboboxcolumn problem
- Next Thread: looping and changing cells value in excel using VB.NET
| Thread Tools | Search this Thread |
.net .net2008 2008 access advanced application array basic beginner browser button buttons center checkbox click client code combo convert cuesent data database datagrid datagridview date datetimepicker designer dissertation dissertations dissertationtopic eclipse excel exists fade filter forms function generatetags gridview html images input internet listview map mobile module monitor msaccess net number objects open panel pdf picturebox picturebox2 port position print printing read regex remove right-to-left save search searchvb.net serial settings shutdown socket sorting sqldatbase sqlserver survey temperature textbox timer timespan transparency txttoxmlconverter user usercontol validation vb vb.net vba vbnet visual visualbasic visualbasic.net visualstudio.net web winforms winsock wpf wrapingcode xml year






