/* * Trace: * On UI thread: BEGINNING of EventHandler * On UI thread: Beginning of GetText * On ThreadPool thread: Called GetAsync() * On ThreadPool thread: Called Thread.Sleep * On ThreadPool thread: Called ReadAsStringAsync() * On ThreadPool thread: Called Thread.Sleep again * On UI thread: END of EventHandler */ private void BLOCKING_UI_CALL(object sender, EventArgs e) { Logger.Write("BEGINNING of EventHandler"); // on UI thread var result = _service .SafeMethodAsync("https://www.google.com") // on UI and ThreadPool threads .Result; // BLOCK UI thread till the end of getting result from library method label1.Text = result; // on UI thread Logger.Write("END of EventHandler"); // on UI thread }
/* * Trace: * On UI thread: BEGINNING of EventHandler * On UI thread: Beginning of SafeMethodAsync * On ThreadPool thread: Called Task.Delay (SafeMethodAsync) * On ThreadPool thread: Called Thread.Sleep (SafeMethodAsync) * On ThreadPool thread: End of SafeMethodAsync * On UI thread: END of EventHandler */ private void BLOCKING_UI_CALL(object sender, EventArgs e) { Logger.Write("BEGINNING of EventHandler"); // on UI thread Task.WaitAll(_service.SafeMethodAsync(), _task2, _task3); Logger.Write("END of EventHandler"); // on UI thread }
/* * Trace: * On UI thread: Beginning of EventHandlerMethod * On UI thread: Beginning of SafeMethodAsync * On ThreadPool thread: Called Task.Delay (SafeMethodAsync) * On ThreadPool thread: Called Thread.Sleep (SafeMethodAsync) * On ThreadPool thread: End of SafeMethodAsync * On UI thread: End of EventHandlerMethod */ private void BLOCKING_OF_UI_THREAD_BUT_WITHOUT_DEADLOCK_BECAUSE_OF_ConfigureAwaitFalse_IN_THE_LIBRARY_METHOD(object sender, EventArgs e) { Logger.Write("Beginning of EventHandlerMethod"); // on UI thread _service .SafeMethodAsync() // on UI and on ThreadPool threads .Wait(); // BLOCK UI thread till the end of execution of library method Logger.Write("End of EventHandlerMethod"); // on UI thread }