C#中BackgroundWorker中的DoWork执行了多次

【问题】

C#中,调试一个BackgroundWorker,发现一个问题:

DoWork在第一次执行完毕后,接下来,又进入了DoWork,

即DoWork被调用了多次。

 

【解决过程】

1.后来参考:

https://stackoverflow.com/questions/12231379/dowork-of-backgroundworker-is-called-twice-when-runworkerasync-is-called-once

所以去看了看自己的代码,也是+=:

private void getUrlRespStreamBytes_bw(ref Byte[] respBytesBuf,
                                string url,
                                Dictionary<string, string> headerDict,
                                Dictionary<string, string> postDict,
                                int timeout,
                                Action<int> funcUpdateProgress)
    {
        // Create a background thread
        gBgwDownload.DoWork += new DoWorkEventHandler(bgwDownload_DoWork);
        gBgwDownload.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgwDownload_RunWorkerCompleted);
        gBgwDownload.WorkerReportsProgress = true;
        gBgwDownload.ProgressChanged += new ProgressChangedEventHandler(bgwDownload_ProgressChanged);

        //init
        bNotCompleted_download = true;
        gFuncUpdateProgress = funcUpdateProgress;
        
        // run in another thread
        object paraObj = new object[] {respBytesBuf, url, headerDict, postDict, timeout};
        gBgwDownload.RunWorkerAsync(paraObj);
    }

所以就去把加号去掉:

private void getUrlRespStreamBytes_bw(ref Byte[] respBytesBuf,
                                string url,
                                Dictionary<string, string> headerDict,
                                Dictionary<string, string> postDict,
                                int timeout,
                                Action<int> funcUpdateProgress)
    {
        // Create a background thread
        gBgwDownload.DoWork = new DoWorkEventHandler(bgwDownload_DoWork);
        gBgwDownload.RunWorkerCompleted = new RunWorkerCompletedEventHandler(bgwDownload_RunWorkerCompleted);
        gBgwDownload.WorkerReportsProgress = true;
        gBgwDownload.ProgressChanged = new ProgressChangedEventHandler(bgwDownload_ProgressChanged);

        //init
        bNotCompleted_download = true;
        gFuncUpdateProgress = funcUpdateProgress;
        
        // run in another thread
        object paraObj = new object[] {respBytesBuf, url, headerDict, postDict, timeout};
        gBgwDownload.RunWorkerAsync(paraObj);
    }

试试结果。

结果却是,直接语法错误,都无法运行。

2.同样参考上贴,去改为,去掉new handler的形式:

private void getUrlRespStreamBytes_bw(ref Byte[] respBytesBuf,
                                string url,
                                Dictionary<string, string> headerDict,
                                Dictionary<string, string> postDict,
                                int timeout,
                                Action<int> funcUpdateProgress)
    {
        // Create a background thread
        gBgwDownload.DoWork += bgwDownload_DoWork;
        gBgwDownload.RunWorkerCompleted += bgwDownload_RunWorkerCompleted;
        gBgwDownload.WorkerReportsProgress = true;
        gBgwDownload.ProgressChanged += bgwDownload_ProgressChanged;

        //init
        bNotCompleted_download = true;
        gFuncUpdateProgress = funcUpdateProgress;
        
        // run in another thread
        object paraObj = new object[] {respBytesBuf, url, headerDict, postDict, timeout};
        gBgwDownload.RunWorkerAsync(paraObj);
    }

试试结果,结果问题依旧。

第二次调用DoWork后,紧接着,还是直接进入DoWork,而不是重新执行别的函数。

3.再去自己改为新建一个变量,而不是始终使用那个全局变量:

private void getUrlRespStreamBytes_bw(ref Byte[] respBytesBuf,
                                string url,
                                Dictionary<string, string> headerDict,
                                Dictionary<string, string> postDict,
                                int timeout,
                                Action<int> funcUpdateProgress)
    {
        // Create a background thread
        gBgwDownload = new BackgroundWorker();
        gBgwDownload.DoWork += bgwDownload_DoWork;
        gBgwDownload.RunWorkerCompleted += bgwDownload_RunWorkerCompleted;
        gBgwDownload.WorkerReportsProgress = true;
        gBgwDownload.ProgressChanged += bgwDownload_ProgressChanged;

        //init
        bNotCompleted_download = true;
        gFuncUpdateProgress = funcUpdateProgress;
        
        // run in another thread
        object paraObj = new object[] {respBytesBuf, url, headerDict, postDict, timeout};
        gBgwDownload.RunWorkerAsync(paraObj);
    }

试试,结果就可以了。

 

【总结】

BackgroundWorker的DoWork被执行多次,原因是

之前BackgroundWorker初始化的时候,DoWork赋值时,都是

new DoWorkEventHandler

的形式,所以每次都新加了个DoWork动作,所以会导致后续多次调用时,被执行多次。

另外,不能每次都

gBgwValue += xx_doWork;

否则,也还是会多把后续的多次的DoWork都加到同一个全局变量gBgwValue上,导致后续DoWork被多次调用执行;

最终,改为,不带new DoWorkEventHandler的,每次是新建一个BackgroundWorker的变量,就可以了:

private BackgroundWorker gBgwDownload;
    
private void yourFunction_bw(xxx)
{
    // Create a background thread
    gBgwDownload = new BackgroundWorker(); /* added this line will fix problem */
    gBgwDownload.DoWork += bgwDownload_DoWork;
    gBgwDownload.RunWorkerCompleted += bgwDownload_RunWorkerCompleted;
    //omited some code
    gBgwDownload.RunWorkerAsync(paraObj);
}
AI语音助手为例,介绍AI技术的优势以及对未来社会的影响。 AI技术曾经被认为是未来科技的梦想,但随着科技的飞速发展,AI技术真正开始融入人们生活和工作。AI语音助手是其最受欢迎的应用之一,它的出现不仅极大方便了人们生活,而且也代表着人工智能技术迈向一个新的阶段。 首先,AI语音助手的最大优势在于可以快速解决人类的疑惑。在日常生活,我们可能会遇到各种问题,例如查天气,交通路线,买东西等等。这时我们只需要说出问题,AI语音助手就会自动搜索、分析、查询,然后给出最合适的答案或建议。这种交互方式不仅更加简单方便,还解放了我们的双手和眼睛,可以专注于其他事情。 另外,AI语音助手还可以帮我们实现智能家居控制,如打开关闭灯光,调整温度等等。这些功能不仅能提高我们的居住舒适度,还能有效节省能源和减少浪费。 除此之外,AI技术也将对未来的社会产生深远的影响。人们将更多地依赖AI技术完成一些枯燥的工作,而AI技术也将取代一些传统行业的就业岗位。同时,AI技术也将改变人们的生活方式和工作方式,例如数字化和自动化时代的到来,虚拟现实的应用等。 总体来说,AI语音助手作为人工智能技术的代表之一,其所带来的便利和优势是明显的。虽然其也存在一些安全和隐私风险,但只要正确认识并严格保护,AI技术将会为未来社会的发展带来积极的影响。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值