C#线程运行和取消按钮,需要能够取消长期等待处理运行 [英] C# Threading Run and Cancel button, need to be able to cancel long proccessing run

查看:325
本文介绍了C#线程运行和取消按钮,需要能够取消长期等待处理运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户点击运行,应用程序通过大量的代码运行生成模型和图表显示。运行大约需要1-2分钟才能运行。我也有一个取消按钮被点击运行按钮后得到启用。我与DotSpatial工作,所以我的按钮都在一个带状UI插件板上。上运行的click事件,并取消在插件中,它调用后端类的代码运行,并单击开始。

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:

Run按钮被点击:

这是在顶部:

//check to see if RunModel thread needs to stop or continue
private volatile bool stopRun = false;
private Thread runThread;



然后,这是这是一个从click事件调用的方法:

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
   }



所以,我认为,当代码获取到runThread.Start(),它会跳进我的RunModel方法,并开始通过代码运行。但事实并非如此。此外,我会想取消此线程(一旦我有它正常工作),所以我有这个,它会从取消点击方法调用:

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;
    }
}



那么这是RunModel(),我'米检查偶尔看看是否STOPRUN布尔已经改变了。

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.....
}

在获得thread.start实际运行Run方法任何帮助吗?然后,我需要不断地检查挥发性布尔在运行,以清洁的一切行动,如果它被停止? !谢谢

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!

推荐答案

我觉得你最好看的BackgroundWorker - 这本质上奔跑分开,但可以留意取消命令。请确保您在初始化它添加'WorkerSupportCancellation':

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.

这篇关于C#线程运行和取消按钮,需要能够取消长期等待处理运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