admin
2020-06-10 a610f2ab6e543d2cb78c1ef212ac6a74ddc067d9
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
// Copyright © 2015 The CefSharp Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
 
#pragma once
 
#include "Stdafx.h"
#include "include/cef_task.h"
#include "CefTaskWrapper.h"
 
using namespace System::Threading::Tasks;
using namespace System::Runtime::InteropServices;
 
namespace CefSharp
{
    namespace Internals
    {
        private ref class CefTaskScheduler : TaskScheduler, ITaskScheduler
        {
        public:
            CefThreadId _thread;
 
            CefTaskScheduler(CefThreadId thread) :
                _thread(thread)
            {
            };
 
            virtual void QueueTask(Task^ task) override
            {
                CefRefPtr<CefTask> taskWrapper = new CefTaskWrapper(task, this);
 
                CefPostTask(_thread, taskWrapper);
            };
 
            virtual void ExecuteTask(Task^ task)
            {
                TryExecuteTask(task);
            };
 
            static bool CurrentlyOnThread(CefThreadId threadId)
            {
                return CefCurrentlyOn(threadId);
            }
 
            static void EnsureOn(CefThreadId threadId, String^ context)
            {
                if (!CefCurrentlyOn(threadId))
                {
                    throw gcnew InvalidOperationException(String::Format("Executed '{0}' on incorrect thread. This method expects to run on the CEF {1} thread!", context, ((CefThreadIds)threadId).ToString()));
                }
            }
 
            static void EnsureOn(CefThreadIds threadId, String^ context)
            {
                EnsureOn((CefThreadId)threadId, context);
            }
 
        protected:
            virtual bool TryExecuteTaskInline(Task^ task, bool taskWasPreviouslyQueued) override
            {
                // You might think this method might not get called,
                // but a .ContinueWith(..., TaskContinuationOpation.ExecuteSyncronously)
                // will probably end up calling this method,
                // so assume the callers know what they're doing and execute the task
                // inline on this thread (if the current thread is correct for this scheduler)
                if (CefCurrentlyOn(_thread))
                {
                    return TryExecuteTask(task);
                }
                return false;
            };
 
            virtual IEnumerable<Task^>^ GetScheduledTasks() override
            {
                return nullptr;
            };
        };
    }
}