Sök…


Syntax

  • bgWorker.CancellationPending //returns whether the bgWorker was cancelled during its operation

  • bgWorker.IsBusy //returns true if the bgWorker is in the middle of an operation

  • bgWorker.ReportProgress(int x) //Reports a change in progress. Raises the "ProgressChanged" event

  • bgWorker.RunWorkerAsync() //Starts the BackgroundWorker by raising the "DoWork" event

  • bgWorker.CancelAsync() //instructs the BackgroundWorker to stop after the completion of a task.

Anmärkningar

Att genomföra långvariga operationer i UI-tråden kan göra att din ansökan blir svarsfri, och användaren ser ut att den har slutat fungera. Det är att föredra att dessa uppgifter körs på en bakgrundstråd. När det är klart kan användargränssnittet uppdateras.

Att göra ändringar i användargränssnittet under BackgroundWorker-operationen kräver att ändringarna görs på UI-tråden, vanligtvis med hjälp av Control.Invoke- metoden på kontrollen du uppdaterar. Att försumma det kommer att göra att ditt program kastar ett undantag.

BackgroundWorker används vanligtvis bara i Windows Forms-applikationer. I WPF-applikationer används uppgifter för att ladda ner arbete på bakgrundstrådar (eventuellt i kombination med async / invänta ). Marshalling-uppdateringar på UI-tråden görs vanligtvis automatiskt när egenskapen som uppdateras implementerar INotifyPropertyChanged , eller manuellt med hjälp av UI-trådens Dispatcher .

Tilldela evenemangshanterare till en BackgroundWorker

När instansen av BackgroundWorker har deklarerats måste den ges egenskaper och händelseshanterare för de uppgifter den utför.

    /* This is the backgroundworker's "DoWork" event handler. This 
       method is what will contain all the work you 
       wish to have your program perform without blocking the UI. */

    bgWorker.DoWork += bgWorker_DoWork;


    /*This is how the DoWork event method signature looks like:*/
    private void bgWorker_DoWork(object sender, DoWorkEventArgs e)
    {
        // Work to be done here   
        // ...
        // To get a reference to the current Backgroundworker:
        BackgroundWorker worker = sender as BackgroundWorker;
        // The reference to the BackgroundWorker is often used to report progress
        worker.ReportProgress(...);
    }

    /*This is the method that will be run once the BackgroundWorker has completed its tasks */

    bgWorker.RunWorkerCompleted += bgWorker_CompletedWork;

    /*This is how the RunWorkerCompletedEvent event method signature looks like:*/
    private void bgWorker_CompletedWork(object sender, RunWorkerCompletedEventArgs e)
    {
        // Things to be done after the backgroundworker has finished
    }

   /* When you wish to have something occur when a change in progress 
     occurs, (like the completion of a specific task) the "ProgressChanged" 
     event handler is used. Note that ProgressChanged events may be invoked
     by calls to bgWorker.ReportProgress(...) only if bgWorker.WorkerReportsProgress
     is set to true.  */

     bgWorker.ProgressChanged += bgWorker_ProgressChanged;

    /*This is how the ProgressChanged event method signature looks like:*/
    private void bgWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        // Things to be done when a progress change has been reported

        /* The ProgressChangedEventArgs gives access to a percentage,
         allowing for easy reporting of how far along a process is*/
        int progress = e.ProgressPercentage;
    }

Tilldela egenskaper till en BackgroundWorker

Detta gör att bakgrundsverktyget kan avbrytas mellan uppgifterna

bgWorker.WorkerSupportsCancellation = true;

Detta gör det möjligt för arbetaren att rapportera framstegen mellan genomförandet av uppgifterna ...

bgWorker.WorkerReportsProgress = true;

//this must also be used in conjunction with the ProgressChanged event

Skapa en ny BackgroundWorker-instans

En BackgroundWorker används vanligtvis för att utföra uppgifter, ibland tidskrävande, utan att blockera UI-tråden.

// BackgroundWorker is part of the ComponentModel namespace.
using System.ComponentModel;

namespace BGWorkerExample 
{
     public partial class ExampleForm : Form 
     {

      // the following creates an instance of the BackgroundWorker named "bgWorker"
      BackgroundWorker bgWorker = new BackgroundWorker();

      public ExampleForm() { ...

Använda en BackgroundWorker för att slutföra en uppgift.

Följande exempel visar användningen av en BackgroundWorker för att uppdatera en WinForms ProgressBar. BackgroundWorker kommer att uppdatera värdet på framstegsfältet utan att blockera UI-tråden och därmed visa ett reaktivt användargränssnitt medan arbetet görs i bakgrunden.

namespace BgWorkerExample
{
    public partial class Form1 : Form
{

    //a new instance of a backgroundWorker is created.
    BackgroundWorker bgWorker = new BackgroundWorker();
    
    public Form1()
    {
        InitializeComponent();

        prgProgressBar.Step = 1;

        //this assigns event handlers for the backgroundWorker
        bgWorker.DoWork += bgWorker_DoWork;
        bgWorker.RunWorkerCompleted += bgWorker_WorkComplete;

        //tell the backgroundWorker to raise the "DoWork" event, thus starting it.
        //Check to make sure the background worker is not already running.
        if(!bgWorker.IsBusy)
            bgWorker.RunWorkerAsync();
        
    }

    private void bgWorker_DoWork(object sender, DoWorkEventArgs e)
    {
        //this is the method that the backgroundworker will perform on in the background thread.
        /* One thing to note! A try catch is not necessary as any exceptions will terminate the backgroundWorker and report 
          the error to the "RunWorkerCompleted" event */
        CountToY();    
    }

    private void bgWorker_WorkComplete(object sender, RunWorkerCompletedEventArgs e)
    {
        //e.Error will contain any exceptions caught by the backgroundWorker
        if (e.Error != null)
        {
            MessageBox.Show(e.Error.Message);
        }
        else
        {
            MessageBox.Show("Task Complete!");
            prgProgressBar.Value = 0;
        }
    }

    // example method to perform a "long" running task.
    private void CountToY()
    {
        int x = 0;

        int maxProgress = 100;
        prgProgressBar.Maximum = maxProgress;
        

        while (x < maxProgress)
        {
            System.Threading.Thread.Sleep(50);
            Invoke(new Action(() => { prgProgressBar.PerformStep(); }));
            x += 1;
        }
    }


}

Resultatet är följande ...

ange bildbeskrivning här ange bildbeskrivning här



Modified text is an extract of the original Stack Overflow Documentation
Licensierat under CC BY-SA 3.0
Inte anslutet till Stack Overflow