2020-01-10 11:56:11 -08:00
|
|
|
// Copyright (c) .NET Foundation. All rights reserved.
|
2017-05-26 17:50:49 -07:00
|
|
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using Microsoft.VisualStudio.Shell;
|
|
|
|
|
using Microsoft.VisualStudio.Threading;
|
|
|
|
|
|
|
|
|
|
namespace Test.Utility.Threading
|
|
|
|
|
{
|
|
|
|
|
public sealed class DispatcherThreadFixture : IDisposable
|
|
|
|
|
{
|
|
|
|
|
private readonly DispatcherThread _dispatcherThread;
|
|
|
|
|
private readonly JoinableTaskContextNode _joinableTaskContextNode;
|
|
|
|
|
|
|
|
|
|
public JoinableTaskFactory JoinableTaskFactory => _joinableTaskContextNode.Factory;
|
|
|
|
|
|
|
|
|
|
public DispatcherThreadFixture()
|
|
|
|
|
{
|
|
|
|
|
// ThreadHelper in VS requires a persistent dispatcher thread. Because
|
|
|
|
|
// each unit test executes on a new thread, we create our own
|
|
|
|
|
// persistent thread that acts like a UI thread. This will be invoked just
|
|
|
|
|
// once for the module.
|
|
|
|
|
_dispatcherThread = new DispatcherThread();
|
|
|
|
|
|
|
|
|
|
_dispatcherThread.Invoke(() =>
|
|
|
|
|
{
|
|
|
|
|
// Internally this calls ThreadHelper.SetUIThread(), which
|
|
|
|
|
// causes ThreadHelper to remember this thread for the
|
|
|
|
|
// lifetime of the process as the dispatcher thread.
|
2020-01-13 12:30:12 -08:00
|
|
|
#pragma warning disable VSTHRD010 // Invoke single-threaded types on Main thread
|
2017-05-26 17:50:49 -07:00
|
|
|
var serviceProvider = ServiceProvider.GlobalProvider;
|
2020-01-13 12:30:12 -08:00
|
|
|
#pragma warning restore VSTHRD010 // Invoke single-threaded types on Main thread
|
2017-05-26 17:50:49 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
_joinableTaskContextNode = new JoinableTaskContextNode(
|
2020-01-10 11:56:11 -08:00
|
|
|
#pragma warning disable VSSDK005 // Avoid instantiating JoinableTaskContext
|
2017-05-26 17:50:49 -07:00
|
|
|
new JoinableTaskContext(_dispatcherThread.Thread, _dispatcherThread.SyncContext));
|
2020-01-10 11:56:11 -08:00
|
|
|
#pragma warning restore VSSDK005 // Avoid instantiating JoinableTaskContext
|
2017-05-26 17:50:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
_dispatcherThread.Dispose();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|