簡體   English   中英

BackgroundWorker中的SQL查詢

[英]SQL Query inside BackgroundWorker

我正在尋找后台工作人員和進度條的實現。 我所能找到的只是使用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);
    }

通過“查詢”,聽起來您只需要執行一項操作。 這使進度條變得棘手,因為無法真正衡量SQL查詢的進度。 它無法告訴您它將持續多長時間。 您可能只想在執行查詢時使用非承諾無限滾動忙碌指示器。

在檢查CancellationPending之后,您應該執行任何sql查詢,這是此后台工作程序的實際任務。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM