When the user clicks the Run button, the application runs a lot of code to create the model and display it in the diagram. Launch takes about 1-2 minutes. I also have a Cancel button, which is activated after clicking the Run button. I work with DotSpatial, so my buttons are on the plugins panel in the ribbon interface. The start and cancel click event is fired in the plugin, which calls the code of the Back-End Run and Click class.
When the user clicks on cancel after start, there is a delay, but the cancel method starts and executes, but the execution never stops, and in the end we see the chart display. So, I think I need a separate thread for Run. I am new to programming and have never worked with Threading. I looked through it and added the code below, but my stream method does not work. Here is my code:
Click the Run button:
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 invoked from the click event:
public void btnRun_testingThread(object sender, EventArgs e) {
So, I think that when the code gets into runThread.Start (), it will go into my RunModel method and start working through the code. But this is not so. Also, I want to cancel this thread (as soon as I process it correctly), so I have this one that is called from the cancel click method:
private void StopRunThread() { if (runThread != null) {
Then it's RunModel (), where I sometimes check if the stopRun bool parameter 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 method of pressing the cancel button:
public void btnCancel_Click(Object sender, EventArgs e) { stopRun = true; StopRunThread();
Any help on getting thread.start to actually run the Run method? Then I need to constantly check the volatile bool at startup to clear everything if it is stopped? Thanks!