| | |
C# Threads...
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: May 2009
Posts: 27
Reputation:
Solved Threads: 0
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:
I tried doing runCode.Suspend(); but it doesn't work, and it says its deprecated... Please help, Thanks!!!!
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)
private void compileRunToolStripMenuItem_Click(object sender, EventArgs e) { Thread runCode = new Thread(new ThreadStart(RunTheCode)); runCode.Start(); }
I tried doing runCode.Suspend(); but it doesn't work, and it says its deprecated... Please help, Thanks!!!!
•
•
Join Date: Aug 2008
Posts: 83
Reputation:
Solved Threads: 18
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:
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.
C# Syntax (Toggle Plain Text)
public void RunTheCode() { while(true) { //some code while(pause) ; } }
Good point @nmaillet. Use boolean (volatile) instance variable to control a thread.
c# Syntax (Toggle Plain Text)
public class Best { volatile bool flag=true ; Thread thread; public Best() { thread=new Thread(new ThreadStart(Run)); thread.Start(); } public void Stop() { flag = false; } public void Run() { while (flag) { Console.WriteLine("{0}", DateTime.Now.ToShortTimeString()); } } }
c# Syntax (Toggle Plain Text)
Best a = new Best(); Thread.Sleep(1000); a.Stop(); // Stop this thread after approximate 1000 millsec.
Failure is not fatal, but failure to change might be. - John Wooden
•
•
Join Date: May 2009
Posts: 27
Reputation:
Solved Threads: 0
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#
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#
•
•
Join Date: Jul 2009
Posts: 903
Reputation:
Solved Threads: 144
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)
bool suspend = true;// set to false to continue processing void SuspendRun () { suspend = true; while (suspend) thread.Sleep(1000); } void ContinueRun() { suspend = false; // will cause loop above to break and continue processing }
![]() |
Similar Threads
- I want google to index forum threads (Search Engine Optimization)
- new way the threads are timed (DaniWeb Community Feedback)
- IE won't let me access threads. Could someone email me a solution please? (Web Browsers)
Other Threads in the C# Forum
- Previous Thread: C# if else problem
- Next Thread: Insert Error:Syntax error in INSERT INTO statement
| Thread Tools | Search this Thread |
.net access algorithm array asp.net barchart bitmap box broadcast c# check checkbox client combobox control conversion csharp custom database databaseconnection datagrid datagridview dataset datetime dbconnection degrees design development draganddrop drawing encryption enum event eventhandlers excel file firefox form format forms function gdi+ grantorrevokepermissionthroughc#.net httpwebrequest image index input install java label libraries list listbox loop mandelbrot marshalbyrefobject math mouseclick movingimage mysql mysql.data.client operator path photoshop picturebox pixelinversion platform post programming radians regex remote remoting resourcefile richtextbox server sleep socket sql statistics stream string system.servicemodel table tcpclientchannel text textbox thread time timer update usercontrol validation visualstudio webbrowser windows winforms wpf wpfc# xml







