C# 立即取消异步线程

发布于 2024-12-17 17:26:22 字数 1028 浏览 2 评论 0原文

当按下开始按钮时,我启动一个线程,该按钮启动延迟计时器,然后显示消息框对话框。 现在,我试图停止这个线程,但我找不到办法,除了添加一个标志,该标志将阻止线程显示 messageBox 对话框,但不会杀死该线程。 如果您能建议一种杀死线程的方法,我将不胜感激。

谢谢 莫蒂

public partial class Form1 : Form
{
    public delegate void example();
    ThreadA threadA = null;

    public Form1()
    {
        InitializeComponent();
    }

    example ex;
    IAsyncResult result;
    private void button_Start_Click(object sender, EventArgs e)
    {
            ex = new example(start);//.BeginInvoke(null, null);
            result = ex.BeginInvoke(null, null);
    }

    private void button_Stop_Click(object sender, EventArgs e)
    {
        if (threadA != null)
            threadA = null;
    }

    private void start()
    {
        if (threadA == null)
        {
            threadA = new ThreadA();
            threadA.run();
        }
    }

}




class ThreadA
{
    //public event
    public Boolean flag = false;
    public void run()
    {
        Thread.Sleep(15000);
        MessageBox.Show("Ended");
    }
}

I start a thread when pressing on start button which start a delay timer and then show a messageBox dialog.
Now, I'm trying to stop this thread, but I can't find a way for that, except add a flag which will prevent the thread to display the messageBox dialog but not to kill the thread.
I would appreciate if you can suggest a way to KILL the thread.

Thanks
Moti

public partial class Form1 : Form
{
    public delegate void example();
    ThreadA threadA = null;

    public Form1()
    {
        InitializeComponent();
    }

    example ex;
    IAsyncResult result;
    private void button_Start_Click(object sender, EventArgs e)
    {
            ex = new example(start);//.BeginInvoke(null, null);
            result = ex.BeginInvoke(null, null);
    }

    private void button_Stop_Click(object sender, EventArgs e)
    {
        if (threadA != null)
            threadA = null;
    }

    private void start()
    {
        if (threadA == null)
        {
            threadA = new ThreadA();
            threadA.run();
        }
    }

}




class ThreadA
{
    //public event
    public Boolean flag = false;
    public void run()
    {
        Thread.Sleep(15000);
        MessageBox.Show("Ended");
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

匿名。 2024-12-24 17:26:23

我将使用带有 CancellationTokenSourceTask 类。

 CancellationTokenSource cts = new CancellationTokenSource();
 Task t = new Task(() => new ThreadA().run(cts.Token), cts.Token); 

  // Start
  t.Start();
  ShowMessageBox(cts)

编辑2:对您的评论:

void ShowMessageBox(CancellationTokenSource cts)
{ 
    if(MessageBox.Show("StopThread",
    "Abort",MessageBoxButtons.YesNo,
     MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
    {
      cts.Cancel();
  }       
}

I'd Use the Task Class with a CancellationTokenSource.

 CancellationTokenSource cts = new CancellationTokenSource();
 Task t = new Task(() => new ThreadA().run(cts.Token), cts.Token); 

  // Start
  t.Start();
  ShowMessageBox(cts)

Edit2: to your Comment:

void ShowMessageBox(CancellationTokenSource cts)
{ 
    if(MessageBox.Show("StopThread",
    "Abort",MessageBoxButtons.YesNo,
     MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
    {
      cts.Cancel();
  }       
}
你げ笑在眉眼 2024-12-24 17:26:23

使用ManualResetEvent

class ThreadA
{
    ManualResetEvent _stopEvent = new ManualResetEvent(false);
    Thread _thread;

    public Boolean flag = false;
    public void run()
    {
        while (true)
        {
            if (_stopEvent.Wait(15000))
                return; // true = event is signaled. false = timeout

            //do some work
        }

        MessageBox.Show("Ended");
    }

    public void Start()
    {
        _stopEvent.Reset();
        _thread = new Thread(run);
        _thread.Start();
    }

    public void Stop()
    {
        _stopEvent.Set();
        _thread.Join();
        _thread = null;
    }
}

但是,如果线程不会一直工作,我会使用Timer。

Use ManualResetEvent

class ThreadA
{
    ManualResetEvent _stopEvent = new ManualResetEvent(false);
    Thread _thread;

    public Boolean flag = false;
    public void run()
    {
        while (true)
        {
            if (_stopEvent.Wait(15000))
                return; // true = event is signaled. false = timeout

            //do some work
        }

        MessageBox.Show("Ended");
    }

    public void Start()
    {
        _stopEvent.Reset();
        _thread = new Thread(run);
        _thread.Start();
    }

    public void Stop()
    {
        _stopEvent.Set();
        _thread.Join();
        _thread = null;
    }
}

However, I would use a Timer if the thread is not going to do work all the time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文