C# - Break Loop from another button - c#

In my program i'm starting for loop using button, I want to break this for loop using another button.
For example:
private void button1_Click(object sender, EventArgs e)
{
for( int i = 0; i < var; i++)
{
//doing something
}
}
And using second button break loop,
private void button2_Click(object sender, EventArgs e)
{
//breaking loop;
}
Need help :)

Set a flag in button2_Click() method and check it in the button1_Click()'s loop.
In order to process Windows events and allow button2_Click() handle to run while iterating, add Application.DoEvents() in your loop:
bool breakLoop = false;
private void button1_Click(object sender, EventArgs e)
{
breakLoop = false;
for( int i = 0; i < var && !breakLoop; i++)
{
//doing something
Application.DoEvents();
}
}
private void button2_Click(object sender, EventArgs e)
{
breakLoop = true;
}

You cannot do that, because the loop in button1_Click event handler will be holding the UI thread. Your user interface will not respond to any event, showing hourglass icon, until the loop is over. This means that button2_Click cannot be entered until button1_Click has completed.
You need to replace the long-running loop from the event handler with something that runs outside the UI thread. For example, you can use Tasks, which can be cancelled using CancellationToken (related Q&A).

Arguably it would be better to use threads and cancellation tokens in some form, rather than the Application.DoEvents(). Something like this:
private CancellationTokenSource loopCanceller = new CancellationTokenSource();
private void button1_Click(object sender, EventArgs e)
{
Task.Factory.StartNew(() =>
{
try
{
for (int i = 0; i < 100; i++)
{
this.loopCanceller.Token.ThrowIfCancellationRequested(); // exit, if cancelled
// simulating half a second of work
Thread.Sleep(500);
// UI update, Invoke needed because we are in another thread
Invoke((Action)(() => this.Text = "Iteration " + i));
}
}
catch (OperationCanceledException ex)
{
loopCanceller = new CancellationTokenSource(); // resetting the canceller
Invoke((Action)(() => this.Text = "Thread cancelled"));
}
}, loopCanceller.Token);
}
private void button2_Click(object sender, EventArgs e)
{
loopCanceller.Cancel();
}

Related

C# BackgroundWorker.CancelAsync() vs DoWorkEventArgs.Cancel

