多个Backgroundworker线程在单个Backgroundworker线程中

时间:2016-03-29 06:26:06

标签: multithreading backgroundworker restore

我在这里遇到多线程问题。尝试使用backgroundworker和线程。

目标:从组合框中选择一个项目,然后单击一个按钮以触发多个备份文件还原到MSSQL Server。目的是弹出与要恢复的备份文件一样多的弹出窗口。主窗口启动后台工作程序,显示整体进度,而子线程导致非模态子弹出窗口表示每个恢复过程的进度。如果串行运行(没有线程/后台工作),这可以正常工作。我的目的是运行一堆并行弹出窗口,以便恢复更快。

问题陈述:我的目的是运行一堆并行弹出窗口,以便恢复更快,而不是串行运行。在尝试调试时,它是一个混乱的断点显示Visual Studio显示(可能代表并行的多个线程)。但最终目标尚未实现。在这方面有人可以帮助我吗?

- 代码摘录

以下是我已经完成的代码提取,它以串行方式工作。但是,只要我将代码放入多线程,就没有任何作用。仅显示弹出窗口,但不会进行任何处理。

这是按钮点击事件,它启动整个过程

private void btnSnapshot_Click(object sender, EventArgs e)
        {
            this.SetPanelEnabledProperty(false); // Disable All Controls on the main window

            // Select the text against which the DATABASE Backup Files are to be picked
            // Start the main background worker process, which in turn will trigger few other child threads
            BGWrk.RunWorkerAsync(2000);

            BGWrk.DoWork += new DoWorkEventHandler(BGWrk_DoWork);
            BGWrk.ProgressChanged += new ProgressChangedEventHandler(BGWrk_ProgressChanged);
            BGWrk.RunWorkerCompleted += new RunWorkerCompletedEventHandler
                                (BGWrk_RunWorkerCompleted);
            BackgroundWorker helperBW = sender as BackgroundWorker;

            BGWrk.WorkerReportsProgress = true;
            BGWrk.WorkerSupportsCancellation = true;

        }

        private void BGWrk_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker helperBW = sender as BackgroundWorker;
            int arg = (int)e.Argument;
            //Call the method that invokes the popup window instances in a loop, for each file found for the SnapShotName selected in the combobox
            ParallelRestoreSnapshot();

            if (helperBW.CancellationPending)
            {
                e.Cancel = true;
            }

        }

        // This method will create backgroundworker instances and in turn the Do_Work events of those backgroundworkers will call the popup against each backup file.
        private bool ParallelRestoreSnapshot()
        {
            FileOperations FIO = new FileOperations();

            if (SelectedSnapShot != null && SelectedSnapShot != "None")
            {
                string[] sBakFiles;
                sBakFiles = FIO.GetListOfBackupFiles(sEntireBackupFilePath);

                try
                {
                    progressPopupsList = new List<FrmProgressPopup>();

                    for (int aIndex = 0; aIndex < sBakFiles.Length; aIndex++)
                    {
                        BackgroundWorker bgPopups = new BackgroundWorker();

                        BAKFileName = sBakFiles[aIndex];

                        bgPopups.RunWorkerAsync(2000);
                        bgPopups.DoWork += new DoWorkEventHandler(bgPopups_DoWork);
                        bgPopups.ProgressChanged += new ProgressChangedEventHandler(bgPopups_ProgressChanged);
                        bgPopups.RunWorkerCompleted += new RunWorkerCompletedEventHandler
                                            (bgPopups_RunWorkerCompleted);

                        bgPopups.WorkerReportsProgress = true;
                        bgPopups.WorkerSupportsCancellation = true;
                    }
                    retVal = true;
                }
                catch (Exception exc)
                {
                    MessageBox.Show("Error while Restoring: " + exc.Message, "Exception encountered", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    //goto NextDB;
                    return false;
                }
            }
            FIO = null;
            return retVal;
        }

// This DoWork event calls a method GeneratePopupInstances which makes copies of a window, which is shown as non-modal

         private void bgPopups_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker helperBW = sender as BackgroundWorker;
            int arg = (int)e.Argument;
            //BackgroundProcessLogicMethod(helperBW, arg);
            GeneratePopupInstances();
            if (helperBW.CancellationPending)
            {
                e.Cancel = true;
            }

        }

0 个答案:

没有答案