Skip to content

Commit 16735be

Browse files
committed
Log the transitions to and from the UI thread.
1 parent 609b1a5 commit 16735be

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

Yafc.UI/Core/UiSynchronizationContext.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,13 @@ public override void Send(SendOrPostCallback d, object? state) {
4343

4444
public void GetResult() { }
4545
public bool IsCompleted => !Ui.IsMainThread();
46-
public void OnCompleted(Action continuation) => _ = ThreadPool.QueueUserWorkItem(ThreadPoolPost!, continuation); // null-forgiving: continuation is not null, so ThreadPoolPost doesn't need to accept a null.
46+
public void OnCompleted(Action continuation) {
47+
Logging.GetLogger<EnterThreadPoolAwaitable>().Debug("Exiting UI thread for action {id}.", continuation.GetHashCode()); // Stack trace enricher will add the stack trace here.
48+
_ = ThreadPool.QueueUserWorkItem(ThreadPoolPost!, () => { // null-forgiving: this action is not null, so ThreadPoolPost doesn't need to accept a null.
49+
Logging.GetLogger<EnterThreadPoolAwaitable>().Debug("Resuming action {id} on a thread-pool thread.", continuation.GetHashCode());
50+
continuation();
51+
});
52+
}
4753

4854
private static void ThreadPoolPost(object state) => ((Action)state)();
4955
}
@@ -53,7 +59,13 @@ public void GetResult() { }
5359

5460
public void GetResult() { }
5561
public bool IsCompleted => Ui.IsMainThread();
56-
public void OnCompleted(Action continuation) => Ui.DispatchInMainThread(MainThreadPost!, continuation); // null-forgiving: continuation is not null, so MainThreadPost doesn't need to accept a null.
62+
public void OnCompleted(Action continuation) {
63+
Logging.GetLogger<EnterMainThreadAwaitable>().Debug("Entering UI thread for action {id}.", continuation.GetHashCode()); // Stack trace enricher will add the stack trace here.
64+
Ui.DispatchInMainThread(MainThreadPost!, () => { // null-forgiving: this action is not null, so MainThreadPost doesn't need to accept a null.
65+
Logging.GetLogger<EnterMainThreadAwaitable>().Debug("Resuming action {id} on the UI thread.", continuation.GetHashCode());
66+
continuation();
67+
});
68+
}
5769

5870
private static void MainThreadPost(object state) => ((Action)state)();
5971
}

0 commit comments

Comments
 (0)