/// <summary> /// Fires off your delegate asyncronously, using the threadpool or a full managed thread if needed. /// </summary> /// <param name="d">A delegate with a return value of some sort - can be cast to (DlgR) from an anonymous delgate with a return: Async.Do((DlgR)MyMethod);</param> /// <param name="state">A user object that can be tracked through the returned result</param> /// <param name="tryThreadPool">True to use the TP, otherwise just go to a ful lthread - good for long running tasks.</param> /// <param name="rMode">If true, will make sure no other instances are running your method.</param> /// <param name="getRetVal">If true, and the method/delgete returns something, it is included in the AsyncRes returned (after the method completes)</param> /// <returns>AsyncRes with all kind o' goodies for waiting, resturn and result values, etc.</returns> public static AsyncRes Do(DlgR d, bool getRetVal, object state, bool tryThreadPool, ReenteranceMode rMode) { return(Do(d, null, getRetVal, state, tryThreadPool, rMode)); }
/// <summary> /// Fires off your delegate asyncronously, using the threadpool or a full managed thread if needed. /// This overload always tries the ThreadPool and DOES NOT check for reentrance. /// </summary> /// <param name="d">A delegate with a return value of some sort - can be cast to (DlgR) from an anonymous delgate with a return: Async.Do((DlgR)MyMethod);</param> /// <param name="getRetVal">If true, and the method/delgete returns something, it is included in the AsyncRes returned (after the method completes)</param> /// <returns>AsyncRes with all kind o' goodies for waiting, etc.</returns> public static AsyncRes Do(DlgR d, bool getRetVal) { return(Do(d, getRetVal, ReenteranceMode.Allow)); }
/// <summary> /// Fires off your delegate asyncronously, using the threadpool or a full managed thread if needed. /// </summary> /// <param name="d">A delegate with a return value of some sort - can be cast to (DlgR) from an anonymous delgate with a return: Async.Do((DlgR)MyMethod);</param> /// <param name="rMode">If true, will make sure no other instances are running your method.</param> /// <param name="getRetVal">If true, and the method/delgete returns something, it is included in the AsyncRes returned (after the method completes)</param> /// <returns>AsyncRes with all kind o' goodies for waiting, resturn and result values, etc.</returns> public static AsyncRes Do(DlgR d, bool getRetVal, ReenteranceMode rMode) { return(Do(d, null, getRetVal, null, true, rMode)); }
/// <summary> /// Fires off your delegate asyncronously, using the threadpool or a full managed thread if needed. /// </summary> /// <param name="d">A delegate with a return value of some sort - can be cast to (DlgR) from an anonymous delgate with a return: Async.Do((DlgR)MyMethod);</param> /// <param name="rMode">If true, will make sure no other instances are running your method.</param> /// <param name="getRetVal">If true, and the method/delgete returns something, it is included in the AsyncRes returned (after the method completes)</param> /// <returns>AsyncRes with all kind o' goodies for waiting, resturn and result values, etc.</returns> public static AsyncRes Do(DlgR d, bool getRetVal, ReenteranceMode rMode) { return Do(d, null, getRetVal, null, true, rMode, null, true); }
/// <summary> /// Fires off your delegate asyncronously, using the threadpool or a full managed thread if needed. /// </summary> /// <param name="d">A void delegate - can be cast to (Dlg) from an anonymous delgate.</param> /// <param name="dr">A delegate with a return value of some sort - can be cast to (DlgR) from an anonymous delgate with a return.</param> /// <param name="state">A user object that can be tracked through the returned result</param> /// <param name="getRetVal">If true, and the method/delgete returns something, it is included in the AsyncRes returned (after the method completes)</param> /// <param name="tryThreadPool">True to use the TP, otherwise just go to a ful lthread - good for long running tasks.</param> /// <param name="rMode">If true, will make sure no other instances are running your method.</param> /// <returns>AsyncRes with all kind o' goodies for waiting, result values, etc.</returns> private static AsyncRes Do(DlgR dr, Dlg d, bool getRetVal, object state, bool tryThreadPool, ReenteranceMode rMode) { //get a generic MethodInfo for checks.. MethodInfo mi = ((dr != null) ? dr.Method : d.Method); //make a unique key for output usage string key = string.Format("{0}{1}{2}{3}", ((getRetVal) ? "<-" : ""), mi.DeclaringType, ((mi.IsStatic) ? ":" : "."), mi.Name); //our custom return value, holds our delegate, state, key, etc. AsyncRes res = new AsyncRes(state, ((dr != null) ? (Delegate)dr : (Delegate)d), key, rMode); //Create a delegate wrapper for what we will actually invoke.. Dlg dlg = (Dlg) delegate { if (!BeforeInvoke(res)) { return; //checks for reentrance issues and sets us up } try { if (res.IsCompleted) { return; } if (dr != null) { res.retVal = dr(); //use this one if theres a return } else { d(); //otherwise the simpler dlg } } catch (Exception ex) { //we never want a rogue exception on a random thread, it can't bubble up anywhere // logger.Error("Async Exception:" + ex); } finally { FinishInvoke(res); //this will fire our callback if they used it, and clean up } }; if (tryThreadPool) //we are going to use the .NET threadpool { try { //get some stats - much better than trying and silently failing or throwing an expensive exception int minThreads, minIO, threads, ioThreads, totalThreads, totalIO; ThreadPool.GetMinThreads(out minThreads, out minIO); ThreadPool.GetAvailableThreads(out threads, out ioThreads); ThreadPool.GetMaxThreads(out totalThreads, out totalIO); //check for at least our thread plus one more in ThreadPool if (threads > minThreads) { //this is what actually fires this task off.. bool result = ThreadPool.QueueUserWorkItem((WaitCallback) delegate { dlg(); }); if (result) { res.result = AsyncAction.ThreadPool; //this means success in queueing and running the item return(res); } else { //according to docs, this "won't ever happen" - exception instead, but just for kicks. //logger.Error("Failed to queue in threadpool, Method: " + key); } } else { Console.WriteLine(String.Format("Insufficient idle threadpool threads: {0} of {1} - min {2}, Method: {3}", threads, totalThreads, minThreads, key)); } } catch (Exception ex) { // logger.Error("Failed to queue in threadpool: " + ex.Message, ex); } } //if we got this far, then something up there failed, or they wanted a dedicated thread Thread t = new Thread((ThreadStart) delegate { dlg(); }); t.IsBackground = true; //this or threadpriority are candidates for additional settings t.Name = "Async_" + key; res.result = AsyncAction.Thread; t.Start(); return(res); }
/// <summary> /// Fires off your delegate asyncronously, using the threadpool or a full managed thread if needed. /// </summary> /// <param name="d">A void delegate - can be cast to (Dlg) from an anonymous delgate.</param> /// <param name="dr">A delegate with a return value of some sort - can be cast to (DlgR) from an anonymous delgate with a return.</param> /// <param name="state">A user object that can be tracked through the returned result</param> /// <param name="getRetVal">If true, and the method/delgete returns something, it is included in the AsyncRes returned (after the method completes)</param> /// <param name="tryThreadPool">True to use the TP, otherwise just go to a ful lthread - good for long running tasks.</param> /// <param name="rMode">If true, will make sure no other instances are running your method.</param> /// <returns>AsyncRes with all kind o' goodies for waiting, result values, etc.</returns> private static AsyncRes Do(DlgR dr, Dlg d, bool getRetVal, object state, bool tryThreadPool, ReenteranceMode rMode, Control control, bool async) { //get a generic MethodInfo for checks.. MethodInfo mi = ((dr != null) ? dr.Method : d.Method); //make a unique key for output usage string key = string.Format("{0}{1}{2}{3}", ((getRetVal) ? "<-" : ""), mi.DeclaringType, ((mi.IsStatic) ? ":" : "."), mi.Name); //our custom return value, holds our delegate, state, key, etc. AsyncRes res = new AsyncRes(state, ((dr != null) ? (Delegate)dr : (Delegate)d), key, rMode); //Create a delegate wrapper for what we will actually invoke.. Dlg dlg = (Dlg)delegate { if (!BeforeInvoke(res)) return; //checks for reentrance issues and sets us up try { if (res.IsCompleted) return; if (dr != null) { res.retVal = dr();//use this one if theres a return } else { d();//otherwise the simpler dlg } } catch (Exception ex) { //we never want a rogue exception on a random thread, it can't bubble up anywhere GSharp.Logging.Log.Exception("Error during async Invoke", ex); } finally { FinishInvoke(res);//this will fire our callback if they used it, and clean up } }; if (control != null) { try { res.control = control; res.result = AsyncAction.ControlInvoked; if (!async) { if (!control.InvokeRequired) { res.completedSynchronously = true; dlg(); } else { control.Invoke(dlg); } } else { control.BeginInvoke(dlg); } } catch (Exception ex) { GSharp.Logging.Log.Exception("Error during async Invoke", ex); } return res; } //don't catch these errors - if this fails, we shouldn't try a real thread or threadpool! if (tryThreadPool) { //we are going to use the .NET threadpool try { //get some stats - much better than trying and silently failing or throwing an expensive exception int minThreads, minIO, threads, ioThreads, totalThreads, totalIO; ThreadPool.GetMinThreads(out minThreads, out minIO); ThreadPool.GetAvailableThreads(out threads, out ioThreads); ThreadPool.GetMaxThreads(out totalThreads, out totalIO); //check for at least our thread plus one more in ThreadPool if (threads > minThreads) { //this is what actually fires this task off.. bool result = ThreadPool.QueueUserWorkItem((WaitCallback)delegate { dlg(); }); if (result) { res.result = AsyncAction.ThreadPool; //this means success in queueing and running the item return res; } else { //according to docs, this "won't ever happen" - exception instead, but just for kicks. GSharp.Logging.Log.Error("Failed to queue in threadpool. Method: " + key); } } else { GSharp.Logging.Log.Error(String.Format("Insufficient idle threadpool threads: {0} of {1} - min {2}, Method: {3}", threads, totalThreads, minThreads, key)); } } catch (Exception ex) { GSharp.Logging.Log.Exception("Failed to queue in threadpool: " + ex.Message + " Method: " + key, ex); } } //if we got this far, then something up there failed, or they wanted a dedicated thread Thread t = new Thread((ThreadStart)delegate { dlg(); }); t.IsBackground = true; //this or threadpriority are candidates for additional settings t.Name = "Async_" + key; res.result = AsyncAction.Thread; t.Start(); return res; }
/// <summary> /// Fires off your delegate asyncronously, using the threadpool or a full managed thread if needed. /// This overload always tries the ThreadPool and DOES NOT check for reentrance. /// </summary> /// <param name="d">A delegate with a return value of some sort - can be cast to (DlgR) from an anonymous delgate with a return: Async.Do((DlgR)MyMethod);</param> /// <param name="getRetVal">If true, and the method/delgete returns something, it is included in the AsyncRes returned (after the method completes)</param> /// <returns>AsyncRes with all kind o' goodies for waiting, etc.</returns> public static AsyncRes Do(DlgR d, bool getRetVal) { return Do(d, getRetVal, ReenteranceMode.Allow); }
/// <summary> /// Fires off your delegate, carefully using the correct UI thread /// </summary> /// <param name="d">A delegate with a return value of some sort - can be cast to (DlgR) from an anonymous delgate with a return: Async.Do((DlgR)MyMethod);</param> /// <param name="state">A user object that can be tracked through the returned result</param> /// <param name="async">Whether to run async, or try on current thread if invoke is not required.</param> /// <param name="getRetVal">If true, and the method/delgete returns something, it is included in the AsyncRes returned (after the method completes)</param> /// <param name="rMode">If true, will make sure no other instances are running your method.</param> /// <param name="c">A control to Invoke upon GUI thread of, if needed. Null if unused.</param> /// <returns>AsyncRes with all kind o' goodies for waiting, result values, etc.</returns> public static AsyncRes UI(DlgR d, bool getRetVal, Control c, object state, bool async, ReenteranceMode rMode) { return Do(d, null, getRetVal, state, false, rMode, c, async); }
/// <summary> /// Fires off your delegate, carefully using the correct UI thread /// </summary> /// <param name="d">A delegate with a return value of some sort - can be cast to (DlgR) from an anonymous delgate with a return: Async.Do((DlgR)MyMethod);</param> /// <param name="async">Whether to run async, or try on current thread if invoke is not required.</param> /// <param name="getRetVal">If true, and the method/delgete returns something, it is included in the AsyncRes returned (after the method completes)</param> /// <param name="c">A control to Invoke upon GUI thread of, if needed. Null if unused.</param> /// <returns>AsyncRes with all kind o' goodies for waiting, result values, etc.</returns> public static AsyncRes UI(DlgR d, bool getRetVal, Control c, bool async) { return Do(d, null, getRetVal, null, false, ReenteranceMode.Allow, c, async); }
/// <summary> /// Fires off your delegate asyncronously, using the threadpool or a full managed thread if needed. /// </summary> /// <param name="d">A delegate with a return value of some sort - can be cast to (DlgR) from an anonymous delgate with a return: Async.Do((DlgR)MyMethod);</param> /// <param name="state">A user object that can be tracked through the returned result</param> /// <param name="tryThreadPool">True to use the TP, otherwise just go to a ful lthread - good for long running tasks.</param> /// <param name="rMode">If true, will make sure no other instances are running your method.</param> /// <param name="getRetVal">If true, and the method/delgete returns something, it is included in the AsyncRes returned (after the method completes)</param> /// <returns>AsyncRes with all kind o' goodies for waiting, resturn and result values, etc.</returns> public static AsyncRes Do(DlgR d, bool getRetVal, object state, bool tryThreadPool, ReenteranceMode rMode) { return Do(d, null, getRetVal, state, tryThreadPool, rMode, null, true); }
/// <summary> /// Fires off your delegate, carefully using the correct UI thread /// </summary> /// <param name="d">A delegate with a return value of some sort - can be cast to (DlgR) from an anonymous delgate with a return: Async.Do((DlgR)MyMethod);</param> /// <param name="state">A user object that can be tracked through the returned result</param> /// <param name="async">Whether to run async, or try on current thread if invoke is not required.</param> /// <param name="getRetVal">If true, and the method/delgete returns something, it is included in the AsyncRes returned (after the method completes)</param> /// <param name="rMode">If true, will make sure no other instances are running your method.</param> /// <param name="c">A control to Invoke upon GUI thread of, if needed. Null if unused.</param> /// <returns>AsyncRes with all kind o' goodies for waiting, result values, etc.</returns> public static AsyncRes UI(DlgR d, bool getRetVal, Control c, object state, bool async, ReenteranceMode rMode) { return(Do(d, null, getRetVal, state, false, rMode, c, async)); }
/// <summary> /// Fires off your delegate, carefully using the correct UI thread /// </summary> /// <param name="d">A delegate with a return value of some sort - can be cast to (DlgR) from an anonymous delgate with a return: Async.Do((DlgR)MyMethod);</param> /// <param name="async">Whether to run async, or try on current thread if invoke is not required.</param> /// <param name="getRetVal">If true, and the method/delgete returns something, it is included in the AsyncRes returned (after the method completes)</param> /// <param name="c">A control to Invoke upon GUI thread of, if needed. Null if unused.</param> /// <returns>AsyncRes with all kind o' goodies for waiting, result values, etc.</returns> public static AsyncRes UI(DlgR d, bool getRetVal, Control c, bool async) { return(Do(d, null, getRetVal, null, false, ReenteranceMode.Allow, c, async)); }