1

我正在寻找后台工作人员和进度条的实现。我能找到的只是使用Threading.Sleep(). 示例都在工作,但如果将模拟更改为实际的 SQL 查询,则它不起作用。

我应该在下面的代码中在哪里插入查询,请帮忙。.NET-2.0

void m_oWorker_DoWork(object sender, DoWorkEventArgs e)
    {
        // The sender is the BackgroundWorker object we need it to
        // report progress and check for cancellation.
        //NOTE : Never play with the UI thread here...
        for (int i = 0; i < 100; i++)
        {
            Thread.Sleep(100);

            // Periodically report progress to the main thread so that it can
            // update the UI.  In most cases you'll just need to send an
            // integer that will update a ProgressBar                    
            m_oWorker.ReportProgress(i);
            // Periodically check if a cancellation request is pending.
            // If the user clicks cancel the line
            // m_AsyncWorker.CancelAsync(); if ran above.  This
            // sets the CancellationPending to true.
            // You must check this flag in here and react to it.
            // We react to it by setting e.Cancel to true and leaving
            if (m_oWorker.CancellationPending)
            {
                // Set the e.Cancel flag so that the WorkerCompleted event
                // knows that the process was cancelled.
                e.Cancel = true;
                m_oWorker.ReportProgress(0);
                return;
            }
        }

        //Report 100% completion on operation completed
        m_oWorker.ReportProgress(100);
    }
4

2 回答 2

1

通过“查询”,听起来您只需要做一项操作。这使得进度条变得棘手,因为没有办法真正衡量 SQL 查询的进度。它不能告诉你它会持续多久。您可能只想在执行查询时使用非承诺的无限滚动繁忙指示器。

于 2013-06-12T07:18:25.230 回答
0

您应该在检查CancellationPending.

于 2013-06-12T06:12:53.017 回答