943,956 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Marked Solved
  • Views: 1570
  • VB.NET RSS
Feb 24th, 2009
0

Threading Problem

Expand Post »
Greetings all,

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)
  1. Public Class myClass
  2.  
  3. Public Sub closeSomething()
  4.  
  5. End Sub
  6.  
  7. Public Sub doSomethingElse()
  8.  
  9. End Sub
  10.  
  11. Public Function doSomething() As String
  12. Dim ret As String = "Blablabla"
  13. Dim ThreadX As New System.Threading.Thread(AddressOf doSomethingElse)
  14.  
  15. ThreadX.IsBackground = True
  16. ThreadX.Start()
  17.  
  18. While ThreadX.ThreadState <> System.Threading.ThreadState.Aborted AndAlso ThreadX.ThreadState <> System.Threading.ThreadState.Stopped
  19. System.Threading.Thread.Sleep(1)
  20. End While
  21.  
  22. Return ret
  23. End Function
  24.  
  25. End Class


Let's say I use the above class in the following way:

VB.NET Syntax (Toggle Plain Text)
  1. 1- Dim str As String
  2. 2- Dim x As New myClass
  3. 3- str = x.doSomething()
  4. 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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
TriceD is offline Offline
21 posts
since Dec 2008
Feb 24th, 2009
0

Re: Threading Problem

That's normal because you don't stop the main thread, your process or application in line 3 has 2 threads working in parallel..
Featured Poster
Reputation Points: 480
Solved Threads: 276
Postaholic
Ramy Mahrous is offline Offline
2,189 posts
since Aug 2006
Feb 24th, 2009
0

Re: Threading Problem

Thanks for your reply RamyMahrous, but how do I stop the main thread?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
TriceD is offline Offline
21 posts
since Dec 2008
Feb 24th, 2009
0

Re: Threading Problem

I was thinking that the following section:
VB.NET Syntax (Toggle Plain Text)
  1. While ThreadX.ThreadState <> System.Threading.ThreadState.Aborted AndAlso ThreadX.ThreadState <> System.Threading.ThreadState.Stopped
  2. System.Threading.Thread.Sleep(1)
  3. 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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
TriceD is offline Offline
21 posts
since Dec 2008
Feb 24th, 2009
0

Re: Threading Problem

My opinion is to remove the initiation of the new thread in the class
vb.net Syntax (Toggle Plain Text)
  1. Dim ThreadX As New System.Threading.Thread(AddressOf doSomethingElse)
  2.  
  3. ThreadX.IsBackground = True
  4. ThreadX.Start()
  5.  
  6. While ThreadX.ThreadState <> System.Threading.ThreadState.Aborted AndAlso ThreadX.ThreadState <> System.Threading.ThreadState.Stopped
rather creating this new thread in the class you call DoSomething
vb.net Syntax (Toggle Plain Text)
  1. 1- Dim str As String
  2. 2- Dim x As New myClass
  3. 3- Dim thrd As new Thread(new ThreadStart(x.DoSomthing)
  4. ' Try to modify DoSomthing to be void to work in this wasy
  5. '4- x.closeSomething()
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
Featured Poster
Reputation Points: 480
Solved Threads: 276
Postaholic
Ramy Mahrous is offline Offline
2,189 posts
since Aug 2006
Feb 25th, 2009
0

Re: Threading Problem

Thanks for your suggestion, RamyMahrous... I was able to circumvent the problem somehow by having the instantiation of myClass in its own thread. No idea why that worked.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
TriceD is offline Offline
21 posts
since Dec 2008
Feb 25th, 2009
0

Re: Threading Problem

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
Featured Poster
Reputation Points: 480
Solved Threads: 276
Postaholic
Ramy Mahrous is offline Offline
2,189 posts
since Aug 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in VB.NET Forum Timeline: datagridviewcomboboxcolumn problem
Next Thread in VB.NET Forum Timeline: looping and changing cells value in excel using VB.NET





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC