我的后台工作人员完成后,我想更改表单上的标签.我应该在哪里添加此代码?

RJ.*_*RJ. 1 c# backgroundworker

后台工作程序完成其过程后,我想更改表单上某些标签上的文本.

这是触发后台工作者的按钮:

private void btnProcessImages_Click(object sender, EventArgs e)
        {
            DialogResult processImagesWarnMsg = MessageBox.Show("You're about to process images, are you sure?", "Warning!", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2);

            if (processImagesWarnMsg == DialogResult.Yes)
            {
                DisableAllButtons();

                if (!processImagesWorker.IsBusy)
                {
                    processImagesWorker.RunWorkerAsync();
                }
                //The problem here is that the below will run BEFORE the worker is complete. Where should I place the below method in my code? 
                //ResetDirectoryStatistics();
            }
        }
Run Code Online (Sandbox Code Playgroud)

这是更改表单上标签文本的方法:

private void ResetDirectoryStatistics()
        {
            lblSelectedDirectory.Text = "N/A";
            lblTotalNumberOfFilesInDirectory.Text = "N/A";
            lblTotalNumberOfSupportedFilesInDirectory.Text = "N/A";
            lblTotalNumberOfUnsupportedFilesInDirectory.Text = "N/A";
            lblTotalNumberOfPoliciesInDirectory.Text = "N/A";
        }
Run Code Online (Sandbox Code Playgroud)

在处理后台工作程序时,我应该在哪里放置ResetDirectoryStatistics方法?我不能将它放在backgroundworker的"DoWork"方法中,因为那将是跨线程的.如果我将该方法放在processImagesWorker.RunWorkerAsync();之后,它将在RunWorker完成之前自行执行.

Kev*_*sse 6

你应该RunWorkerCompleted在后台工作者的情况下调用你的方法.此事件使用UI线程,因此无需担心跨线程问题.