I want to use the RunWorkerCompletedEventArgs.Cancelled value in my BackgroundWorker completed handler, but the documentation isn't clear how how BackgroundWorker.CancelAsync() and DoWorkEventArgs.Cancel (in the background worker do work handler) will each effect it. Are they functionally the same?
For example, is this...
private void _Worker_DoWork(object sender, DoWorkEventArgs e)
{
(sender as BackgroundWorker).CancelAsync();
}
...equivalent to this?
private void _Worker_DoWork(object sender, DoWorkEventArgs e)
{
e.Cancel = true;
}
Will the latter result in subsequent evaluations of CancellationPending() to evaluate as true? Also, if the background worker is cancelled externally (i.e. myBW.CancelAsync(); outside the do work handler), will e.Cancel = false cause an evaluation of CancellationPending() to be false?
BackgroundWorker.CancelAsync() will set the value of BackgroundWorker.CancellationPending to true, so the DoEvent code can check it.
DoWorkEventArgs.Cancel is there to tell RunWorkerCompleted Event that the process was Canceled. You are not supposed to use the result of a operation that was aborted or ended in Exception. Setting DoWorkEventArgs.Cancel to true will set RunWorkerCompletedEventArgs.Canceled to true. Wich will also force RunWorkerCompletedEventArgs.Result to throw an exception if accessed.
I have some pretty old example code from when I learned Multithrading with BGW. It should help you.
#region Primenumbers
private void btnPrimStart_Click(object sender, EventArgs e)
{
if (!bgwPrim.IsBusy)
{
//Prepare ProgressBar and Textbox
int temp = (int)nudPrim.Value;
pgbPrim.Maximum = temp;
tbPrim.Text = "";
//Start processing
bgwPrim.RunWorkerAsync(temp);
}
}
private void btnPrimCancel_Click(object sender, EventArgs e)
{
if (bgwPrim.IsBusy)
{
bgwPrim.CancelAsync();
}
}
private void bgwPrim_DoWork(object sender, DoWorkEventArgs e)
{
int highestToCheck = (int)e.Argument;
//Get a reference to the BackgroundWorker running this code
//for Progress Updates and Cancelation checking
BackgroundWorker thisWorker = (BackgroundWorker)sender;
//Create the list that stores the results and is returned by DoWork
List<int> Primes = new List<int>();
//Check all uneven numbers between 1 and whatever the user choose as upper limit
for(int PrimeCandidate=1; PrimeCandidate < highestToCheck; PrimeCandidate+=2)
{
//Report progress
thisWorker.ReportProgress(PrimeCandidate);
bool isNoPrime = false;
//Check if the Cancelation was requested during the last loop
if (thisWorker.CancellationPending)
{
//Tell the Backgroundworker you are canceling and exit the for-loop
e.Cancel = true;
break;
}
//Determin if this is a Prime Number
for (int j = 3; j < PrimeCandidate && !isNoPrime; j += 2)
{
if (PrimeCandidate % j == 0)
isNoPrime = true;
}
if (!isNoPrime)
Primes.Add(PrimeCandidate);
}
//Tell the progress bar you are finished
thisWorker.ReportProgress(highestToCheck);
//Save Return Value
e.Result = Primes.ToArray();
}
private void bgwPrim_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
pgbPrim.Value = e.ProgressPercentage;
}
private void bgwPrim_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
pgbPrim.Value = pgbPrim.Maximum;
this.Refresh();
if (!e.Cancelled && e.Error == null)
{
//Show the Result
int[] Primes = (int[])e.Result;
StringBuilder sbOutput = new StringBuilder();
foreach (int Prim in Primes)
{
sbOutput.Append(Prim.ToString() + Environment.NewLine);
}
tbPrim.Text = sbOutput.ToString();
}
else
{
tbPrim.Text = "Operation canceled by user or Exception";
}
}
#endregion
Personally I consider the BackgroundWorker class in a GUI to be good Multitasking "Training Wheels".
No, they are not the same. The "CancelAsync()" method runs from outside of the "backgroundworker"'s code. The "CancellationPending" can be check in the "DoWork" body and "e.Cancel" is set in "DoWork" to be used in the "Completed" method.
Please see the page below for more information:
(https://www.c-sharpcorner.com/uploadfile/mahesh/backgroundworker-in-C-Sharp/)

setText in textbox with infinite loop

I am writing activity recognition software in realtime. But i can't setText in textbox with infinite loop. I try to search Google but no answer. When, i using "textbox.Text += "ZZZZ", it working but I using "textbox.Text = "ZZZ" ", it not working. I hope someone can point me how to solve
private void button1_Click(object sender, EventArgs e)
{
for (; ; ){
Thread.Sleep(20);
........process....
tb_activity = "AAA";
}
}
You can use the new await and async feature in .Net:
private void button1_Click(object sender, EventArgs e)
{
EndlessTask();
}
async Task EndlessTask()
{
for(int i = 0; true; i++)
{
textBox1.Text = i.ToString();
await Task.Delay(500);
}
}
[Edit] Note, if you want to get rid of the async warning:
#pragma warning disable 4014
EndlessTask();
#pragma warning restore 4014
i can't setText in textbox with infinite loop
you can never confirm that un till unless you debug your application while using the Thread.Sleep() in infinite loop.
Reason: when you use Thread.Sleep() it makes your Main thread to sleep so it is not a good practice to use Thread.Sleep() . it hangs your UI therefore you can not see the Control Updates on the UI like Label Text update ,TextBox Text Update things like that.
ofcourse you call Application.DoEvents() to refresh the UI but it is not a good practice as there are many other problems.
Solution: i suggest you to use Timer instead of using Thread.Sleep() as it runs in the background so it doesnot hang your UI and also you can see the updates on UI.
Simple Example to show you how to use Timer for updating the Text on Textbox # certain intervals
public partial class Form1 : Form
{
int count = 0;
string text1 = "this is a scrolling text";
System.Windows.Forms.Timer timer1 = new System.Windows.Forms.Timer();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
button1.Enabled = false;
textBox1.ReadOnly = true;
SetTimer(500);
}
private void SetTimer(int milliseconds)
{
timer1.Tick+=new EventHandler(timer1_Tick);
timer1.Interval = milliseconds;
timer1.Start();
}
private void timer1_Tick(Object o, EventArgs e)
{
if (count < text1.Length)
{
textBox1.Text += text1[count];
count++;
}
else
{
timer1.Stop();
button1.Enabled = true;
textBox1.ReadOnly = false;
}
}
}

