/// <summary> /// Implements <see cref="IBackgroundDispatcher.BeginInvoke(WaitCallback, object, AsyncCallback, object)" />. /// </summary> public IAsyncResult BeginInvoke(WaitCallback callback, object state, AsyncCallback completionCallback, object asyncState) { var invokerArgs = _etwActivityMethodInvoker.CreateInvokerArgs(callback, new object[] { state }); var result = _invokerWaitCallback.BeginInvoke(invokerArgs, completionCallback, asyncState); return(result); }
/// <summary> /// Extends BeginInvoke so that when a state object is not needed, null does not need to be passed. /// <example> /// waitcallback.BeginInvoke(state, callback); /// </example> /// </summary> public static IAsyncResult BeginInvoke(this WaitCallback waitcallback, Object state, AsyncCallback callback) { if (waitcallback == null) { throw new ArgumentNullException("waitcallback"); } return(waitcallback.BeginInvoke(state, callback, null)); }
static int Main () { AppDomain.CurrentDomain.UnhandledException += OnUnhandledException; WaitCallback wcb = new WaitCallback ((a) => { throw new Exception ("From the threadpoool"); }); wcb.BeginInvoke (wcb, OnCBFinished, null); Thread.Sleep (1000); return 1; }
static int Main () { AppDomain.CurrentDomain.UnhandledException += OnUnhandledException; WaitCallback wcb = new WaitCallback ((a) => { Thread.CurrentThread.Abort (); }); wcb.BeginInvoke (wcb, null, null); Thread.Sleep (1000); return 0; }
static int Main() { AppDomain.CurrentDomain.UnhandledException += OnUnhandledException; WaitCallback wcb = new WaitCallback((a) => { throw new Exception("From the threadpoool"); }); wcb.BeginInvoke(wcb, OnCBFinished, null); Thread.Sleep(1000); return(1); }
static int Main() { AppDomain.CurrentDomain.UnhandledException += OnUnhandledException; WaitCallback wcb = new WaitCallback((a) => { Thread.CurrentThread.Abort(); }); wcb.BeginInvoke(wcb, OnCBFinished, null); Thread.Sleep(1000); return(1); }
static int Main() { AppDomain.CurrentDomain.UnhandledException += OnUnhandledException; WaitCallback wcb = new WaitCallback((a) => { throw new Exception("From the threadpoool"); }); wcb.BeginInvoke(wcb, OnCBFinished, null); // Should not finish, OnUnhandledException exit path is expected to be executed Thread.Sleep(10000); return(2); }
static int Main () { monitor = new object (); AppDomain.CurrentDomain.UnhandledException += OnUnhandledException; WaitCallback wcb = new WaitCallback ((a) => { Thread.CurrentThread.Abort(); }); wcb.BeginInvoke (wcb, OnCBFinished, null); lock (monitor) { Monitor.Wait (monitor); } Thread.Sleep (1000); return 1; }
public static bool QueueUserWorkItem (WaitCallback callBack, object state) { if (callBack == null) throw new ArgumentNullException ("callBack"); if (callBack.IsTransparentProxy ()) { IAsyncResult ar = callBack.BeginInvoke (state, null, null); if (ar == null) return false; } else { AsyncResult ares = new AsyncResult (callBack, state, true); pool_queue (ares); } return true; }
static int Main() { monitor = new object(); AppDomain.CurrentDomain.UnhandledException += OnUnhandledException; WaitCallback wcb = new WaitCallback((a) => { Thread.CurrentThread.Abort(); }); wcb.BeginInvoke(wcb, OnCBFinished, null); lock (monitor) { Monitor.Wait(monitor); } Thread.Sleep(1000); return(1); }
static void OnSearchTextChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) { //change ToAbortFlag = IsWorkingFlag; if (workerCallback == null) { workerCallback = new WaitCallback(doSearch); } searchObjState.SearchString = e.NewValue.ToString(); //if (searchObjState.SearchString.Length > 2) { workerCallback.BeginInvoke(searchObjState, null, null); } }
static int Main() { AppDomain.CurrentDomain.UnhandledException += OnUnhandledException; WaitCallback wcb = new WaitCallback((a) => { Thread.CurrentThread.Abort(); }); wcb.BeginInvoke(wcb, OnCBFinished, null); if (!mre.WaitOne(10000)) { return(2); } GC.Collect(); GC.WaitForPendingFinalizers(); /* expected exit code: 255 */ Thread.Sleep(10000); return(0); }
public static bool QueueUserWorkItem (WaitCallback callBack, object state) { if (callBack == null) throw new ArgumentNullException ("callBack"); #if MOONLIGHT callBack = MoonlightHandler (callBack); #endif if (callBack.IsTransparentProxy ()) { IAsyncResult ar = callBack.BeginInvoke (state, null, null); if (ar == null) return false; } else { if (!callBack.HasSingleTarget) throw new Exception ("The delegate must have only one target"); AsyncResult ares = new AsyncResult (callBack, state, true); pool_queue (ares); } return true; }
public static bool UnsafeQueueUserWorkItem (WaitCallback callBack, object state) { // no stack propagation here (that's why it's unsafe and requires extra security permissions) if (!callBack.IsTransparentProxy ()) { if (!callBack.HasSingleTarget) throw new Exception ("The delegate must have only one target"); AsyncResult ares = new AsyncResult (callBack, state, false); pool_queue (ares); return true; } try { if (!ExecutionContext.IsFlowSuppressed ()) ExecutionContext.SuppressFlow (); // on current thread only IAsyncResult ar = callBack.BeginInvoke (state, null, null); if (ar == null) return false; } finally { if (ExecutionContext.IsFlowSuppressed ()) ExecutionContext.RestoreFlow (); } return true; }
public static bool UnsafeQueueUserWorkItem (WaitCallback callBack, object state) { if (Microsoft.ThreadPool.UseMicrosoftThreadPool) { return Microsoft.ThreadPool.UnsafeQueueUserWorkItem (callBack, state); } else { if (callBack == null) throw new ArgumentNullException ("callBack"); // no stack propagation here (that's why it's unsafe and requires extra security permissions) if (!callBack.IsTransparentProxy ()) { AsyncResult ares = new AsyncResult (callBack, state, false); pool_queue (ares); return true; } try { if (!ExecutionContext.IsFlowSuppressed ()) ExecutionContext.SuppressFlow (); // on current thread only IAsyncResult ar = callBack.BeginInvoke (state, null, null); if (ar == null) return false; } finally { if (ExecutionContext.IsFlowSuppressed ()) ExecutionContext.RestoreFlow (); } return true; } }
public static bool QueueUserWorkItem (WaitCallback callBack, object state) { if (Microsoft.ThreadPool.UseMicrosoftThreadPool) { return Microsoft.ThreadPool.QueueUserWorkItem (callBack, state); } else { if (callBack == null) throw new ArgumentNullException ("callBack"); if (callBack.IsTransparentProxy ()) { IAsyncResult ar = callBack.BeginInvoke (state, null, null); if (ar == null) return false; } else { AsyncResult ares = new AsyncResult (callBack, state, !ExecutionContext.IsFlowSuppressed()); pool_queue (ares); } return true; } }
//异步页面的任务启动方法 public IAsyncResult OnBegin(object sender, EventArgs e, AsyncCallback cb, object extraData) { _proc = new WaitCallback(ChatInvokeProc); Hashtable theUser = new Hashtable(); theUser = (Hashtable)extraData; string user = theUser["guid"].ToString(); _guid = user; Thread.CurrentThread.Name = "上下文线程" + user; //用户处理,不存在则增加,即为登录 theUser["asyn"] = this; theUser["lastUpdateTime"] = DateTime.Now.ToString(); Hashtable feachUser = new Hashtable(); bool isInCach=false; for (var i = 0; i < globalCache.userCache.Count; i++) { feachUser = (Hashtable)globalCache.userCache[i]; if (theUser["guid"].ToString() == feachUser["guid"].ToString()) { globalCache.userCache[i] = theUser; isInCach = true; } } if (!isInCach) { globalCache.userCache.Add(theUser); } //开始异步执行,这里会开启一个新的辅助线程 return _proc.BeginInvoke(extraData, cb, extraData); }
public static bool UnsafeQueueUserWorkItem (WaitCallback callBack, object state) { // no stack propagation here (that's why it's unsafe and requires extra security permissions) IAsyncResult ar = null; try { if (!ExecutionContext.IsFlowSuppressed ()) ExecutionContext.SuppressFlow (); // on current thread only ar = callBack.BeginInvoke (state, null, null); } finally { if (ExecutionContext.IsFlowSuppressed ()) ExecutionContext.RestoreFlow (); } return (ar != null); }
public static object Invoke(object untypedMethod, object[] arguments) { return(Helper.SimpleWrap <object>( delegate(ClrSyncManager manager) { var method = (Method)untypedMethod; if (method.ShortName == "BeginInvoke") { // extract the delegate arguments, and callback Debug.Assert(arguments.Length >= 3); var delegateArguments = new object[arguments.Length - 3]; var callback = (AsyncCallback)arguments[arguments.Length - 2]; var asyncState = arguments[arguments.Length - 1]; var @delegate = (global::System.Delegate)arguments[0]; global::System.Array.Copy(arguments, 1, delegateArguments, 0, delegateArguments.Length); // create the IAsyncResult that everyone shares var asyncResult = new WrappedAsyncResult(asyncState); // we wrap the actual invoke // so that CHESS gets control, as usual Helper.ThreadRoutineArg p = new Helper.ThreadRoutineArg(); p.s = new Original::Semaphore(0, 1); p.o = null; p.wcb = (o => { manager.SetMethodInfo("BeginInvoke(Begin)"); manager.SyncVarAccess(asyncResult, MSyncVarOp.LOCK_ACQUIRE); manager.CommitSyncVarAccess(); // synchronous call of the actual method object result = null; try { result = @delegate.DynamicInvoke(delegateArguments); // make sure to copy values of out params List <object> byRefResults = new List <object>(); var delegateParameters = @delegate.GetType().GetMethod("Invoke").GetParameters(); for (int i = 0; i < delegateParameters.Length; i++) { if (delegateParameters[i].ParameterType.IsByRef) { byRefResults.Add(delegateArguments[i]); } } asyncResult.SetByRefResults(byRefResults.ToArray()); } catch (Exception e) { // From ECMA: // Note: the callee can throw exceptions. // Any unhandled exception propagates to the caller via the EndInvoke method. asyncResult.CalleeThrown = e; } // this is the actual return results of the method, which becomes the return value // of the EndInvoke asyncResult.Result = result; manager.SetMethodInfo("BeginInvoke(Completed)"); manager.SyncVarAccess(asyncResult, MSyncVarOp.LOCK_RELEASE); // we set completed before the callback is done because // the callback may call EndInvoke. asyncResult.Completed = true; asyncResult.MyHandle.Set(); manager.CommitSyncVarAccess(); // From ECMA: The VES shall call this delegate when the value [the callback] // is computed or an exception has been raised indicating that the result will // not be available. // TODO: but how is the callback supposed to "know" whether or not an exception was raised? if (callback != null) { callback(asyncResult); } }); // create the wrapper WaitCallback call = Helper.ThreadCreateWrapper(manager); // store it aware for later use asyncResult.MyDelegate = call; // TODO: how do we insure this one is done without instrumentation? IAsyncResult invokeResult = call.BeginInvoke(p, null, null); // we squirrel this way because we have to match the above BeginInvoke // with the EndInvoke later asyncResult.RealAsyncResult = invokeResult; // let the asynch task proceed ChessTask child = manager.TaskFork(); manager.RegisterTaskSemaphore(child, p.s, false); manager.TaskResume(child); return asyncResult; } else if (method.ShortName == "EndInvoke") { WrappedAsyncResult asyncResult = (WrappedAsyncResult)arguments[arguments.Length - 1]; // wait for the BeginInvoke to complete while (true) { manager.SetMethodInfo("EndInvoke(Begin)"); manager.SyncVarAccess(asyncResult, MSyncVarOp.LOCK_ACQUIRE); if (!asyncResult.Completed) { manager.LocalBacktrack(); continue; } manager.CommitSyncVarAccess(); break; } // for good measure, do the EndInvoke, to match the BeginInvoke asyncResult.MyDelegate.EndInvoke(asyncResult.RealAsyncResult); manager.SetMethodInfo("EndInvoke(Completed)"); manager.SyncVarAccess(asyncResult, MSyncVarOp.LOCK_RELEASE); manager.CommitSyncVarAccess(); if (asyncResult.CalleeThrown != null) { throw asyncResult.CalleeThrown; } else { // copy the results back! global::System.Array.Copy(asyncResult.GetByRefResults(), 0, arguments, 1, asyncResult.GetByRefResults().Length); } return asyncResult.Result; } else { throw new InvalidOperationException("unexpected method: " + method.FullName); } }, delegate() { // TODO: no instrumentation return null; })); }
public static bool QueueUserWorkItem (WaitCallback callBack, object state) { if (callBack == null) throw new ArgumentNullException ("callBack"); #if NET_2_1 && !MONOTOUCH callBack = MoonlightHandler (callBack); #endif IAsyncResult ar = callBack.BeginInvoke (state, null, null); if (ar == null) return false; return true; }