Skip to content

Commit 88762cb

Browse files
committed
Core - Add CefThread.ExecuteOnUiThread(Action action)
- DevToolsClient now uses this overload
1 parent f853006 commit 88762cb

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

CefSharp/DevTools/DevToolsClient.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,6 @@ public Task<T> ExecuteDevToolsMethodAsync<T>(string method, IDictionary<string,
167167
CefThread.ExecuteOnUiThread(() =>
168168
{
169169
ExecuteDevToolsMethod(browserHost, messageId, method, parameters, methodResultContext);
170-
return (object)null;
171170
});
172171
}
173172
else

CefSharp/Internals/CefThread.cs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,58 @@ public static Task<TResult> ExecuteOnUiThread<TResult>(Func<TResult> function)
100100
}
101101
}
102102

103+
/// <summary>
104+
/// Execute the provided action on the CEF UI Thread
105+
/// </summary>
106+
/// <param name="action">action</param>
107+
/// <returns>Task</returns>
108+
public static Task ExecuteOnUiThread(Action action)
109+
{
110+
lock (LockObj)
111+
{
112+
if (HasShutdown)
113+
{
114+
throw new Exception("Cef.Shutdown has already been called, it's no longer possible to execute on the CEF UI Thread. Check CefThread.HasShutdown to guard against this execption");
115+
}
116+
117+
var taskFactory = UiThreadTaskFactory;
118+
119+
if (taskFactory == null)
120+
{
121+
//We don't have a task factory yet, so we'll queue for execution.
122+
return QueueForExcutionWhenUiThreadCreated(action);
123+
}
124+
125+
return taskFactory.StartNew(action);
126+
}
127+
}
128+
129+
/// <summary>
130+
/// Wait for CEF to Initialize, continuation happens on
131+
/// the CEF UI Thraed.
132+
/// </summary>
133+
/// <returns>Task that can be awaited</returns>
134+
private static Task QueueForExcutionWhenUiThreadCreated(Action action)
135+
{
136+
var tcs = new TaskCompletionSource<bool>();
137+
138+
EventHandler handler = null;
139+
140+
handler = (s, args) =>
141+
{
142+
Initialized -= handler;
143+
144+
//TODO: Should this call UiThreadTaskFactory.StartNew?
145+
action();
146+
147+
tcs.SetResult(true);
148+
};
149+
150+
Initialized += handler;
151+
152+
return tcs.Task;
153+
}
154+
103155
/// <summary>
104156
/// Wait for CEF to Initialize, continuation happens on
105157
/// the CEF UI Thraed.
@@ -115,6 +167,7 @@ private static Task<T> QueueForExcutionWhenUiThreadCreated<T>(Func<T> func)
115167
{
116168
Initialized -= handler;
117169

170+
//TODO: Should this call UiThreadTaskFactory.StartNew?
118171
var result = func();
119172

120173
tcs.SetResult(result);

0 commit comments

Comments
 (0)