DoWork of BackgroundWorker is called twice when RunWorkerAsync is called once?

前端 未结 7 1271
情深已故
情深已故 2020-12-06 01:51

I have create a backgroundworker in an class it works, but if i call and wait until the end run, call it for the second time it will do the same process twice

i thin

7条回答
  •  有刺的猬
    2020-12-06 02:16

    I have encounter same problem as above commenter "Power-Mosfet"

    and in the end, added a new BackgroundWorker() then assigned to the global bw value will fix my problem.

    code is, change from:

    private BackgroundWorker gBgwDownload;
    
    private void yourFunction_bw(xxx)
    {
        // Create a background thread
        gBgwDownload.DoWork += bgwDownload_DoWork;
        gBgwDownload.RunWorkerCompleted += bgwDownload_RunWorkerCompleted;
        //omited some code
        gBgwDownload.RunWorkerAsync(paraObj);
    }
    

    to:

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

提交回复
热议问题