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

DispatcherOperation.cs « System.Windows.Threading « WindowsBase « FPF « src - github.com/microsoft/vs-editor-api.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4fd9847b4eb508afdb9664f3f60fd45ee0618417 (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;

using CoreFoundation;

namespace System.Windows.Threading
{
    public enum DispatcherOperationStatus
    {
        Pending,
        Aborted,
        Completed,
        Executing
    }

    public delegate object DispatcherOperationCallback(object arg);

    public sealed class DispatcherOperation
    {
        readonly Delegate method;
        readonly object[] args;
        readonly CancellationToken cancellationToken;
        readonly DispatcherOperationTaskSource taskSource;

        public Dispatcher Dispatcher { get; }
        public DispatcherPriority Priority { get; }

        Exception exception;
        public object Result { get; private set; }
        public DispatcherOperationStatus Status { get; private set; }

        public event EventHandler Aborted;
        public event EventHandler Completed;

        internal DispatcherOperation(
            Dispatcher dispatcher,
            Delegate method,
            DispatcherPriority priority,
            object[] args,
            CancellationToken cancellationToken,
            DispatcherOperationTaskSource taskSource)
        {
            Dispatcher = dispatcher;
            this.method = method;
            Priority = priority;
            this.args = args;
            this.cancellationToken = cancellationToken;
            this.taskSource = taskSource;
            Status = DispatcherOperationStatus.Pending;
        }

        internal DispatcherOperation BeginInvoke()
        {
            DispatchQueue.MainQueue.DispatchAsync(() =>
            {
                CoreInvoke(beginInvokeBehavior: true);
                if (exception != null)
                    Dispatcher.OnUnhandledException(exception);
            });

            return this;
        }

        internal DispatcherOperation InvokeAsync()
        {
            if (taskSource == null)
                throw new InvalidOperationException();

            DispatchQueue.MainQueue.DispatchAsync(() =>
            {
                try
                {
                    CoreInvoke(beginInvokeBehavior: false);
                }
                catch (OperationCanceledException)
                {
                    taskSource.SetCanceled();
                    return;
                }

                if (cancellationToken.IsCancellationRequested)
                    taskSource.SetCanceled();
                else if (exception != null)
                    taskSource.SetException(exception);
                else
                    taskSource.SetResult();
            });

            return this;
        }

        internal void Invoke(TimeSpan timeout)
        {
            if (timeout != Timeout.InfiniteTimeSpan)
                throw new NotSupportedException("timeouts are not supported");

            var mainQueue = DispatchQueue.MainQueue;

            if (DispatchQueue.CurrentQueue != mainQueue)
                mainQueue.DispatchSync(
                    () => CoreInvoke(beginInvokeBehavior: false));
            else
                CoreInvoke(beginInvokeBehavior: false);

            if (exception != null)
                throw exception;
        }

        void CoreInvoke(bool beginInvokeBehavior)
        {
            Status = DispatcherOperationStatus.Executing;

            var oldSynchronizationContext = SynchronizationContext.Current;

            try
            {
                SynchronizationContext.SetSynchronizationContext(Dispatcher.DefaultSynchronizationContext);

                try
                {
                    if (method is Action action)
                        action();
                    else
                        Result = method.DynamicInvoke(args);
                }
                catch (Exception e)
                {
                    exception = e;
                }

                if (beginInvokeBehavior)
                {
                    if (exception is OperationCanceledException)
                    {
                        Status = DispatcherOperationStatus.Aborted;
                        exception = null;
                        Aborted?.Invoke(this, EventArgs.Empty);
                    }
                    else
                    {
                        Status = DispatcherOperationStatus.Completed;
                        Completed?.Invoke(this, EventArgs.Empty);
                    }
                }
            }
            finally
            {
                SynchronizationContext.SetSynchronizationContext(oldSynchronizationContext);
            }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public TaskAwaiter GetAwaiter()
        {
            if (taskSource == null)
                throw new InvalidOperationException();

            return taskSource.GetTask().GetAwaiter();
        }

        public Task Task
        {
            get
            {
                if (taskSource == null)
                    throw new InvalidOperationException();

                return taskSource.GetTask();
            }
        }
    }
}