ArcGIS Explorer Component Help |
BackgroundWorker..::.DoWork Event |
BackgroundWorker Class Example See Also |
Assembly: ESRI.ArcGISExplorer (in ESRI.ArcGISExplorer.dll) Version: 2.0.0.1500 (2.0.0.1500)
Syntax
C# |
---|
public event DoWorkEventHandler DoWork |
Visual Basic (Declaration) |
---|
Public Event DoWork As DoWorkEventHandler |
Remarks
This event occurs on a worker thread.
This event occurs when the RunWorkerAsync method is called on this BackgroundWorker, providing on opportunity to perform work on a worker thread.
You must be careful not to manipulate any UI thread objects in your DoWork event handler - you can pass information between the UI and worker threads through the arguments of the DoWork, ProgressChanged, and RunWorkerCompleted event handlers.
If an exception is thrown within the DoWork event handler and is not caught within that scope, then in addition to the event handler terminating, the Error will be set with the exception. Any exceptions caught within the DoWork event handler will not be set into this property and therefore should be handled appropriately or re-thrown in order to respond to the exception in the UI if required.
Examples
using System; using System.Collections.Generic; using System.Text; using ESRI.ArcGISExplorer.Geometry; using ESRI.ArcGISExplorer.Mapping; namespace ThreadingExample { public class SimpleButton : ESRI.ArcGISExplorer.Application.Button { // References to the MapDisplay and BackgroundWorker are created, // and therefore only used, on the UI thread. MapDisplay _md; ESRI.ArcGISExplorer.Threading.BackgroundWorker _bgWorker; // Constructor is called on the UI thread. public SimpleButton() { _md = ESRI.ArcGISExplorer.Application.Application.ActiveMapDisplay; // Create an Explorer BackgroundWorker component and hook // up the DoWork event handler. _bgWorker = new ESRI.ArcGISExplorer.Threading.BackgroundWorker(); _bgWorker.DoWork += new ESRI.ArcGISExplorer.Threading.DoWorkEventHandler(DoWork); } // OnClick is called on the UI thread. public override void OnClick() { // Access the active display and track a Point. Point ptOnUIThread = _md.TrackPoint(); if (!_bgWorker.IsBusy) { // Trigger the DoWork event handler. _bgWorker.RunWorkerAsync(ptOnUIThread.ToXmlString()); } } // DoWork is called on a worker thread. private void DoWork(object sender, ESRI.ArcGISExplorer.Threading.DoWorkEventArgs args) { Point ptOnBgThread = Point.CreateFromXmlString((string)args.Argument); // Perform some work on the background thread, such as // saving the clicked locations to a file. string filename = System.IO.Path.GetTempFileName(); System.IO.StreamWriter sw = System.IO.File.CreateText(filename); sw.Write(ptOnBgThread.ToString()); sw.Close(); } } }
Imports System Imports System.Collections.Generic Imports System.Text Imports ESRI.ArcGISExplorer.Geometry Imports ESRI.ArcGISExplorer.Mapping Public Class SimpleButton Inherits ESRI.ArcGISExplorer.Application.Button ' References to the MapDisplay and BackgroundWorker are created, ' and therefore only used, on the UI thread. Private _md As MapDisplay Private _bgWorker As ESRI.ArcGISExplorer.Threading.BackgroundWorker ' Constructor is called on the UI thread. Public Sub New() _md = ESRI.ArcGISExplorer.Application.Application.ActiveMapDisplay ' Create an Explorer BackgroundWorker component and hook ' up the DoWork event handler. _bgWorker = New ESRI.ArcGISExplorer.Threading.BackgroundWorker() AddHandler _bgWorker.DoWork, AddressOf DoWork End Sub ' OnClick is called on the UI thread. Public Overrides Sub OnClick() ' Access the active display and track a Point. Dim ptOnUIThread As ESRI.ArcGISExplorer.Geometry.Point = _md.TrackPoint() If (Not _bgWorker.IsBusy) Then ' Trigger the DoWork event handler. _bgWorker.RunWorkerAsync(ptOnUIThread.ToXmlString()) End If End Sub ' DoWork is called on a worker thread. Private Sub DoWork(ByVal sender As Object, ByVal args As ESRI.ArcGISExplorer.Threading.DoWorkEventArgs) Dim ptOnBgThread As ESRI.ArcGISExplorer.Geometry.Point = ESRI.ArcGISExplorer.Geometry.Point.CreateFromXmlString(CStr(args.Argument)) ' Perform some work on the background thread, such as ' saving the clicked locations to a file. Dim filename As String = System.IO.Path.GetTempFileName() Dim sw As System.IO.StreamWriter = System.IO.File.CreateText(filename) sw.Write(ptOnBgThread.ToString()) sw.Close() End Sub End Class