Welcome to mirror list, hosted at ThFree Co, Russian Federation.

JoinableTaskHelper.cs « TextData « Internal « Def « Text « src - github.com/microsoft/vs-editor-api.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3521c282b1d5d25a69190b9ca186bf98e0cece32 (plain)
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
//
//  Copyright (c) Microsoft Corporation. All rights reserved.
//  Licensed under the MIT License. See License.txt in the project root for license information.
//
// This file contain internal APIs that are subject to change without notice.
// Use at your own risk.
//
using System;
using System.Diagnostics.CodeAnalysis;
using System.Threading.Tasks;
using Microsoft.VisualStudio.Threading;

namespace Microsoft.VisualStudio.Utilities
{
    /// <summary>
    /// A helper for managing JoinableTasks.
    /// </summary>
    public class JoinableTaskHelper
    {
#pragma warning disable CA1051 // Do not declare visible instance fields
        [SuppressMessage("Microsoft.Security", "CA2104", Justification = "By design")]
        public readonly JoinableTaskContext Context;

        [SuppressMessage("Microsoft.Security", "CA2104", Justification = "By design")]
        public readonly JoinableTaskCollection Collection;

        [SuppressMessage("Microsoft.Security", "CA2104", Justification = "By design")]
        public readonly JoinableTaskFactory Factory;
#pragma warning restore CA1051 // Do not declare visible instance fields

        public JoinableTaskHelper(JoinableTaskContext context)
        {
            this.Context = context ?? throw new ArgumentNullException(nameof(context));
            this.Collection = context.CreateCollection();
            this.Factory = context.CreateFactory(this.Collection);
        }

        public JoinableTask RunOnUIThread(Action action, bool forceTaskSwitch = true)
        {
            using (this.Context.SuppressRelevance())
            {
                return this.Factory.RunAsync(async delegate
                                    {
                                        if (forceTaskSwitch && this.Context.IsOnMainThread)
                                        {
                                            await Task.Yield();
                                        }

                                        await this.Factory.SwitchToMainThreadAsync();
                                        action();
                                    });
            }
        }

        public JoinableTask<T> RunOnUIThread<T>(Func<T> function, bool forceTaskSwitch = true)
        {
            using (this.Context.SuppressRelevance())
            {
                return this.Factory.RunAsync(async delegate
                                    {
                                        if (forceTaskSwitch && this.Context.IsOnMainThread)
                                        {
                                            await Task.Yield();
                                        }

                                        await this.Factory.SwitchToMainThreadAsync();
                                        return function();
                                    });
            }
        }

        public Task DisposeAsync()
        {
            return this.Collection.JoinTillEmptyAsync();
        }

        public void Dispose()
        {
            this.Context.Factory.Run(async delegate
            {
                // Not this.Factory
                await this.DisposeAsync().ConfigureAwait(false);
            });
        }
    }
}