4 Ekim 2017 Çarşamba

BackgroundWorker Sınıfı

Giriş
Şu satırı dahil ederiz.
using System.ComponentModel;
BackGroundWorker anladığım kadarıyla hem WPF hem de WinForms ile kullanılabiliyor. Açıklaması şöyle
The BackgroundWorker class (under System.ComponentModel namespace) uses thread pool too. It abstracts the thread creation and monitoring process when exposing events that report the thread’s process, making this class suitable for GUI responses, for example, reflecting a long-running process that executes in the background. By wrapping the System.Threading.Thread class, it is much easier to interact with the thread.

The BackgroundWorker class exposes two main events: ProgressChanged and RunWorkerCompleted; the first is triggered by calling the ReportProgress method.
Thread yaratmak için kullanılabilecek şeyler şöyle
C# allows the creation of thread pools by calling on of the following:

- ThreadPool.QueueUserWorkItem
- Asynchronous delegate
- BackgroundWorker
- Task Parallel Library (TPL)
Constructor
Şöyle yaparız
private readonly BackgroundWorker bw = new BackgroundWorker();
CancelAsynch metodu
CancellationPending bayrağını kaldırır. Çağrılabilmesi için WorkerSupportsCancellation bayrağının atanmış olması gerekir. Şöyle yaparız
bw.CancelAsync ();
DoWork Alanı
Açıklaması şöyle
DoWork should run on a different thread than the created instance of the BackgroundWorker.
DoWork için lambda kullanırsak şöyle yaparız
bw.DoWork += (s, e) => {...};
Sınıfımın metodunu kullanmak istersek şöyle yaparız.
bw.DoWork += backgroundWorkerX_DoWork;
Metodu tanımlamak için şöyle yaparız.
public void backgroundWorkerX_DoWork(object sender, DoWorkEventArgs e)
{
  ...
}
Örnek
İlerlemeyi göstermek için şöyle yaparız.
private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
  BackgroundWorker worker = sender as BackgroundWorker;

  int progress =;

  while (some_condition)
  {
    ...
    worker.ReportProgress(progress++);
  }
}
ProgressChanged Alanı
Açıklaması şöyle
ProgressChanged event handler executes on the thread that created the BackgroundWorker.
Nesne yaratılırken şöyle yaparız
worker.WorkerReportsProgress = true;
worker.ProgressChanged += worker_ProgressChanged;
İlerleme şöyle gösterilir
private void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
  Console.Write("Progress: " + e.ProgressPercentage);
}
RunWorkerAsync metodu
Şöyle yaparız.
bw.RunWorkerAsync();
RunWorkerCompleted Alanı
Şöyle yaparız.
bw.RunWorkerCompleted += worker_RunWorkerCompleted;
Metodu tanımlamak için şöyle yaparız.
private void worker_RunWorkerCompleted(object sender, 
                                       RunWorkerCompletedEventArgs e)
{
  ...
}
WorkerReportsProgress Alanı
Şöyle yaparız
worker.WorkerReportsProgress = true;
WorkerSupportsCancellation Alanı
Nesne yaratılırken şöyle yaratırız.
worker.WorkerSupportsCancellation = true;
DoWork içinde bayrak kontrol edilir.
private void worker_DoWork(object sender, DoWorkEventArgs e)
{
  BackgroundWorker worker = sender as BackgroundWorker;

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

Hiç yorum yok:

Yorum Gönder