private void materialRaisedButton16_Click(object sender, EventArgs e)
{
foreach (var process in Process.GetProcessesByName("RobloxPlayerBeta"))
{
process.Kill();
}
materialRaisedButton16.Text = "Successfully killed process!";
// sleep for 2s WITHOUT freezing GUI
materialRaisedButton16.Text = "Click to kill process";
}
Hi, my code is above. I need the text of the button to change for 2s then change back to the original. How is this possible?
Thanks,
Tim
Implement like this
private async Task DelayTask()
{
await Task.Delay(2000); //2000 = 2sec
DoyourStuffHere();
materialRaisedButton16.Text = "Click to kill process";
}
And Call It Like This
private void materialRaisedButton16_Click(object sender, EventArgs e)
{
foreach (var process in Process.GetProcessesByName("RobloxPlayerBeta"))
{
process.Kill();
}
materialRaisedButton16.Text = "Successfully killed process!";
// sleep for 2s WITHOUT freezing GUI
Task taketime = this.DelayTask();
}
Not freezing the GUI requires some form of Mutlitasking. Possibly even Multithreading. Very strictly speaking calling a bunch of helper processes is a primitive form of Multithreading already. Possibly the oldest one, we invented just as we came off Cooperative Multitasking back in the days.
You have many Options to do Multitasking (inlcuding Multithreading) in .NET Async...await. Tasks. Threads. For beginners in Multithreading, I would advise BackgroundWorker generally. I wrote this little intro examples a few years back that I link often:
#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
Of course in your case, something much simpler could work: A baseline Timer. All you really want is a 2 second delay? Make a TImer set to 2 seconds, no repeat, start it in "materialRaisedButton16_Click". And let it's tick to the rest. All true multithreading really does is allow you to write the same stuff in slightly more readable form (with some tradeoffs for performance).
the easiest way would be:
foreach (var process in Process.GetProcessesByName("RobloxPlayerBeta"))
{
process.Kill();
}
materialRaisedButton16.Text = "Successfully killed process!";
// sleep for 2s WITHOUT freezing GUI
Task.Delay(2000).ContinueWith(()=>{
materialRaisedButton16.Text = "Click to kill process";
}, TaskScheduler.FromCurrentSynchronizationContext()); // this is to make it run in the UI thread again
You can use Timer.
On its tick event you update the text of the button back to the value u need.
Related
I have a tcp server and client in c# using Sytem.Net.Sockets. My worker function is as following
private void Worker_DoWork(object sender, DoWorkEventArgs e)
{
byte[] buffer = new byte[1];
socket.Receive(buffer);
chatTextBox.Text = buffer[0].ToString();
}
I'm calling this after the server is created, as soon as it receives a message from the client it writes it on the screen and stops. My issue is I want it at the end to call itself again so it waits for another message to display. If I just add worker.RunWorkerAsync(); at the bottom it doesn't work, however if I just call it from another button it works and receives and writes the message.
The BackgroundWorker "wraps" around a Thread and helps you with all the plumbing. It is a horribly dated approach to Multitasking taht you should not use in producive code anymore. However it is also the best "Training Wheels" for Multitasking I know off.
Your fundamental approach is flawed. The BGW not allowing that is really just it helping you learn the things you need to learn. A list of mistakes in your code:
you are accessing a GUI element directly in DoWork. Do not do that. Only write the UI in ReportProgress and RunWorker completed Events. This is a general Multithreading rule, that is helped by teh BGW's design.
you are trying to restart the thread, before it has finished. If you wanted to restart it, RunWorkerCompleted would be the right place
however more sensible would be for the core of the BackgroundWorker to be a semi-infinite loop. Something that runs until canceled. Normally handing out inforamtion with reporting is not a good idea, but in this case it is the best idea I have.
The only other thing I can give you, is my old BGW example code:
#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
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/)
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();
}
i have a strange situation.
please see the backgroundWorker5_RunWorkerCompleted event:
private void backgroundWorker5_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
btnStartAdventures.Text = "Start Adventure";
btnStartAdventures.Enabled = true;
if (e.Error != null)
{
MessageBox.Show(e.Error.Message);
return;
}
if (e.Cancelled)
{
lblStatusValueInAdventures.Text = "Cancelled...";
}
else
{
lblStatusValueInAdventures.Text = "Completed";
timer1.Enabled = true;
timer1.Start();
// MessageBox.Show("start timer");
Thread.Sleep((int.Parse(txtDelayInAdventures.Text)) * 60000);
//MessageBox.Show("end timer");
timer1.Enabled = false;
timer1.Stop();
lblTimer.Text = "0";
btnStartAdventures.PerformClick();
}
}
and that Timer is :
private void timer1_Tick(object sender, EventArgs e)
{
this.Invoke(new MethodInvoker(delegate { lblTimer.Text = (int.Parse(lblTimer.Text) + 1).ToString(); }));
}
but this timer can not change lblTimer's Text.
how can i fix this problem?
EDIT:
that Thread.Sleep is necessary and i can not remove it.
i want a loop that never ends and those codes are for that.
thanks in advance
As requested;
What do you mean by "a loop that never ends"? A Thread.Sleep on the UI thread (RunWorkerCompleted event executes on the UI thread) will effectively freeze the UI thread, which means that no interaction with the UI thread will be shown.
Comments:
What are you trying to achieve? As far as I can guess, you are doing
some work in a background thread - backgroundWorker5 - (the UI thread
is responsive). When backgroundWorker5 is finished you want to start a
timer and display a counter in a label while the UI is still
responsive (for somebody to stop the timer maybe?). Something like
that? – Mario 3 mins ago edit
yes you are right. i want a loop and it never stops until a user click
cancel button. – MoonLight 1 min ago
So, try something like this:
int time = 0;
private void backgroundWorker5_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
btnStartAdventures.Text = "Start Adventure";
btnStartAdventures.Enabled = true;
if (e.Error != null)
{
MessageBox.Show(e.Error.Message);
return;
}
if (e.Cancelled)
{
lblStatusValueInAdventures.Text = "Cancelled...";
}
else
{
lblStatusValueInAdventures.Text = "Completed";
timer1.Interval = 1000; //<--- Tick each second, you can change this.
timer1.Enabled = true;
timer1.Start();
// MessageBox.Show("start timer");
}
}
private void timer1_Tick(object sender, EventArgs e)
{
lblTimer.Text = (time + 1).ToString();
}
private void button_Cancel_Click(object sender, EventArgs e)
{
//MessageBox.Show("end timer");
timer1.Enabled = false;
timer1.Stop();
lblTimer.Text = "0";
btnStartAdventures.PerformClick();
}
Thread.Sleep
There's your problem.
Never call Thread.Sleep in a UI thread; it will freeze the UI.
Get rid of that, and it will work fine.
You can put the rest of the work in the timer callback.
You can also use C# 5 async to make this much simpler.
You have to refresh item.
lblTimer.Refresh()
and also you could refresh form
frmName.Refresh();
and make thread to sleep 0 milliseconds that gives space for other processes.
In a win form application, I have an array of threads which are started like this:
bool stop = false;
Thread[] threads = new Thread[10];
for (int i = 0; i < threads.Length; i++)
threads[i] = new Thread(new ThreadStart(Job));
// How to make sure all threads have exited, when the boolean = false
void Job()
{
while (!stop)
// Do something
}
Now if user press STOP, the boolean value for stop will set to true, so threads exit the Job method one after another. How can I make sure all threads are exited?
NOTE: I need traditional threading for my case and TaskLibrary doesn't fit my scenario.
Use the Join method to check if all threads have stopped.
foreach (var t in threads)
{
t.Join();
}
Have you thought about using BackgroundWorkers instead? You said "traditional threads"..I'm not exactly sure what you mean so I don't know if this is a valid proposal or not, but here it is anyways in case Join() doesn't solve your problem
BackgroundWorker[] workers = new BackgroundWorker[10];
bool allThreadsDone = false;
// initialize BackgroundWorkers
for (int i = 0; i < 10; i++)
{
workers[i] = new BackgroundWorker();
workers[i].WorkerSupportsCancellation = true;
workers[i].RunWorkerCompleted +=
new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
workers[i].DoWork += new DoWorkEventHandler(AlgorithmsUI_DoWork);
workers[i].RunWorkerAsync();
}
// thread entry point..DoWork is fired when RunWorkerAsync is called
void AlgorithmsUI_DoWork(object sender, DoWorkEventArgs e)
{
while (!stop)
// do something
}
// this event is fired when the BGW finishes execution
private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
bool threadsStillRunning = false;
foreach (BackgroundWorker worker in workers)
{
if (worker.IsBusy)
{
threadsStillRunning = true;
break;
}
}
if (!threadsStillRunning)
allThreadsDone = true;
}
protected override OnFormClosing(FormClosingEventArgs e)
{
if (!allThreadsDone)
{
e.Cancel = true;
MessageaBox.Show("Threads still running!");
}
}
This should prevent your form from closing if any threads are still running.
I'm not sure if this is what you're looking for, but here's a simple solution I used back in .NET 3.0 to make sure a large but deterministic number of threads had completed before continuing:
Global:
AutoResetEvent threadPoolComplete = new AutoResetEvent(false);
static int numThreadsToRun;
As you activate the threads:
numThreadsToRun = [number of threads];
[start your threads];
threadPoolComplete.WaitOne();
At the end of each thread's code:
if (Interlocked.Decrement(ref numThreadsToRun) == 0)
{
threadPoolComplete.Set();
}