当前位置: 首页>>代码示例>>C#>>正文


C# BackgroundWorker类代码示例

本文整理汇总了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();
 }
开发者ID:Nazgard,项目名称:t2_1stan,代码行数:7,代码来源:ArchiveControl.cs

示例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");
    }
开发者ID:karthiksaligrama,项目名称:background-worker-unity3d,代码行数:33,代码来源:Plane.cs

示例3: TestCallDoWorkOnBackgroundThreadAndThenCallOnSuccessOnMainThread

 public void TestCallDoWorkOnBackgroundThreadAndThenCallOnSuccessOnMainThread()
 {
     var strategy = new MockBackgroundWorkerStrategy();
     var operation = new BackgroundWorker<object>(strategy);
     operation.Execute();
     strategy.CheckThatOnDoWorkWasCalled();
 }
开发者ID:d20021,项目名称:LiveSDK-for-Windows,代码行数:7,代码来源:BackgroundWorkerTest.cs

示例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);
 }
开发者ID:peatear,项目名称:equilibrium,代码行数:10,代码来源:btConManager.cs

示例5: Start

 // Use this for initialization
 void Start()
 {
     // init a background worker thread
     BackgroundWorker worker = new BackgroundWorker();
     worker.DoWork += HandleWorkerDoWork;
     worker.RunWorkerAsync();
 }
开发者ID:derFunk,项目名称:unity3d-webplayer-jslogger,代码行数:8,代码来源:JsLoggerThreadExample.cs

示例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();
    }
开发者ID:isti37,项目名称:NativeWorkbench,代码行数:29,代码来源:NativeWorkbenchScript.cs

示例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);
        }
开发者ID:ClimbTheWorld,项目名称:HidLibrary,代码行数:26,代码来源:HidDevice.cs

示例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);
            }

        }
开发者ID:malimu,项目名称:CrisisTracker,代码行数:34,代码来源:Program.cs

示例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");
            }
        }
开发者ID:noahfalk,项目名称:corefx,代码行数:34,代码来源:BackgroundWorkerTests.cs

示例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();
        }
开发者ID:BiYiTuan,项目名称:csla,代码行数:32,代码来源:BackgroundWorkerTests.cs

示例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);
    }
开发者ID:isti37,项目名称:NativeWorkbench,代码行数:28,代码来源:DebugFormLauncher.cs

示例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);
        }
开发者ID:jj-jabb,项目名称:HidLibrary,代码行数:8,代码来源:HidDeviceEventMonitor.cs

示例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);
 }
开发者ID:GTuritto,项目名称:ngenerics,代码行数:8,代码来源:BackgroundWorkerTest.cs

示例14: StartBackgroundInitialization

 void StartBackgroundInitialization()
 {
     Log.Info("PictureOfTheDay: Init!");
     pluginBackgroundWorker = new BackgroundWorker();
     pluginBackgroundWorker.WorkerReportsProgress = true;
     pluginBackgroundWorker.WorkerSupportsCancellation = false;
     pluginBackgroundWorker.DoWork += DoWork;
     pluginBackgroundWorker.RunWorkerAsync();
 }
开发者ID:j-b-n,项目名称:MPDomoticz,代码行数:9,代码来源:BackgroundWorker.cs

示例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);
 }
开发者ID:eatonmi,项目名称:Crawler,代码行数:9,代码来源:Program.cs


注:本文中的BackgroundWorker类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。