當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。