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: 64c0c0dd9236ab3dae064900390c1593d8903928 (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
//
//  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.Threading.Tasks;
using Microsoft.VisualStudio.Threading;

namespace Microsoft.VisualStudio.Utilities
{
    /// <summary>
    /// A helper for managing JoinableTasks.
    /// </summary>
    public class JoinableTaskHelper
    {
        public readonly JoinableTaskContext Context;
        public readonly JoinableTaskCollection Collection;
        public readonly JoinableTaskFactory Factory;

        public JoinableTaskHelper(JoinableTaskContext context)
        {
            if (context == null)
                throw new ArgumentNullException(nameof(context));

            this.Context = 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 async Task DisposeAsync()
        {
            await this.Collection.JoinTillEmptyAsync();
        }

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