简体   繁体   English

虽然我使用的后台工作程序GUI仍挂着…

[英]although I am using a background worker GUI still hanging…

I have a GUI Where i have to import some document , but when i do using background worker, the GUI is hanging , which must not happen since I am using a background worker, why this happens? 我有一个GUI,我必须在其中导入一些文档,但是当我使用后台工作程序时,GUI挂起了,由于我正在使用后台工作程序,这一定不会发生,为什么会这样? kindly find code below.. 请在下面找到代码。

void ImportNotes_ContextMenuStripItem_Click(object sender, EventArgs e)
{
    if (!backgroundWorker_notesImport.IsBusy)
    {
        mainFrm.ProgressBar.Visible = true;
        backgroundWorker_notesImport.RunWorkerAsync();
    }
}

private void backgroundWorker_notesImport_DoWork(object sender, DoWorkEventArgs e)
{
    ImportNotes();
}

private void backgroundWorker_notesImport_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    mainFrm.ProgressBar.Value = e.ProgressPercentage;
}

void ImportNotes() { }

Does the ImportNotes() method just do the whole import in one big step? ImportNotes()方法是否只需一步就可以完成整个导入?

If so then you still aren't giving the UI chance to do anything. 如果是这样,那么您仍然不会给UI做任何事情的机会。 The example from the MSDN shows how it should be used: MSDN中的示例显示了应如何使用它:

    // This event handler is where the time-consuming work is done.
    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        BackgroundWorker worker = sender as BackgroundWorker;

        for (int i = 1; i <= 10; i++)
        {
            if (worker.CancellationPending == true)
            {
                e.Cancel = true;
                break;
            }
            else
            {
                // Perform a time consuming operation and report progress.
                System.Threading.Thread.Sleep(500);
                worker.ReportProgress(i * 10);
            }
        }
    }

You need to have a loop that allows some processing (the Sleep in this case) and a call to ReportProgress . 您需要有一个循环,该循环允许进行一些处理(在本例中为Sleep )并调用ReportProgress

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM