BackgroundWorker即使该方法尚未完成,也已完成

时间:2016-03-17 12:53:35

标签: c# asynchronous backgroundworker

这里的任何人都可以查看我的代码。

尚未完成处理事情的方法

但是Backgroundworker已经完成了这个过程。

        bg.DoWork += Bg_DoWork;

        bg.RunWorkerCompleted += (object ss, RunWorkerCompletedEventArgs ee) =>
        {
            _sysLog("Successfully uploaded.", Color.Green);              
        };

        bg.RunWorkerAsync();

后台工作人员开展工作。

    private void Bg_DoWork(object sender, DoWorkEventArgs e)
    {           
        var files = Folder.GetFileToPublicFolder(Folder.srcFolder);

        foreach (var chunk in files.Split(10))
        {
            foreach (var item in chunk)
            {
                File.Move(Path.Combine(Folder.srcFolder, item.Name), Path.Combine(Folder.tmpFolder, item.Name));
            }

            ProcessParallelThread(e);
        }

    }

Parallel Thread将处理公用文件夹中的所有文件。

    private void ProcessParallelThread(DoWorkEventArgs e)
    {
        object LockObject = new object();

        var Files = Folder.GetFileToPublicFolder(Folder.tmpFolder);

        Parallel.ForEach(Files, new ParallelOptions { MaxDegreeOfParallelism = 10 }, xFile =>
        {
            lock (LockObject)
            {
                bg.ReportProgress(0, Thread.CurrentThread.ManagedThreadId + " :(" + xFile + ") : Started ");
            }
            try
            {
                UploadFile(xFile);

                Thread.Sleep(1000);

                lock (LockObject)
                {
                    bg.ReportProgress(0, Thread.CurrentThread.ManagedThreadId + " :(" + xFile + ") : Ended =>>");
                }
            }
            catch (Exception)
            {
                bg.ReportProgress(0, Thread.CurrentThread.ManagedThreadId + " :(" + xFile + ") : Error X___X");

            }
        });
    }

将根据需要完成的线程数量来处理此上传文件。

    private void UploadFile(FileInfo file)
    {
        var slicePDF = new PDFSplitter();

        var pdf = slicePDF.SplitPdf(file);

        var poExist = new POProcess();

        if (poExist.checkPO(pdf.pono).Result)
        {
            bg.ReportProgress(0, Thread.CurrentThread.ManagedThreadId + " :(" + file.Name + ") : File Exist X___X");

            File.Delete(Folder.tmpFolder + "\\" + file.Name);

        }
        else
        {
            var processPDF = new FTPProcess();

            var link = processPDF.uploadPDF(Folder.tmpFolder + "\\" + file.Name, pdf.sid).Result;

            if (link == "Error")
            {
                bg.ReportProgress(0, Thread.CurrentThread.ManagedThreadId + " :(" + file.Name + ") : Link Error X___X");

                MoveToErrFolder(file);
            }
            else
            {
                var po = new PO();

                po.sid = pdf.sid;
                po.pono = pdf.pono;
                po.region = pdf.region;
                po.location = pdf.location;
                po.division = pdf.division;
                po.link = link;
                po.filestatus = "Available";
                po.released = DateTime.Now;
                po.expiration = DateTime.Now.AddDays(7);
                po.isExpired = "no";

                if (poExist.addPO(po).Result)
                {
                    bg.ReportProgress(0, Thread.CurrentThread.ManagedThreadId + " :(" + file.Name + ") : Success");

                    Folder.CreateBackFolder(Folder.bckFolderPath);

                    File.Move(Folder.tmpFolder + "\\" + file.Name, Folder.bckFolderPath + "\\" + file.Name);

                }
                else
                {
                    bg.ReportProgress(0, Thread.CurrentThread.ManagedThreadId + " :(" + file.Name + ") : Error in Database X___X");

                    MoveToErrFolder(file);

                }                    
            }
        }
    }

提前谢谢你!

2 个答案:

答案 0 :(得分:0)

当执行“DoWork”方法的所有代码时,后台工作程序的“工作”结束。

在此代码中的代码执行另一个将执行asyncronous方面“do work”方法的线程。

所以你可以:

1)删除内部线程,后台工作已经执行    另一个线程尊重第一个。

2)在任务中转换内部线程并等待它。

答案 1 :(得分:0)

我认为方法ProcessParallelThread不会在Parallel.ForEach中启动的所有线程完成之前结束,所以这没问题

...但也许你的其他一些方法如MoveToErrFolder等是异步的,如果是的话,那就是问题。

也 我没有看到您的BackgroundWorker初始化部分,但它是否将WorkerReportsProgress设置为true? - ReportProgress来电需要