Browse Source

Merge pull request #1085 from angelobreuer/master

Application.Shutdown() does not reset SynchronizationContext
Charlie Kindel 4 years ago
parent
commit
285cc82b9f
2 changed files with 33 additions and 0 deletions
  1. 6 0
      Terminal.Gui/Core/Application.cs
  2. 27 0
      UnitTests/ApplicationTests.cs

+ 6 - 0
Terminal.Gui/Core/Application.cs

@@ -540,6 +540,12 @@ namespace Terminal.Gui {
 			Driver?.End ();
 			Driver = null;
 			_initialized = false;
+
+			// Reset synchronization context to allow the user to run async/await,
+			// as the main loop has been ended, the synchronization context from 
+			// gui.cs does no longer process any callbacks. See #1084 for more details:
+			// (https://github.com/migueldeicaza/gui.cs/issues/1084).
+			SynchronizationContext.SetSynchronizationContext (syncContext: null);
 		}
 
 		static void Redraw (View view)

+ 27 - 0
UnitTests/ApplicationTests.cs

@@ -1,6 +1,8 @@
 using System;
 using System.Collections.Generic;
 using System.Linq;
+using System.Threading;
+using System.Threading.Tasks;
 using Terminal.Gui;
 using Xunit;
 
@@ -220,5 +222,30 @@ namespace Terminal.Gui {
 			Application.Shutdown ();
 			Assert.Equal (3, count);
 		}
+
+		[Fact]
+		public void Shutdown_Allows_Async ()
+		{
+			static async Task TaskWithAsyncContinuation ()
+			{
+				await Task.Yield ();
+				await Task.Yield ();
+			}
+
+			Init ();
+			Application.Shutdown ();
+
+			var task = TaskWithAsyncContinuation ();
+			Thread.Sleep (20);
+			Assert.True (task.IsCompletedSuccessfully);
+		}
+
+		[Fact]
+		public void Shutdown_Resets_SyncContext ()
+		{
+			Init ();
+			Application.Shutdown ();
+			Assert.Null (SynchronizationContext.Current);
+		}
 	}
 }