Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Have a look at <a href="https://stackoverflow.com/questions/1862590/how-to-update-gui-with-backgroundworker">this</a> question...</p> <p>cheers,</p> <p>EDIT:</p> <p>Joe - not sure if you're getting any closer to groking this, so I thought I'd try to put together a simple usage of BackgroundWorker to demonstrate how simple and powerful this class is!</p> <p>first - in your constructor...</p> <pre><code>public MainWindow() { InitializeComponent(); BackgroundWork worker = new BackgroundWorker(); worker = new BackgroundWorker(); worker.DoWork += new DoWorkEventHandler(worker_DoWork); worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted); System.Timers.Timer t = new System.Timers.Timer(10000); // 10 second intervals t.Elapsed += (sender, e) =&gt; { // Don't try to start the work if it's still busy with the previous run... if (!worker.IsBusy) worker.RunWorkerAsync(); }; } } </code></pre> <p>so we have set up something to delegate some work (in the method 'worker_DoWork') on a background thread... whatever happends in that method will not impact the UI thread, and it should look something like:</p> <pre><code>private void worker_DoWork(object sender, DoWorkEventArgs e) { // Whatever comes back from the lengthy process, we can put into e.Result e.Result = DoMyBigOperation(); } </code></pre> <p>Now when this thread completes, it will fire the RunWorkerCompleted event, which we have handled as such:</p> <pre><code>private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { // First, handle the case where an exception was thrown. if (e.Error != null) { // handle the System.Exception MessageBox.Show(e.Error.Message); } else if (e.Cancelled) { // now handle the case where the operation was cancelled... lblCase.Content = "The operation was cancelled"; } else { // Finally, handle the case where the operation succeeded lblCase.Content = e.Result.ToString(); } } </code></pre> <p>Hope this helps! IanR</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload