红着眼 2023-11-22 20:32 采纳率: 27.8%
浏览 5
已结题

C# BackgroundWorker通过一个按钮来重新的启动线程。

C# BackgroundWorker多线程怎么用一个按钮来重新启动呢?

如下代码,Button_Click的开始是正常,Button_Click_1的停止也是正常的。
想要做到的效果是点击Button_Click_2,判断线程如果是取消了的,就开启,如果是开启的就取消了重新的开启。
也就是将Button_Click和Button_Click_1的功能通过一个按钮来实现。
Button_Click_2方法中这样的写法运行到后面会报BackgroundWorker调用线程无法访问此对象,因为另一个线程拥有该对象。的错误

/// <summary>
/// 开始执行后台操作
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Button_Click(object sender, RoutedEventArgs e)
{
    if (ordernChangedWorker.IsBusy == false)
    {
        textLB.Content = "开始了";
        ordernChangedWorker.RunWorkerAsync();
    }
}
/// <summary>
/// 取消,停止
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Button_Click_1(object sender, RoutedEventArgs e)
{
    MessageBox.Show("取消中");
    ordernChangedWorker.CancelAsync();
}

/// <summary>
/// 重新开始
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Button_Click_2(object sender, RoutedEventArgs e)
{
    if (ordernChangedWorker.IsBusy)
    {
        MessageBox.Show("取消中");
        ordernChangedWorker.CancelAsync();
        //取消后,用线程判断IsBusy,等待继续启动
        new Thread((ThreadStart)(delegate()
        {
            while (ordernChangedWorker.IsBusy)
            {
                Thread.Sleep(100);
            }
            ordernChangedWorker.RunWorkerAsync();
        })).Start();
    }
    else
    {
        ordernChangedWorker.RunWorkerAsync();
    }
}
  • 写回答

8条回答 默认 最新

  • 社区专家-Monster-XH 2023-11-22 20:38
    关注

    ~~启动

    
    using System;
    using System.ComponentModel;
    using System.Threading;
    using System.Windows;
    
    namespace YourNamespace
    {
        public partial class YourWindow : Window
        {
            private BackgroundWorker ordernChangedWorker;
    
            public YourWindow()
            {
                InitializeComponent();
    
                ordernChangedWorker = new BackgroundWorker();
                ordernChangedWorker.WorkerSupportsCancellation = true;
                // 订阅 RunWorkerCompleted 事件
                ordernChangedWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(ordernChangedWorker_RunWorkerCompleted);
            }
    
            private void Button_Click(object sender, RoutedEventArgs e)
            {
                if (!ordernChangedWorker.IsBusy)
                {
                    textLB.Content = "开始了";
                    ordernChangedWorker.RunWorkerAsync();
                }
            }
    
            private void Button_Click_1(object sender, RoutedEventArgs e)
            {
                MessageBox.Show("取消中");
                ordernChangedWorker.CancelAsync();
            }
    
            private void Button_Click_2(object sender, RoutedEventArgs e)
            {
                if (ordernChangedWorker.IsBusy)
                {
                    MessageBox.Show("取消中");
                    ordernChangedWorker.CancelAsync();
                }
                else
                {
                    textLB.Content = "开始了";
                    ordernChangedWorker.RunWorkerAsync();
                }
            }
    
            private void ordernChangedWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
            {
                if (e.Cancelled)
                {
                    // 重新启动
                    ordernChangedWorker.RunWorkerAsync();
                }
                else
                {
                    // 操作完成
                    textLB.Content = "操作完成";
                }
            }
    
            // 这里是 BackgroundWorker 的 DoWork 事件处理器
            // 根据具体业务逻辑来实现
            private void ordernChangedWorker_DoWork(object sender, DoWorkEventArgs e)
            {
                // 你的业务逻辑代码
            }
        }
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(7条)

报告相同问题?

问题事件

  • 系统已结题 12月1日
  • 已采纳回答 11月23日
  • 修改了问题 11月22日
  • 创建了问题 11月22日

悬赏问题

  • ¥15 asp写PC网站开通了微信支付,扫码付款不能跳转
  • ¥50 AI大模型精调(百度千帆、飞浆)
  • ¥15 关于#c语言#的问题:我在vscode和codeblocks中编写c语言时出现打不开源文件该怎么办
  • ¥15 非科班怎么跑代码?如何导数据和调参
  • ¥15 福州市的全人群死因监测点死亡原因报表
  • ¥15 Altair EDEM中生成一个颗粒,并且各个方向没有初始速度
  • ¥15 系统2008r2 装机配置推荐一下
  • ¥500 服务器搭建cisco AnyConnect vpn
  • ¥15 悬赏Python-playwright部署在centos7上
  • ¥15 psoc creator软件有没有人能远程安装啊