mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-02-15 02:48:35 -05:00
Create MS.SqlTools.Credentials project (#249)
* Initial credential service files * Clean-up hostloader * Update build scripts to build credentials archive * Move hosting files to new assembly * Add credentials files to MS.SqlTools.Credentials * Remove duplicate files * Update namespace in program.cs * Fix test build breaks * Update extensions visibility. * Remove unused resource strings * Add xproj files to SLN for appveyor builds * Fix appveyor build break in test project * Fix extensibility tests * Fix various typos in latest iteration * Add settings for Integration build * Fix codecoverage.bat to use full pdb for new projects * Fix bug when packing in folder with native images * Fix typos in xproj * Reset XLF to fix build.cmd
This commit is contained in:
52
src/Microsoft.SqlTools.Hosting/Utility/AsyncContext.cs
Normal file
52
src/Microsoft.SqlTools.Hosting/Utility/AsyncContext.cs
Normal file
@@ -0,0 +1,52 @@
|
||||
//
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||
//
|
||||
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Microsoft.SqlTools.ServiceLayer.Utility
|
||||
{
|
||||
/// <summary>
|
||||
/// Simplifies the setup of a SynchronizationContext for the use
|
||||
/// of async calls in the current thread.
|
||||
/// </summary>
|
||||
public static class AsyncContext
|
||||
{
|
||||
/// <summary>
|
||||
/// Starts a new ThreadSynchronizationContext, attaches it to
|
||||
/// the thread, and then runs the given async main function.
|
||||
/// </summary>
|
||||
/// <param name="asyncMainFunc">
|
||||
/// The Task-returning Func which represents the "main" function
|
||||
/// for the thread.
|
||||
/// </param>
|
||||
public static void Start(Func<Task> asyncMainFunc)
|
||||
{
|
||||
// Is there already a synchronization context?
|
||||
if (SynchronizationContext.Current != null)
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
"A SynchronizationContext is already assigned on this thread.");
|
||||
}
|
||||
|
||||
// Create and register a synchronization context for this thread
|
||||
var threadSyncContext = new ThreadSynchronizationContext();
|
||||
SynchronizationContext.SetSynchronizationContext(threadSyncContext);
|
||||
|
||||
// Get the main task and act on its completion
|
||||
Task asyncMainTask = asyncMainFunc();
|
||||
asyncMainTask.ContinueWith(
|
||||
t => threadSyncContext.EndLoop(),
|
||||
TaskScheduler.Default);
|
||||
|
||||
// Start the synchronization context's request loop and
|
||||
// wait for the main task to complete
|
||||
threadSyncContext.RunLoopOnCurrentThread();
|
||||
asyncMainTask.GetAwaiter().GetResult();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user