Async Behaviour

I have the following code to update the progress bar in async fashion and i notice
its async behaviour through the call to MessageBox.In this case it works perfectly
but when i give a sleep of 1s(1000) the MessageBox doesnot pops up and the the complete progress bar fills at once.
Kindly tell why this is happening.
private void button1_Click(object sender, EventArgs e)
{
Update_Async async = new Update_Async(Update_Async_method);
progressBar1.BeginInvoke(async,10);
MessageBox.Show("Updation In Progress");
}
public void Update_Async_method(int a)
{
this.progressBar1.Maximum = a;
for (int i = 1; i <= a; i++)
{
progressBar1.Value = a;
Thread.Sleep(10);
//Thread.Sleep(1000);
}
}
Try Update_Async.BeginInvoke(async, 10) instead if you want the delegate to run asynchrnously but, you'll have to cross thread checking on the update to the progress bar.
In response to your comment, very similar to what you are doing already,
void UpdatingFunction(int value)
{
if (this.progressBar.InvokeRequired)
{
this.progressBar.BeginInvoke(UpdatingFunction, value);
return;
}
// Invoke not required, work on progressbar.
}
This also explains what the Invoke methods on controls are for.
Delegate.BeginInvoke will run a method in a thread once and then dispose it. It is a poor choice if you want to repeatedly do some work in a thread and return intermediate results. If that is what you want, you should use BackgroundWorker. Highly abbreviated snippet:
BackgroundWorker bw;
YourFormConstructor()
{
...
bw = new BackgroundWorker();
bw.WorkerReportsProgress = true;
bw.DoWork += BackgroundCalculations;
bw.ProgressChanged += ShowBackgroundProgress;
}
private void button1_Click(object sender, EventArgs e)
{
bw.RunWorkerAsync(10);
}
void ShowBackgroundProgress(object sender, ProgressChangedEventArgs e)
{
this.progressBar.Value = e.ProgressPercentage;
}
static void BackgroundCalculations(object sender, DoWorkEventArgs e)
{
BackgroundWorker bw = sender as BackgroundWorker;
int max = (int)e.Argument;
for (int i = 0; i < max; i++)
{
bw.ReportProgress(i * 100 / max);
Thread.Sleep(10);
}
bw.ReportProgress(100);
}
}

How to add delay inside a for-loop using timers

By following code(the interval of timer2 is 1000)
private void timer1_Tick(object sender, EventArgs e) {
timer7.Enabled=false;
timer8.Enabled=false;
lblTimer_Value_InBuildings.Text="0";
}
private void timer2_Tick(object sender, EventArgs e) {
lblTimer_Value_InBuildings.Text=(int.Parse(lblTimer_Value_InBuildings.Text)+1).ToString();
}
we can not create a delay in a for-loop
for(int i=1; i<=Max_Step; i++) {
// my code...
// I want delay here:
timer1.Interval=60000;
timer1.Enabled=true;
timer2.Enabled=true;
// Thread.Sleep(60000); // makes no change even if uncommenting
}
Whether I uncomment the line Thread.Sleep(60000); or not, we see nothing changed with lblTimer_Value_InBuildings in timer2_Tick.
Would you please give me a solution(with or without timers)?
Your timer is your loop, you don't need a for loop. You just keep track of your loop variables outside of the function calls. I would recommend wrapping all of this functionality into a class, to keep it separate from your GUI code.
private int loopVar = 0;
public void Form_Load()
{
// Start 100ms after form load.
timer1.Interval = 100;
timer1.Enabled = true;
}
private void timer1_Tick(object sender, EventArgs e)
{
timer1.Enabled = false;
// My Code Here
loopVar++;
if (loopVar < Max_Step)
{
// Come back to the _tick after 60 seconds.
timer1.Interval = 60000;
timer1.Enabled = true;
}
}
When you do Thread.Sleep(60000), you are telling the UI thread to sleep for 60 seconds. What this also does is prevent the execution of the timer, because the UI thread is hung up sleeping instead of processing events such as the timer sleep.
The code totally made me feel boring
methods:
private void timer1_Tick(object sender, EventArgs e) {
timer2.Enabled=false;
timer1.Enabled=false;
lblTimer_Value_InBuildings.Text="0";
}
private void timer2_Tick(object sender, EventArgs e) {
lblTimer_Value_InBuildings.Text=(int.Parse(lblTimer_Value_InBuildings.Text)+1).ToString();
}
private void timer3_Tick(object sender, EventArgs e) {
if(0!=(int)timer3.Tag) {
// your code goes here and peformed per step
timer1.Enabled=true;
timer2.Enabled=true;
}
timer3.Tag=(1+(int)timer3.Tag)%Max_Step;
}
initials:
var delayedInterval=60000;
timer1.Interval=60000;
timer2.Interval=1000;
timer3.Interval=delayedInterval+timer1.Interval;
lblTimer_Value_InBuildings.Text="0";
timer3.Tag=1;
timer3.Enabled=true;
Your original code never makes timer1 and timer2 stopped, so I think you should correct timer7 and timer8 to timer1 and timer2, otherwise they don't make sense here.

