M
muscha
Hi All,
I'm having a bit of problem with threading, when I first call Start() on my
code the thread work, I call Stop() and it stopped, but when I call Start()
again my thread is not started. Although in the debugger I can see that it
went to the Start() method and creating the thread again.
Can anyone help me? This is the code:
<snip>
public static MySingleton GetInstance() {
if (instance == null) {
instance = new MySingleton();
}
return instance;
}
public void Start() {
lock (this) {
if (!started){
started = true;
thread = new Thread(new ThreadStart(Run));
thread.Start();
}
}
}
public void Stop() {
stopped = true;
}
private void Run() {
while (!stopped) {
Console.WriteLine("Running ...");
Thread.Sleep(1000);
}
thread.Join();
started = false;
}
</snip>
Thanks,
/m
I'm having a bit of problem with threading, when I first call Start() on my
code the thread work, I call Stop() and it stopped, but when I call Start()
again my thread is not started. Although in the debugger I can see that it
went to the Start() method and creating the thread again.
Can anyone help me? This is the code:
<snip>
public static MySingleton GetInstance() {
if (instance == null) {
instance = new MySingleton();
}
return instance;
}
public void Start() {
lock (this) {
if (!started){
started = true;
thread = new Thread(new ThreadStart(Run));
thread.Start();
}
}
}
public void Stop() {
stopped = true;
}
private void Run() {
while (!stopped) {
Console.WriteLine("Running ...");
Thread.Sleep(1000);
}
thread.Join();
started = false;
}
</snip>
Thanks,
/m