Threading Problem

Please support our VB.NET advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Dec 2008
Posts: 18
Reputation: TriceD is an unknown quantity at this point 
Solved Threads: 0
TriceD TriceD is offline Offline
Newbie Poster

Threading Problem

 
0
  #1
Feb 24th, 2009
Greetings all,

I have some difficulties concerning threading and hope you guys could be of help.

Let's say I have the following class:

  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:

  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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 2,065
Reputation: Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice 
Solved Threads: 256
Featured Poster
Ramy Mahrous's Avatar
Ramy Mahrous Ramy Mahrous is offline Offline
Postaholic

Re: Threading Problem

 
0
  #2
Feb 24th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 18
Reputation: TriceD is an unknown quantity at this point 
Solved Threads: 0
TriceD TriceD is offline Offline
Newbie Poster

Re: Threading Problem

 
0
  #3
Feb 24th, 2009
Thanks for your reply RamyMahrous, but how do I stop the main thread?
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 18
Reputation: TriceD is an unknown quantity at this point 
Solved Threads: 0
TriceD TriceD is offline Offline
Newbie Poster

Re: Threading Problem

 
0
  #4
Feb 24th, 2009
I was thinking that the following section:
  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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 2,065
Reputation: Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice 
Solved Threads: 256
Featured Poster
Ramy Mahrous's Avatar
Ramy Mahrous Ramy Mahrous is offline Offline
Postaholic

Re: Threading Problem

 
0
  #5
Feb 24th, 2009
My opinion is to remove the initiation of the new thread in the class
  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
  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
BI Developer | LINKdotNET
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 18
Reputation: TriceD is an unknown quantity at this point 
Solved Threads: 0
TriceD TriceD is offline Offline
Newbie Poster

Re: Threading Problem

 
0
  #6
Feb 25th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 2,065
Reputation: Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice 
Solved Threads: 256
Featured Poster
Ramy Mahrous's Avatar
Ramy Mahrous Ramy Mahrous is offline Offline
Postaholic

Re: Threading Problem

 
0
  #7
Feb 25th, 2009
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
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC