public WaitHandle RunAsync() { if (isRunning) { throw new InvalidOperationException("Import is already running"); } try { /* must be set (as soon as possible) in a function that is _not_ called asynchronously * (i.e. dont call it in ImportThread()) */ isRunning = true; importSucceeded = false; cancellationRequested = false; asyncOperation = AsyncOperationManager.CreateOperation(null); Reset(); /* invoke the import function on a new thread and return a waithandle */ Action <string, VolumeDatabase, string, int> it = ImportThread; IAsyncResult ar = it.BeginInvoke(sourceDbPath, targetDb, dbDataPath, bufferSize, null, null); return(ar.AsyncWaitHandle); } catch (Exception) { isRunning = false; if (asyncOperation != null) { asyncOperation.OperationCompleted(); } throw; } }
//private void InitThems() //{ // AsyncOperation operation = AsyncOperationManager.CreateOperation(null); // AutoResetEvent autoResetEvent = new AutoResetEvent(false); // _autoResetEvents.Add(autoResetEvent); // ThreadPool.QueueUserWorkItem(o => // { // AsyncOperation asyncOperation = o as AsyncOperation; // try // { // ResourceDictionary defaultDictionary = new ResourceDictionary() // { // Source = new Uri("/GuoKun.Themes;component/Themes/Default.xaml", UriKind.RelativeOrAbsolute), // }; // _application.Resources.MergedDictionaries.Add(defaultDictionary); // } // catch(Exception e) // { // _log.Fatal("初始化系统样式异常:", e); // asyncOperation.Post // ( // x => // { // MessageBox.Show("初始化系统样式异常,系统将退出!"); // Environment.Exit(-1); // }, // null // ); // } // asyncOperation.OperationCompleted(); // autoResetEvent.Set(); // }, operation); //} private void InitializeDatabase() { AsyncOperation operation = AsyncOperationManager.CreateOperation(null); AutoResetEvent autoResetEvent = new AutoResetEvent(false); _autoResetEvents.Add(autoResetEvent); ThreadPool.QueueUserWorkItem(o => { AsyncOperation asyncOperation = o as AsyncOperation; try { string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, _configuration.DbConfigFile); IConfigurationSource configurationSource = new FileConfigurationSource(path); DatabaseFactory.SetDatabaseProviderFactory(new DatabaseProviderFactory(configurationSource)); DatabaseFactory.CreateDatabase().CreateConnection().Open(); } catch (Exception e) { _log.Fatal("初始化数据库异常:", e); asyncOperation.Post ( x => { MessageBox.Show("初始化数据库异常,系统将退出!"); Environment.Exit(-1); }, null ); } asyncOperation.OperationCompleted(); autoResetEvent.Set(); }, operation); }
private void EntryDisposed(object sender, TransitionEventArgs <StateID, EventID, EventArgs> e) { scheduler.Dispose(); operation.OperationCompleted(); isDisposed = true; }
/// <summary> /// Clears the work queue with the given priority. /// </summary> /// <param name="priority">A value between 0 and <see cref="PriorityQueues"/> /// indicating the priority queue to cancel.</param> private void ClearWorkQueue(int priority) { while (items[priority].Count > 0) { AsyncOperation asyncOp = items[priority].First.Value; asyncOp.OperationCompleted(); items[priority].RemoveFirst(); } }
private void OnConversionCompleted(ConversionCompletedEventArgs args) { if (_async != null) { _async.Post(x => ConversionCompleted(this, args), null); _async.OperationCompleted(); _async = null; } }
private void AsynOperationCompleted(object args) { lock (statusChangeLockObject) { isWorking = false; if (!enabled) { isRunning = false; try { mainThreadOperation.OperationCompleted(); } catch { } } } OnRunWorkerCompleted((RunWorkerCompletedEventArgs)args); workThreadOperation = AsyncOperationManager.CreateOperation(null); workThreadOperation.PostOperationCompleted(completedCallback, null); }
private void Completed() { // This is in a helper method to ensure the JIT doesn't artifically extend the lifetime of the operation. var tracker = new OperationCompletedTracker(); AsyncOperationManager.SynchronizationContext = tracker; AsyncOperation operation = AsyncOperationManager.CreateOperation(new object()); Assert.False(tracker.OperationDidComplete); operation.OperationCompleted(); Assert.True(tracker.OperationDidComplete); }
/// <summary> /// Clean up any resources being used. /// </summary> /// <param name="disposing"> /// true if managed resources should be disposed; otherwise, false. /// </param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { _contextMenuGenerator.Dispose(); StyleConfig.StyleChange -= OnStyleChanged; _msgViewer.Dispose(); _asyncOperation.OperationCompleted(); components.Dispose(); } base.Dispose(disposing); }
/// <summary> /// Clears the work queue with the given priority. /// This method must be called from inside a lock. /// </summary> /// <param name="priority">A value between 0 and <see cref="PriorityQueues"/> /// indicating the priority queue to cancel.</param> private void ClearWorkQueue(int priority) { AsyncOperation singleOp = singleItems[priority]; if (singleOp != null) { singleOp.OperationCompleted(); singleItems[priority] = null; } while (items[priority].Count > 0) { AsyncOperation asyncOp = items[priority].First.Value; asyncOp.OperationCompleted(); items[priority].RemoveFirst(); } }
public WaitHandle RunAsync() { if (isRunning) { throw new InvalidOperationException("Scanner is already running"); } if (scanSucceeded) { throw new InvalidOperationException("Scanning has been completed successfully. Create a new scanner to scan another volume"); } try { /* must be set (as soon as possible) in a function that is _not_ called asynchronously * (i.e. dont call it in ScanningThread()) */ isRunning = true; cancellationRequested = false; asyncOperation = AsyncOperationManager.CreateOperation(null); Reset(); BufferedVolumeItemWriter writer = null; if (this.HasDB) { writer = new BufferedVolumeItemWriter(database, true, Options.BufferSize); } /* invoke the scanning function on a new thread and return a waithandle */ Action <PlatformIO.DriveInfo, TVolume, BufferedVolumeItemWriter> st = ScanningThread; IAsyncResult ar = st.BeginInvoke(drive, volume, writer, null, null); return(ar.AsyncWaitHandle); } catch (Exception) { isRunning = false; if (asyncOperation != null) { asyncOperation.OperationCompleted(); } throw; } }
/// <summary> /// Adds the operation to the work queue. /// This method must be called from inside a lock. /// </summary> /// <param name="argument">The argument of an asynchronous operation.</param> /// <param name="priority">A value between 0 and <see cref="PriorityQueues"/> indicating the priority of this item. /// An item with a higher priority will be processed before items with lower priority.</param> /// <param name="single">true to run this operation without waiting for queued items; otherwise /// false to add this operatino to th queue.</param> private void AddWork(object argument, int priority, bool single) { // Create an async operation for this work item AsyncOperation asyncOp = AsyncOperationManager.CreateOperation(argument); if (single) { AsyncOperation currentOp = singleItems[priority]; currentOp?.OperationCompleted(); singleItems[priority] = asyncOp; } else if (processingMode == ProcessingMode.FIFO) { items[priority].AddLast(asyncOp); } else { items[priority].AddFirst(asyncOp); } }
protected override void Dispose(bool disposing) { if (disposing) { _webBrowser.ProgressChanged -= WebBrowserProgressChanged; _webBrowser.Navigated -= WebBrowserNavigated; _webBrowser.StatusTextChanged -= WebBrowserStatusTextChanged; _webBrowser.DocumentTitleChanged -= WebBrowserDocumentTitleChanged; _webBrowser.CanGoBackChanged -= WebBrowserCanGoBackChanged; _webBrowser.CanGoForwardChanged -= WebBrowserCanGoForwardChanged; _menuGenerator.Dispose(); _toolbarGenerator.Dispose(); _asyncOp.OperationCompleted(); if (components != null) { components.Dispose(); } } base.Dispose(disposing); }
/// <summary> /// Used by the worker thread to process items. /// </summary> private void Run() { while (!Stopping) { lock (lockObject) { // Wait until we have pending work items if (paused || IsWorkQueueEmpty()) { Monitor.Wait(lockObject); } } // Loop until we exhaust the queue bool queueFull = true; while (queueFull && !Stopping && !Paused) { // Get an item from the queue AsyncOperation asyncOp = null; object request = null; int priority = 0; lock (lockObject) { // Check queues Utility.Tuple <AsyncOperation, int> work = GetWork(); asyncOp = work.Item1; priority = work.Item2; if (asyncOp != null) { request = asyncOp.UserSuppliedState; } // Check if the item was removed if (request != null && cancelledItems.ContainsKey(request)) { request = null; } } if (request != null) { Exception error = null; // Start the work QueuedWorkerDoWorkEventArgs doWorkArg = new QueuedWorkerDoWorkEventArgs(request, priority); try { // Raise the do work event OnDoWork(doWorkArg); } catch (Exception e) { error = e; } // Raise the work complete event QueuedWorkerCompletedEventArgs workCompletedArg = new QueuedWorkerCompletedEventArgs(request, doWorkArg.Result, priority, error, doWorkArg.Cancel); if (!Stopping) { asyncOp.PostOperationCompleted(workCompletedCallback, workCompletedArg); } } else if (asyncOp != null) { asyncOp.OperationCompleted(); } // Check if the cache is exhausted lock (lockObject) { queueFull = !IsWorkQueueEmpty(); } } } }
public void Close() { _operation?.OperationCompleted(); EngineClose(); }
private void method_9() { // This item is obfuscated and can not be translated. object obj2; Label_0001: if (this.method_7()) { return; } lock ((obj2 = this.object_0)) { if (this.method_0()) { Monitor.Wait(this.object_0); } } bool flag2 = true; while (!flag2) { if (0 == 0) { goto Label_0001; } AsyncOperation operation = null; object key = null; int priority = 0; lock ((obj2 = this.object_0)) { Class7 <AsyncOperation, int> class2 = this.method_2(); operation = class2.method_0(); priority = class2.method_1(); if (operation != null) { key = operation.UserSuppliedState; } if ((key != null) && this.dictionary_0.ContainsKey(key)) { key = null; } } if (key != null) { Exception error = null; QueuedWorkerDoWorkEventArgs e = new QueuedWorkerDoWorkEventArgs(key, priority); try { this.OnDoWork(e); } catch (Exception exception2) { error = exception2; } QueuedWorkerCompletedEventArgs arg = new QueuedWorkerCompletedEventArgs(key, e.Result, priority, error, e.Cancel); if (!this.method_7()) { operation.PostOperationCompleted(this.sendOrPostCallback_0, arg); } } else if (operation != null) { operation.OperationCompleted(); } lock ((obj2 = this.object_0)) { flag2 = !this.method_0(); continue; } break; } goto Label_004E; }
public void Dispose() { ContextMenuName = null; _uiAsyncOperation.Send(() => _notifyIcon.Dispose()); _uiAsyncOperation.OperationCompleted(); }