本文整理汇总了C#中BackgroundWorker类的典型用法代码示例。如果您正苦于以下问题:C# BackgroundWorker类的具体用法?C# BackgroundWorker怎么用?C# BackgroundWorker使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
BackgroundWorker类属于命名空间,在下文中一共展示了BackgroundWorker类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: bgworkercounter
public void bgworkercounter()
{
var backgroundWorker1 = new BackgroundWorker();
backgroundWorker1.DoWork += backgroundWorker1_DoWork;
backgroundWorker1.RunWorkerCompleted += backgroundWorker1_RunWorkerCompleted;
backgroundWorker1.RunWorkerAsync();
}
示例2: OnTouched
private void OnTouched()
{
if (_backgroundWorker != null) _backgroundWorker.CancelAsync();
_backgroundWorker = new BackgroundWorker();
if (string.IsNullOrEmpty(Thread.CurrentThread.Name)) Thread.CurrentThread.Name = "Main";
Debug.Log("START " + Thread.CurrentThread.Name);
Plane.InfoStr += "\nS("+Thread.CurrentThread.Name+");";
_backgroundWorker.DoWork += (o, a) =>
{
Debug.Log("INSIDE1 " + Thread.CurrentThread.Name);
Plane.InfoStr += "IN1("+Thread.CurrentThread.Name+");"+a.Argument+";";
for (var i = 0; i < 10000000; ++i)
{
if (a.IsCanceled) return;
var n = 67876 + i / 100f;
var x = Mathf.Pow(n, 3);
}
Debug.Log("INSIDE2 " + Thread.CurrentThread.Name);
Plane.InfoStr += "IN2("+Thread.CurrentThread.Name+");";
a.Result = a.Argument+"!";
};
_backgroundWorker.RunWorkerCompleted += (o, a) =>
{
Debug.Log("END " + Thread.CurrentThread.Name);
Plane.InfoStr += "E("+Thread.CurrentThread.Name+");"+a.Result+";";
};
_backgroundWorker.RunWorkerAsync("A1");
}
示例3: TestCallDoWorkOnBackgroundThreadAndThenCallOnSuccessOnMainThread
public void TestCallDoWorkOnBackgroundThreadAndThenCallOnSuccessOnMainThread()
{
var strategy = new MockBackgroundWorkerStrategy();
var operation = new BackgroundWorker<object>(strategy);
operation.Execute();
strategy.CheckThatOnDoWorkWasCalled();
}
示例4: Initialize
/// <summary>
/// Initialize the manager, should be called in OnNavigatedTo of main page.
/// </summary>
public void Initialize()
{
socket = new StreamSocket();
dataReadWorker = new BackgroundWorker();
dataReadWorker.WorkerSupportsCancellation = true;
dataReadWorker.DoWork += new DoWorkEventHandler(ReceiveMessages);
}
示例5: Start
// Use this for initialization
void Start()
{
// init a background worker thread
BackgroundWorker worker = new BackgroundWorker();
worker.DoWork += HandleWorkerDoWork;
worker.RunWorkerAsync();
}
示例6: NativeWorkbench
public NativeWorkbench()
{
try
{
// KeyDown += OnKeyDown;
Tick += OnTick;
}
catch (Exception e)
{
SaveLoad.Log(e.ToString());
// Debug.WriteLine(e);
}
BackgroundWorker myWorker = new BackgroundWorker();
myWorker.DoWork += (sender, e) =>
{
try
{
Application.EnableVisualStyles();
Application.Run(_nativeWorkbenchForm);
}
catch (Exception ex)
{
SaveLoad.Log(ex.ToString());
}
};
myWorker.RunWorkerAsync();
}
示例7: HidDevice
internal HidDevice(string devicePath)
{
this.devicePath = devicePath;
try
{
var hidHandle = OpenDeviceIO(devicePath, NativeMethods.ACCESS_NONE);
deviceAttributes = GetDeviceAttributes(hidHandle);
deviceCapabilities = GetDeviceCapabilities(hidHandle);
CloseDeviceIO(hidHandle);
}
catch (Exception exception)
{
throw new Exception(string.Format("Error querying HID device '{0}'.", devicePath), exception);
}
deviceEventMonitor = new HidDeviceEventMonitor(this);
deviceEventMonitor.Inserted += DeviceEventMonitorInserted;
deviceEventMonitor.Removed += DeviceEventMonitorRemoved;
readWorker = new BackgroundWorker<HidDeviceData>();
readWorker.Updated += new EventHandler<EventArgs<HidDeviceData>>(readWorker_Updated);
readWorker.DoWork += new System.ComponentModel.DoWorkEventHandler(readWorker_DoWork);
}
示例8: Main
static void Main(string[] args)
{
_tweetQueue = new Queue<string>();
_worker = new BackgroundWorker();
_worker.DoWork += new DoWorkEventHandler(DBWrite);
DateTime executionStart = DateTime.Now;
while (true)
{
Console.Write("-");
try
{
executionStart = DateTime.Now;
ConsumeStream();
}
catch (Exception e)
{
Output.Print(_name, "Exception from Run:" + Environment.NewLine + e);
while (_worker.IsBusy)
{
Console.WriteLine("Waiting for worker to finish");
Thread.Sleep(1000);
}
if ((DateTime.Now - executionStart).TotalSeconds < 5)
{
Output.Print(_name, "Previous failure was quick. Waiting 60 seconds.");
Thread.Sleep(59000);
}
}
Console.WriteLine(".");
Thread.Sleep(1000);
}
}
示例9: TestCancelAsync
public void TestCancelAsync()
{
BackgroundWorker bw = new BackgroundWorker();
bw.DoWork += DoWorkExpectCancel;
bw.WorkerSupportsCancellation = true;
manualResetEvent3 = new ManualResetEventSlim(false);
bw.RunWorkerAsync("Message");
bw.CancelAsync();
bool ret = manualResetEvent3.Wait(TimeoutLong);
Assert.True(ret);
// there could be race condition between worker thread cancellation and completion which will set the CancellationPending to false
// if it is completed already, we don't check cancellation
if (bw.IsBusy) // not complete
{
if (!bw.CancellationPending)
{
for (int i = 0; i < 1000; i++)
{
Wait(TimeoutShort);
if (bw.CancellationPending)
{
break;
}
}
}
// Check again
if (bw.IsBusy)
Assert.True(bw.CancellationPending, "Cancellation in Main thread");
}
}
示例10: BackgroundWorker_CancelAsync_ReportsCancelledWhenWorkerSupportsCancellationIsTrue
public void BackgroundWorker_CancelAsync_ReportsCancelledWhenWorkerSupportsCancellationIsTrue()
{
UnitTestContext context = GetContext();
int numTimesProgressCalled = 0;
BackgroundWorker target = new BackgroundWorker();
target.DoWork += (o, e) =>
{
// report progress changed 10 times
for (int i = 1; i < 11; i++)
{
Thread.Sleep(100);
if (target.CancellationPending)
{
e.Cancel = true;
return;
}
}
};
target.WorkerSupportsCancellation = true;
target.RunWorkerCompleted += (o, e) =>
{
// target does not support ReportProgress we shold get a System.InvalidOperationException from DoWork
context.Assert.IsNull(e.Error);
context.Assert.IsTrue(e.Cancelled);
context.Assert.Success();
};
target.RunWorkerAsync(null);
target.CancelAsync();
context.Complete();
}
示例11: Main
// private static SimpleCompileForm _simpleCompileForm;
private static void Main(string[] args)
{
var worker = new BackgroundWorker();
worker.DoWork += (sender, e) =>
{
try
{
_nativeWorkbenchForm = new NativeWorkbenchForm();
_timer = new System.Windows.Forms.Timer();
_timer.Stop();
_timer.Interval = 100;
_timer.Tick += _timer_Tick;
_timer.Start();
}
catch (Exception ex)
{
Debug.WriteLine(ex);
}
Application.EnableVisualStyles();
Application.Run(_nativeWorkbenchForm);
};
worker.RunWorkerAsync();
Thread.Sleep(-1);
}
示例12: HidDeviceEventMonitor
public HidDeviceEventMonitor(HidDevice device)
{
this.device = device;
monitor = new BackgroundWorker<bool>();
monitor.Updated += new EventHandler<EventArgs<bool>>(monitor_Updated);
monitor.DoWork += new System.ComponentModel.DoWorkEventHandler(monitor_DoWork);
}
示例13: DefaultValues
public void DefaultValues()
{
var backgroundWorker = new BackgroundWorker<object, object, object>();
Assert.IsTrue(backgroundWorker.ThrowExceptionOnCompleted);
Assert.IsFalse(backgroundWorker.IsBusy);
Assert.IsFalse(backgroundWorker.WorkerReportsProgress);
Assert.IsFalse(backgroundWorker.WorkerSupportsCancellation);
}
示例14: StartBackgroundInitialization
void StartBackgroundInitialization()
{
Log.Info("PictureOfTheDay: Init!");
pluginBackgroundWorker = new BackgroundWorker();
pluginBackgroundWorker.WorkerReportsProgress = true;
pluginBackgroundWorker.WorkerSupportsCancellation = false;
pluginBackgroundWorker.DoWork += DoWork;
pluginBackgroundWorker.RunWorkerAsync();
}
示例15: Worker
public Worker(string[] arguments)
{
this.worker = new BackgroundWorker();
worker.WorkerSupportsCancellation = true;
worker.WorkerReportsProgress = true;
worker.DoWork += new DoWorkEventHandler(worker_DoWork);
worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
worker.RunWorkerAsync(arguments);
}