C# Threads...

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: May 2009
Posts: 27
Reputation: kashn is an unknown quantity at this point 
Solved Threads: 0
kashn kashn is offline Offline
Light Poster

C# Threads...

 
0
  #1
Aug 3rd, 2009
Hi,

I'm compiling C# code to memory and running it from there, now what I want to do is pause, resume, stop that same code running in the memory.

I'm using this currently to run the code:

  1. private void compileRunToolStripMenuItem_Click(object sender, EventArgs e)
  2. {
  3. Thread runCode = new Thread(new ThreadStart(RunTheCode));
  4. runCode.Start();
  5. }

I tried doing runCode.Suspend(); but it doesn't work, and it says its deprecated... Please help, Thanks!!!!
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 83
Reputation: nmaillet is an unknown quantity at this point 
Solved Threads: 18
nmaillet nmaillet is offline Offline
Junior Poster in Training

Re: C# Threads...

 
0
  #2
Aug 3rd, 2009
The Suspend method has been deprecated because it is unsafe, since you are never certain where you stopped the threads execution. Instead you could declared a boolean variable, and then check it at certain points during the execution of the thread. For example:
  1. public void RunTheCode()
  2. {
  3. while(true)
  4. {
  5. //some code
  6. while(pause) ;
  7. }
  8. }
Then you need to set the pause variable when you want to pause the current thread, and it will pause after the current iteration of the while loop has finished.
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 27
Reputation: kashn is an unknown quantity at this point 
Solved Threads: 0
kashn kashn is offline Offline
Light Poster

Re: C# Threads...

 
0
  #3
Aug 3rd, 2009
The thing is that, the code is translated into assemble and executed from the memory...

Is it possible to like interrupt the thread and do like Thread.Sleep(1) in a loop...? If so, how?

Thanks!
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,612
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 463
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven

Re: C# Threads...

 
1
  #4
Aug 4th, 2009
Good point @nmaillet. Use boolean (volatile) instance variable to control a thread.
  1. public class Best
  2. {
  3. volatile bool flag=true ;
  4. Thread thread;
  5. public Best()
  6. {
  7. thread=new Thread(new ThreadStart(Run));
  8. thread.Start();
  9. }
  10. public void Stop()
  11. {
  12. flag = false;
  13. }
  14. public void Run()
  15. {
  16.  
  17. while (flag)
  18. {
  19. Console.WriteLine("{0}", DateTime.Now.ToShortTimeString());
  20. }
  21. }
  22. }
  1. Best a = new Best();
  2. Thread.Sleep(1000);
  3. a.Stop(); // Stop this thread after approximate 1000 millsec.
Failure is not fatal, but failure to change might be. - John Wooden
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 27
Reputation: kashn is an unknown quantity at this point 
Solved Threads: 0
kashn kashn is offline Offline
Light Poster

Re: C# Threads...

 
0
  #5
Aug 4th, 2009
I do not think you guys understand :/

I'm using the Compile Engine, from: http://www.codeproject.com/KB/dotnet/DotNetScript.aspx

Now a script is executed from from my app, something like this basically: http://69.10.233.10/KB/dotnet/DynamicCompileAndRun.aspx

And while that script is running, I want to pause it in its tracks... Is it possible? I know its possible in Delphi, so don't see what it should'nt be in C#
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 903
Reputation: DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough 
Solved Threads: 144
DdoubleD DdoubleD is offline Offline
Posting Shark

Re: C# Threads...

 
0
  #6
Aug 4th, 2009
It's hard to determine exactly what/how you are trying to achieve this. If you have a handle to the thread, you could have it Sleep() in a loop until you are ready for it to resume processing:
  1. bool suspend = true;// set to false to continue processing
  2. void SuspendRun ()
  3. {
  4. suspend = true;
  5. while (suspend)
  6. thread.Sleep(1000);
  7. }
  8. void ContinueRun()
  9. {
  10. suspend = false; // will cause loop above to break and continue processing
  11. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 3
Reputation: castegna is an unknown quantity at this point 
Solved Threads: 0
castegna castegna is offline Offline
Newbie Poster

Re: C# Threads...

 
0
  #7
Aug 4th, 2009
I will suggest declare a variable that controls your thread, when this variable has certain value you can send the thread to sleep for x seconds, and then wake it up again. What I mean with this is use like semaphores in C++
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C# Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC