1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| public static void testWait() { XmlConfigurator.Configure(new System.IO.FileInfo("log4net.config")); TaskCompletionSource<string> source = new TaskCompletionSource<string>();
new Thread(o => { log.Info("Thread Sleep"); Thread.Sleep(3000); log.Info("Thread TrySetResult"); source.TrySetCanceled(); }).Start();
log.InfoFormat("Start Wait"); try { bool wait = source.Task.Wait(5000); log.InfoFormat("Wait:{0}", wait); if (wait) { if (source.Task.Status == TaskStatus.RanToCompletion) { log.InfoFormat("Result:{0}", source.Task.Result); } else if (source.Task.Status == TaskStatus.Faulted) { log.InfoFormat("Exception:{0}", source.Task.Exception); } } else { log.InfoFormat("Timeout"); } } catch (Exception e) { log.InfoFormat("Exception: {0}=>{1}", e.GetType(),e.InnerException.GetType()); } }
|