943,844 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 743
  • C# RSS
Aug 3rd, 2009
0

C# Threads...

Expand Post »
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:

C# Syntax (Toggle Plain Text)
  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!!!!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
kashn is offline Offline
27 posts
since May 2009
Aug 3rd, 2009
0

Re: C# Threads...

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:
C# Syntax (Toggle Plain Text)
  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.
Reputation Points: 69
Solved Threads: 48
Posting Whiz in Training
nmaillet is offline Offline
203 posts
since Aug 2008
Aug 3rd, 2009
0

Re: C# Threads...

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!
Reputation Points: 10
Solved Threads: 0
Light Poster
kashn is offline Offline
27 posts
since May 2009
Aug 4th, 2009
1

Re: C# Threads...

Good point @nmaillet. Use boolean (volatile) instance variable to control a thread.
c# Syntax (Toggle Plain Text)
  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. }
c# Syntax (Toggle Plain Text)
  1. Best a = new Best();
  2. Thread.Sleep(1000);
  3. a.Stop(); // Stop this thread after approximate 1000 millsec.
Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,527 posts
since Oct 2008
Aug 4th, 2009
0

Re: C# Threads...

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#
Reputation Points: 10
Solved Threads: 0
Light Poster
kashn is offline Offline
27 posts
since May 2009
Aug 4th, 2009
0

Re: C# Threads...

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:
C# Syntax (Toggle Plain Text)
  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. }
Reputation Points: 341
Solved Threads: 233
Posting Shark
DdoubleD is offline Offline
984 posts
since Jul 2009
Aug 4th, 2009
0

Re: C# Threads...

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++
Reputation Points: 10
Solved Threads: 0
Newbie Poster
castegna is offline Offline
3 posts
since Jun 2007

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 C# Forum Timeline: C# if else problem
Next Thread in C# Forum Timeline: Insert Error:Syntax error in INSERT INTO statement





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


Follow us on Twitter


© 2011 DaniWeb® LLC