C# BackgroundWorker - how should i get rid of DoEvents

I'm trying to figure out the best way to handle a backgroundworker that is triggered off radio button clicks. I created a very simple form with 3 radio buttons and a label. Each of the radio buttons share the same event radioButton_CheckedChanged. If the event completes then I update the label to "Complete". If you click another radio button before the event completes then update label to Cancelled. Below is the code i have written in this quick example. Although the application tends to run as expected my concern is the use of Application.DoEvents. What are my alternatives to this. For obvious reasons i can't sleep while IsBusy. Am I going about this all wrong or is there a better way to do this?
Thanks, poco
private void radioButton_CheckedChanged(object sender, EventArgs e)
{
RadioButton rb = sender as RadioButton;
if (rb.Checked)
{
if (backgroundWorker1.IsBusy)
{
backgroundWorker1.CancelAsync();
while (backgroundWorker1.IsBusy)
Application.DoEvents();
}
backgroundWorker1.RunWorkerAsync();
}
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
for (int i = 0; i < 100 && !worker.CancellationPending; ++i)
Thread.Sleep(1);
if (worker.CancellationPending)
{
e.Cancel = true;
return;
}
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Cancelled)
label1.Text = "Canceled";
else
label1.Text = "Complete";
}
You should move the code that must run when the BackgroundWorker completes into the RunWorkerCompleted hander. In pseudo-code:
private void radioButton_CheckedChanged(object sender, EventArgs e)
{
// ...
if (backgroundWorker1.IsBusy)
{
backgroundWorker1.CancelAsync();
addJobToQueue(); // Don't wait here, just store what needs to be executed.
} else {
backgroundWorker1.RunWorkerAsync();
}
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Cancelled) {
label1.Text = "Canceled";
}
else {
label1.Text = "Complete";
}
// We've finished! See if there is more to do...
if (thereIsAnotherJobInTheQueue())
{
startAnotherBackgroundWorkerTask();
}
}
DoEvents is not supposed to be taken so casually. There are better ways. One of the very nice ones is described here in SO. This answer is probably best for you.
Hence your solution becomes:
private AutoResetEvent _resetEvent = new AutoResetEvent(false);
private void radioButton_CheckedChanged(object sender, EventArgs e)
{
RadioButton rb = sender as RadioButton;
if (rb.Checked)
{
if (backgroundWorker1.IsBusy)
{
backgroundWorker1.CancelAsync();
_resetEvent.WaitOne(); // will block until _resetEvent.Set() call made
}
backgroundWorker1.RunWorkerAsync();
}
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
for (int i = 0; i < 100 && !worker.CancellationPending; ++i)
Thread.Sleep(1);
if (worker.CancellationPending)
{
e.Cancel = true;
}
_resetEvent.Set();
}

Categories

Resources