问题
Let me describe what I am trying to achieve. I am programming in C#.
I have a function (method) DoCalculations(). I want to call this DoCalculations() method recursively. However, in C# I get exception as unhandled exception System.StackOverflow. So, I am trying to run DoCalculations() method on a back ground thread.
On FORM LOAD event. I have done following:-
Thread objThread = new Thread(new ThreadStart(DoCalculations));
On START BUTTON CLICK event. I am starting the Thread as follows.
objThread.IsBackground = true;
objThread.Start();
while (!objThread.IsAlive)
{
}
And I intend to run method DoCalculations() continuously with above While Loop.
DoCalculations()
{
//some calculations.
}
I do get control in DoCalculations() method one time. However, I want to do this every instant.
Please if any one can assist me with regards to back ground thread, or is there any better way to achieve to do parallel computation.
I have used above approach in VB.NET, making me more confused why its not working in C#.NET
Any assistance, comments greatly appreciated.
Thanks AP
回答1:
First, use a background worker, as it is easier to use and faster.
Second, your looping code should go in the background thread.
Here is some code to get you started.
BackgroundWorker bw = new BackgroundWorker();
bw.DoWork += bw_DoWork;
bw.RunWorkerAsync();
...
private void bw_DoWork(object sender, DoWorkEventArgs e)
{
while (someCondition)
{
DoCalculations();
}
}
EDIT: It sounds like you are just trying to fix a stack overflow, and not knowing how to do so. Forget the multithreading, instead of calling DoCalculations recursively, call it in a loop, and make sure that the loop knows when to terminate. Also, if you don't want to lock up the GUI during this time, then a background thread is better anyway.
回答2:
System.Threading.Timer Provides a mechanism for executing a method at specified intervals.
How to Use in your case....
using System.Threading;
Declare timer callback and timer object as follows..
private TimerCallback calculateTimerDelegate = null;
private System.Threading.Timer calculationTimer = null;
And in your Load() event initialize them..
calculateTimerDelegate = new TimerCallback(DoCalculation);
calculationTimer = new Timer(calculateTimerDelegate , null, 10000, (1 * 60 * 1000));
Now, your method of calculation....
DoCalculations()
{
//some calculations.
}
回答3:
If you want to run it repeadetly on a background thread, you can use the System.Threading.Timer
class like this:
var timer = new Timer( DoCalculations, null, TimeSpan.FromSeconds(0), TimeSpan.FromSeconds(1));
This will run your caclulations every second. If you want to adjust the time, change the last TimeSpan
parameter.
回答4:
Use BackgroundWorker Class
http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx
回答5:
If you're getting a stack overflow exception, the way to make it go away will not be discovered by introducing multithreading. There's a bug in your recursive code.
来源:https://stackoverflow.com/questions/11154747/c-sharp-background-thread