I'm new to multithreading in c# and am confused among all thread stuff. here is what I'm trying to have:
public class TheClass
{
Thread _thread;
bool _isQuitting;
bool _isFinished;
object jobData;
public void Start()
{
jobData = new object();
_thread = new Thread(new ThreadStart(Run));
_thread.Start();
}
private void Run()
{
while (!_isQuitting)
{
lock (jobData) // or should i use mutex or monitor?
{
DoStuff();
}
// sleep till get triggered
}
DoCleanupStuff();
_isFinished = true;
}
public void AddJob(object param)
{
jobData = param;
//wake up the thread
}
public void Stop()
{
_isQuitting = true;
//wake up & kill the thread
//wait until _isFinished is true
//dispose the _thread object
}
}
in this class, what is the most efficient method to achieve commented out phrases and for overall performance?
I don't know about the performance, but it looks like you want AutoResetEvent.
This certainly gives you one of the simplest ways to do what you're describing. Test it out, and if you find performance is an issue, then worry about another way to do it.