我正在與c#工作半年前,我正在學習。 我有一個幫助我工作的程序,它在我的工作場所與4種數據庫進行通信。它的工作完美,但現在我想設計使它更酷,但當我想設置一個進度環時,我卡住了,因爲我的方法在與我的數據庫進行通信時凍結UI。 我想建立一個後臺工作人員,我嘗試了所有我發現甚至調用過的東西,但沒有任何工作。如何讓backgroundworker更新UI併爲其設置(城域)進度?
public partial class MainWindow : MetroWindow
{
BackgroundWorker workerThread = null;
public MainWindow()
{
InitializeComponent();
InstantiateWorkerThread();
progressrinG.IsActive = true;
}
private void InstantiateWorkerThread()
{
workerThread = new BackgroundWorker();
workerThread.ProgressChanged += WorkerThread_ProgressChanged;
workerThread.DoWork += WorkerThread_DoWork;
workerThread.RunWorkerCompleted += WorkerThread_RunWorkerCompleted;
workerThread.WorkerReportsProgress = true;
//workerThread.WorkerSupportsCancellation = true;
}
private void WorkerThread_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
}
private void WorkerThread_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
}
private void WorkerThread_DoWork(object sender, DoWorkEventArgs e)
{
//My huge method,asking 4 database that would update UI too
//... Textboxes,Grids,Expanders.
}
private void Button3_Click(object sender, RoutedEventArgs e)
{
workerThread.RunWorkerAsync();
while (workerThread.IsBusy)
{
progressrinG.Visibility = Visibility.Visible;
}
}
}
即使調用,當我按下按鈕,5-10秒後結束我的方法,但在做的,我不看我的progressring應用程序被凍結。
有關我該做什麼錯的任何想法?或者我該怎麼辦?提前致謝!
我試圖找到一個示例現在如何做到這一點,然後我會嘗試編輯我的方法。謝謝。 Upvoted。 – Tamas555