0

I am explaining my scenario, i have a function which print 1 to 10000 while printing i have to stop the process and let user know the current i value and if user presses enter it should continue again.

i am using

if ((Console.KeyAvailable) && (Console.ReadKey(true).Key == ConsoleKey.Escape))

but it doesn,t work, to complicate my task i am using threads, even if i manage to break this thread, child thread starts executing, i just want to stop the entire execution process for a moment and execute another function.

forum.test17
  • 2,119
  • 6
  • 30
  • 62

2 Answers2

0

Check out the BackgroundWorker class, specifically, how to implement cancellation.

You'll basically need to check inside the loop if a cancellation is pending. If it is, then exit the loop.

Merlyn Morgan-Graham
  • 58,163
  • 16
  • 128
  • 183
  • Another couple tutorials on `BackgroundWorker`: http://msdn.microsoft.com/en-us/library/cc221403(v=vs.95).aspx and http://www.codeproject.com/KB/cpp/BackgroundWorker_Threads.aspx – Merlyn Morgan-Graham Oct 03 '11 at 10:21
0

If your using Threads. You can use this kind of code..

// Start
thread = new Thread(new ThreadStart(YourCommand));
thread.Start();

// Pause
if (thread != null)
{
thread.Suspend();
}

//Continue
if (thread != null)
{
thread.Resume();
}

//Alive
if (thread != null)
{
    if (thread.IsAlive)
    {
     thread.Abort();
    }    
}

Or you can use timer....

Sam Casil
  • 938
  • 1
  • 11
  • 16
  • Can you change this code to support cooperative cancellation? You should avoid thread abort, see the links on this answer for why: http://stackoverflow.com/questions/421389/is-this-thread-abort-normal-and-safe/421445#421445 – Merlyn Morgan-Graham Oct 03 '11 at 18:50