Ok i'm begginer at coding
So what i'm trying to do is button who gonna wait for
the user to click on one of the multiple other button to continue
void Mainbutton()
{
//the program run throw so method
//Wait for the user to choose one button (I made a numeric pad with buttons)
//Then use this information to work
}
I know my english isnt that good
Thanks a lot
Try something like this:
bool gotResponse = false;
//you need to run MainTask in a different thread
Thread thread = new Thread(new ThreadStart(DoWork));
thread.Start();
void DoWork()
{
//do some work
//when something else needed from user then popup message
MessageBox.Show("say whatever you need to say");
while(!gotResponse)
{
//note: this loop doesn't stop until gotResponse = true;
}
//do rest of your work
}
private button_Click(object sender, RoutedEventArgs e)
{
gotResponse = true;
}
Related
I have two thread which start running When user select play button.
But when use select Pause or Resume button my UI hangs because of Thread.Join().
Below is code, I am looking for some alternative to overcome this.
I already tried with Invoke but it's not working even with this approach my UI freeze the moment I call Thread methods.
ThreadStart m_executeThreadStart;
Thread m_executeThread;
//user Selected Start Button
private void Start()
{
m_executeThreadStart = new ThreadStart(method1);
m_executeThread = new Thread(m_executeThreadStart);
m_executeThread.Name = "ExecuteTestSession";
m_executeThread.IsBackground = true;
m_executeThread.Start();
// Start the asynchronous operation.
// InitializeBackgroundWorker();
// backgroundWorker1.RunWorkerAsync();
//Creating result sync thread
ThreadStart m_resultSyncThreadStart = new ThreadStart(method2);
Thread m_resultSyncThread = new Thread(m_resultSyncThreadStart);
m_resultSyncThread.Name = "SyncResultDatabase";
m_resultSyncThread.Start();
}
private void method1()
{
//do some work
//read data from OPC sever (device)
}
private void method2()
{
//do some work
//updated database accordingly method 1 data
}
//user Press Pause button
public void Suspend()
{
//do work
m_executeThread.Join();
//do work
}
//user Press Resume button
public void Resume()
{
//do work
m_executeThread.Join();
//do work
}
You can add an event handler to your background worker, then you don't need Thread.Join(). Your event handler can then perform the necessary actions when the long running process has completed:
backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
// code to run after long running process has completed
}
The async / await pattern was designed to stop the UI from freezing (when implemented correctly) you could use this as an alternative approach - https://learn.microsoft.com/en-us/dotnet/csharp/async
Also see: https://learn.microsoft.com/en-us/windows/uwp/debug-test-perf/keep-the-ui-thread-responsive
i founded solution , below is link for answer.
How to pause/suspend a thread then continue it?
ManualResetEvent mrse = new ManualResetEvent(true);
ThreadStart m_executeThreadStart;
Thread m_executeThread;
//user Selected Start Button
private void Start()
{
m_executeThreadStart = new ThreadStart(method1);
m_executeThread = new Thread(m_executeThreadStart);
m_executeThread.Name = "ExecuteTestSession";
m_executeThread.IsBackground = true;
m_executeThread.Start();
// Start the asynchronous operation.
// InitializeBackgroundWorker();
// backgroundWorker1.RunWorkerAsync();
//Creating result sync thread
ThreadStart m_resultSyncThreadStart = new ThreadStart(method2);
Thread m_resultSyncThread = new Thread(m_resultSyncThreadStart);
m_resultSyncThread.Name = "SyncResultDatabase";
m_resultSyncThread.Start();
}
private void method1()
{
//do some work
//read data from OPC sever (device)
mrse.WaitOne();
}
private void method2()
{
//do some work
//updated database accordingly method 1 data
mrse.WaitOne();
}
//user Press Pause button
public void Suspend()
{
//do work
mrse.Reset();
//do work
}
//user Press Resume button
public void Resume()
{
//do work
mrse.Set();
//do work
}
Appreciated your help.
Here's the thing, I have started with C# and I want to do something like this:
I have a Windows Form Application with one button and the picturebox.
Clicking on a button should result in turning property "Running" on true/false, by the actual state. That is done.
Also, it should result into turning on the script that will be doing constantly a job as the program runs. This "job" will be described in Run() method. And I want this method to be executed only when Running == true, in moment it becomes false, the method should end.
So I decided put it into the thread and in the method where I switch between Running = true and Running = false, I try to start the thread and abort it.
Why do I want to do this? Because I want to be able to turn program working on and off by the button I mentioned at the beginning.
This is what I came up with:
Thread thProgram;
public Form1()
{
InitializeComponent();
thProgram = new Thread(new ThreadStart(this.Run));
}
private bool Running = false;
public void Run()
{
int i = 0;
while(this.Running)
{
i++;
}
MessageBox.Show("Terminated");
}
// handling bot activation button (changing color of a pictureBox1), switching this.Running property
private void button1_Click(object sender, EventArgs e)
{
if(this.Running)
{
thProgram.Abort();
pictureBox1.BackColor = Color.Red;
this.Running = false;
}
else
{
thProgram.Start();
pictureBox1.BackColor = Color.Lime;
this.Running = true;
}
}
I can click the button exactly twice, and it appears that everything is all right... but when I click it for the third time, error pops up:
(it highlights the line "thProgram.Start();"
An unhandled exception of type 'System.Threading.ThreadStateException' occurred in mscorlib.dll
Additional information: Thread is running or terminated; it cannot restart.
Thanks in advance for any help you are able to provide me with.
The exception is Self-explaining
When you press the button for the first time, the thread starts and falls into its main loop.
The second button press aborts the thread (which is always a bad idea. That flag you used is enough) and the thread terminates.
The third button press? From MSDN documentation for Thread.Start() :
Once the thread terminates, it cannot be restarted with another call to Start.
To freeze a thread without terminating it, I suggest using AutoResetEvent:
public Form1()
{
InitializeComponent();
thProgram = new Thread(new ThreadStart(this.Run));
}
private bool Running = false;
private AutoResetEvent ThreadHandle = new AutoResetEvent(false);
public void Run()
{
int i = 0;
while(true)
{
ThreadHandle.WaitOne();
i++;
}
MessageBox.Show("Terminated");
}
// handling bot activation button (changing color of a pictureBox1), switching this.Running property
private void button1_Click(object sender, EventArgs e)
{
if(this.Running)
{
thProgram.Abort();
pictureBox1.BackColor = Color.Red;
this.ThreadHandle.Reset();
this.Running = false;
}
else
{
thProgram.Start();
pictureBox1.BackColor = Color.Lime;
this.ThreadHandle.Set();
this.Running = true;
}
}
Why does my first attempt to change a button's text in this code not work, while the third attempt does work ?
My user will have to wait a few seconds while the serial port connects. After that, I want to alert him that he has already connected (a second attempt can cause problems).
I wanted to let him know that things are okay, so he won't think "duh" and click twice.
Fail. The text change never appears.
Okay, why does the third change in button text work, but the first one does not ? I don't know if the second one works or not.
/***********************************************************************
* Button: Connect Port *
***********************************************************************/
private void TheConnectPortButton_Click(object sender, EventArgs e)
{
string OldText = TheConnectPortButton.Text;
TheConnectPortButton.Text = "Busy, Please Wait"; /////// This never happens
ButtonBoss.ButtonHandler_ConnectPort();
TheConnectPortButton.Text = OldText; /////// Not sure if this happens
if (aUartSemaphoreThatTells.IfWeHaveConnectedToAPort == (int)aValueWhichIndicatesThat.YesWeHaveAGoodPortConnected)
{
TheConnectPortButton.Text = "Connected"; /////// This one does happen
}
}
the aUartSemaphoreThatTells.IfWeHaveConnectedToAPort is also used by the ButtonBoss routine to make sure he doesn't connect a second time, along with other button routines (e.g., make sure we are connected before we Tx/Rx or whatever).
I tried changing the code after the routine returns to look like this...
if (aUartSemaphoreThatTells.IfWeHaveConnectedToAPort == (int)aValueWhichIndicatesThat.YesWeHaveAGoodPortConnected)
{
TheConnectPortButton.Text = "Connected";
}
else
{
TheConnectPortButton.Text = OldText;
}
...and I still get the same result.
My guess (and that's all it is) is that threading is somehow involved in all this, and that the serial port routines trump the button text changing routines by some convolution that I don't follow properly at the moment.
Question: What do I need to do to get the text to change before the connection stuff hogs the system ?
(If that's what's happening)
Question 2: If I can't make this happen, I think I've read about "greying out" the buttons, or, I believe I saw somewhere that I can actually make a button go away right before the user's eyes so that he can't click it again. Links to example code would be welcome.
The problem is you're doing everything from one and the same event-handler consequently, so that the button has no time to get updated (redrawn). You could call Application.DoEvents(); method, but it's not a good idea at all, please, read Use of Application.DoEvents()
I think usually you're expected to push a time-consuming task into a separate thread, get progress report from it and update your GUI. There is a plenty of ways to create a "worker" thread and get some respond from it. For example, use a BackgroundWorker Class:
public partial class Form1 : Form
{
public Form1() { InitializeComponent(); }
private void button1_Click(object sender, EventArgs e)
{
BackgroundWorker w = new BackgroundWorker();
w.WorkerReportsProgress = true;
w.DoWork += new DoWorkEventHandler(w_DoWork);
w.ProgressChanged += new ProgressChangedEventHandler(w_ProgressChanged);
w.RunWorkerCompleted += new RunWorkerCompletedEventHandler(w_RunWorkerCompleted);
w.RunWorkerAsync();
button1.Text = "Started";
}
//may influence GUI, as this event handler is run on the GUI thread
void w_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
button1.Text = "Job is done";
}
//may influence GUI, as this event handler is run on the GUI thread
void w_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
button1.Text = e.ProgressPercentage.ToString();
}
//runs in the worker thread...should do the actual job
//may influence GUI through `ReportProgress`
//or through `Invoke` method
void w_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
for (int i = 1; i <= 10; i++)
{
Thread.Sleep(500);
worker.ReportProgress(10 * i);
}
}
}
Or you may use Task Class:
public partial class Form1 : Form
{
public Form1() { InitializeComponent(); }
private void button1_Click(object sender, EventArgs e)
{
new Task(DoTask).Start();
}
void DoTask()
{
for (int i = 1; i <= 10; i++)
{
Thread.Sleep(500);
//here you need to update GUI through `Invoke` method
//as the GUI may only be influenced from the the thread,
//where it's created
this.Invoke(new Action<int>((j) =>
{
button1.Text = j.ToString();
}), 10 * i);
}
}
}
When a user clicks on Run, the application runs through a lot of code to generate a model and display it in a Chart. The Run takes about 1-2 minutes to run. I also have a Cancel button that gets enabled after the Run button is clicked. I am working with DotSpatial, so my buttons are on a plugin panel in a ribbon UI. The click event on the Run and Cancel start in the plugin, which calls the back-end class's code Run and Click.
When the user hits cancel after the run starts, there is a delay, but the cancel method is invokes and executes, but the run never stops and we eventually see the chart display. So, I'm thinking I need a separate thread for the Run. I'm fairly new to programming, and never worked with Threading. I've looked into it and added the below code, but my thread method isn't running. Here's my code:
The Run button is clicked:
This is at the top:
//check to see if RunModel thread needs to stop or continue
private volatile bool stopRun = false;
private Thread runThread;
Then this is the method that's called from the click event:
public void btnRun_testingThread(object sender, EventArgs e)
{
//create a new thread to run the RunModel
if (runThread == null)
{
//we don't want to stop this thread
stopRun = false;
runThread = new Thread(RunModel);
runThread.Start(); <--this isn't doing anything
}
So, I would think that when the code gets to the runThread.Start(), it would jump into my RunModel method and start running through the code. But it doesn't. Additionally, I'll want to cancel out of this thread (once I have it working correctly), so I have this, which gets called from the cancel click method:
private void StopRunThread()
{
if (runThread != null)
{
//we want to stop the thread
stopRun = true;
//gracefully pause until the thread exits
runThread.Join();
runThread = null;
}
}
Then the this is the RunModel() where I'm checking occasionally to see if the stopRun bool has changed.
public void RunModel()
{
...some code.....
//check to see if cancel was clicked
if (stopRun)
{
....clean up code....
return;
}
....some more code....
//check to see if cancel was clicked
if (stopRun)
{
....clean up code....
return;
}
}
And the cancel button click method:
public void btnCancel_Click(Object sender, EventArgs e)
{
stopRun = true;
StopRunThread();
//the model run has been canceled
....some code.....
}
Any help on getting the thread.start to actually run the Run method? Then do I need to constantly check the volatile bool in the run in order to clean everything up if it's being stopped? Thanks!
I think you'd be best looking at the BackgroundWorker - this essentially runs separately but can watch out for cancellation commands. Make sure you add 'WorkerSupportCancellation' when you initialise it:
BackgroundWorker backgroundWorker1 = new BackgroundWorker();
backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork); // This does the job ...
backgroundWorker1.WorkerSupportsCancellation = true; // This allows cancellation.
Then on click you can start your process:
public void btnRun_testingThread(object sender, EventArgs e)
{
backgroundWorker1.RunWorkerAsync();
}
Your cancel button can issue a cancellation request:
public void btnCancel_Click(Object sender, EventArgs e)
{
backgroundWorker1.CancelAsync();
}
Then your worker can monitor for this as it's doing it's work ...
void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
for (int i = 0; i < 100; i++)
{
if (backgroundWorker1.CancellationPending)
{
break;
}
else
{
// Do whatever you're doing.
}
}
e.Result = backgroundWorker1.CancellationPending ? null : orders;
}
You can enhance this further by adding progress bars etc., but that gets a bit more complicated so I won't go into it here.
Considering new info provided in commend I believe you just missed a start of the RunModel() method in debugger because of wrong assumption regarding thread.Start() method behaviour.
Please see a note from MSDN, Thread.Start Method
Once a thread is in the ThreadState.Running state, the operating
system can schedule it for execution. The thread begins executing
at the first line of the method represented by the ThreadStart or
ParameterizedThreadStart delegate supplied to the thread constructor.
Small demonstration that thread start takes some time bits, for me it starts in 38-40 milliseconds:
Stopwatch watch = new Stopwatch();
Thread thread = new Thread((ThreadStart)watch.Stop);
thread.Start();
watch.Start();
Thread.Sleep(5000);
double startedAfter = watch.ElapsedMilliseconds;
Since .NET Framework 4.0 consider using TPL Tasks rather than threads explicitly, some pros:
You can easily synchronize with UI thread by passing in a Task UI Thread synchronization context
You can easily stop a Taks using CancellationToken
So I have two event handlers button1_Click() and button2_Click()
In button1_Click() I have something running like this:
toGet = textbox1.Text;
got = 0;
while (got <= toGet)
{
//DoStuff
}
But button2_Click is supposed to be a stop button, and stop button1 early.
How do I go about this?
Thanks for the help. I saw this article here about it, but couldn't get it to work.
Windows.Forms answer
The least sophisticated method is this:
private bool m_stop;
private void button1_Click (object s, EventArgs ea)
{
try
{
// Don't forget to disable all controls except the ones you want a user to be able to click while your method executes.
toGet = textbox1.Text;
got = 0;
while (got <= toGet)
{
Application.DoEvents ();
// DoEvents lets other events fire. When they are done, resume.
if (m_stop)
break;
//DoStuff
}
finally
{
// Enable the controls you disabled before.
}
}
private void button2_Click (object s, EventArgs ea)
{
m_stop = true;
}
It has the distinct advantage of letting you execute button1_Click on the UI thread, still lets the UI respond to your stop button.
It has a disadvantage that you must protect against reentrancy. What happens if they click your button1 while button1_click is already executing!?!?
Edit: Another way I have used is to use a Timer instead of a loop. Then, the stop method just stops the timer.
As much as I understood, correct me if I'm wrong, you're on single thread.
Wired, but you can check for single boolean value inside the your While loop, just as post suggested.
May be to make life easier (may be this is what "couldn't get it to work" means) is inside loop call
1) Windows Forms: Application.DoEvents()
2) WPF (little bit more tricky) : DoEvents in WPF
This to make breathe system.
You need to start the process inside the button1 in new thread, and when you press the button2 flag a local variable to false to stop the loop. like:
using System.Threading;
private volatile bool _requestStop = false;
private readonly object _oneExecuteLocker = new object();
private void OnButton1Click(ojbect sender, EventArgs e)
{
new Thread(() =>
{
if (Monitor.TryEnter(_oneExecuteLocker))
{//if we are here that is means the code is not already running..
try
{
while (!_requestStop)
{
//DoStuff
}
}
finally
{
Monitor.Exit(_oneExecuteLocker);
}
}
}){ IsBackground = true }.Start();
}
private void OnButton2Click(object sender, EventArgs e)
{
_requestStop = true;
}
Notes:
When ever you want to update a UI control inside the newly created thread you should use contorl.Invoke(/*the code get/set or call method in the UI*/).
The Monitro.Enter is just to be sure that your code will not executed multiple time per click if it already running.