Break a while loop using an external event

I have a while loop in a windows service that runs for some x times and initiates x phone calls after reading them from some file. Now, if I want to make a user interface that makes it possible to stop phone calls, that is, break the while loop before it ends. How can I do it?

Suppose I have a function.

DialCalls(x) { for(int i= 0 ; i<x ; i++) { // Initiate call } } 

2,3 DialCalls functions can work in different threads, since I also executed threads in the application. Thus, in principle, the best way to break a loop from a web page is possible.

+8
c # while-loop workflow-foundation state-machines
source share
7 answers

Using the undo task (new in .net 4):

 [Fact] public void StartAndCancel() { var cancellationTokenSource = new CancellationTokenSource(); var token = cancellationTokenSource.Token; var tasks = Enumerable.Repeat(0, 2) .Select(i => Task.Run(() => Dial(token), token)) .ToArray(); // start dialing on two threads Thread.Sleep(200); // give the tasks time to start cancellationTokenSource.Cancel(); Assert.Throws<AggregateException>(() => Task.WaitAll(tasks)); Assert.True(tasks.All(t => t.Status == TaskStatus.Canceled)); } public void Dial(CancellationToken token) { while (true) { token.ThrowIfCancellationRequested(); Console.WriteLine("Called from thread {0}", Thread.CurrentThread.ManagedThreadId); Thread.Sleep(50); } } 

http://blogs.msdn.com/b/csharpfaq/archive/2010/07/19/parallel-programming-task-cancellation.aspx The task swallows exceptions, so there is some clearing up to, perhaps as IDisposable? About task exceptions

+6
source share

You can write an if-statement inside your loop to query the state of an external event.

For example, you might have a flag:

bool break_out = false;

And in your external event, you set the break_out flag to true:

break_out = true;

Now in your loop you can:

 DialCalls(x) { for(int i= 0 ; i<x ; i++) { // Initiate call if (break_out) { break; } } } 
+4
source share

Create an event that supports cancellation:

 public event EventHandler<CancelEventArgs> Call; 

If the subscriber exists and cancels it, do not call:

 while (true) { var e = new System.ComponentModel.CancelEventArgs(); if (Call != null) { Call(this, e); } if (!e.Cancel) { // Initiate call } else { break; } } 
+3
source share
 while (!_shouldStop) { /* work in progress */ } 

and in the Stop() method, which must be in the same working class, the bool variable must be set to true.

In addition, if there are any waiters (such as AutoResetEvent ), they must be .Set(); .

+1
source share

Using a variable telling when to break.

 private bool _cancel; void DialCalls(...) { for (int i=0; i<x; i++) { ... if (_cancel) break; .... } } private void SomeEventHandler(object sender, EventArgs e) { _cancel = true; } 

or

 for (int i=0; i<x && !_cancel; i++) 
0
source share

Have a global variable that is unstable

 public static volatile bool stop; stop=False; DialCalls(x) { for(int i= 0 ; i<x && !stop ; i++) { // Initiate call } } On Button Click => stop =True; 
0
source share

Introduce the endpoint of the service that receives the message, which switches the global / static logical (or some other) variable, and have a loop inside DialCalls to check the specified variable at the beginning of each iteration. It will not interrupt any phone calls that are currently being made, but I’m sure you don’t want the service to hang in the middle of a call, just don’t move on to the next one. This will interrupt for all threads; just make sure the variable itself is thread safe (e.g. use the volatile keyword).

0
source share

All Articles