Click here to Skip to main content
15,884,176 members
Home / Discussions / C#
   

C#

 
GeneralRe: How can I get handle (IntPtr) to a "sub"-form in another application? Pin
arnold_w1-Aug-20 6:07
arnold_w1-Aug-20 6:07 
GeneralRe: How can I get handle (IntPtr) to a "sub"-form in another application? Pin
Richard Andrew x641-Aug-20 6:10
professionalRichard Andrew x641-Aug-20 6:10 
GeneralRe: How can I get handle (IntPtr) to a "sub"-form in another application? Pin
arnold_w1-Aug-20 6:15
arnold_w1-Aug-20 6:15 
GeneralRe: How can I get handle (IntPtr) to a "sub"-form in another application? Pin
Richard Andrew x641-Aug-20 6:21
professionalRichard Andrew x641-Aug-20 6:21 
GeneralRe: How can I get handle (IntPtr) to a "sub"-form in another application? Pin
arnold_w1-Aug-20 6:51
arnold_w1-Aug-20 6:51 
GeneralRe: How can I get handle (IntPtr) to a "sub"-form in another application? Pin
Richard Andrew x641-Aug-20 7:05
professionalRichard Andrew x641-Aug-20 7:05 
QuestionAzure Active Directory question Pin
pkfox31-Jul-20 0:50
professionalpkfox31-Jul-20 0:50 
AnswerRe: Azure Active Directory question Pin
Richard Deeming4-Aug-20 0:01
mveRichard Deeming4-Aug-20 0:01 
pkfox wrote:
It must be the UI thread in Winforms
Correct. Your async method tries to return to the current "execution context" after the await. In a WinForms app, when the async method was called from the UI thread, it tries to return to the UI thread. But you've blocked the UI thread by trying to execute the async method synchronously:
C#
this.GetAccessToken().GetAwaiter().GetResult(); // Blocks the UI thread
Therefore, the async method can never complete.

If you're not accessing the UI from your GetAccessToken method, you can add .ConfigureAwait(false) to your ExecuteAsync call, so that the rest of the method can complete on a thread-pool thread:
C#
this.Result = await this.App.AcquireTokenForClient(this.ResourceIds).ExecuteAsync().ConfigureAwait(false);
Otherwise, you'll need to find a way to avoid blocking the UI thread. You'd need to make the calling method async. However, you want to avoid async void methods[^]. A simple solution is to move the code into a task-returning async method, and assign the returned task to a "discard" variable to avoid the compiler warning.

Old code, don't use:
C#
private void ButtonClick(object sender, EventArgs e)
{
    // Some code...
    this.GetAccessToken().GetAwaiter().GetResult();
    // Some more code...
}
Bad fix, don't use:
C#
private async void ButtonClick(object sender, EventArgs e)
{
    // Some code...
    await this.GetAccessToken();
    // Some more code
}
Better fix:
C#
private void ButtonClick(object sender, EventArgs e)
{
    _ = SomeMethodAsync();
}

private async Task SomeMethodAsync()
{
    // Some code...
    await this.GetAccessToken();
    // Some more code
}

ConfigureAwait FAQ | .NET Blog[^]



"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer

GeneralRe: Azure Active Directory question Pin
pkfox4-Aug-20 3:36
professionalpkfox4-Aug-20 3:36 
GeneralRe: Azure Active Directory question Pin
Richard Deeming4-Aug-20 3:40
mveRichard Deeming4-Aug-20 3:40 
GeneralRe: Azure Active Directory question Pin
pkfox4-Aug-20 5:38
professionalpkfox4-Aug-20 5:38 
GeneralRe: Azure Active Directory question Pin
Richard Deeming4-Aug-20 5:48
mveRichard Deeming4-Aug-20 5:48 
GeneralRe: Azure Active Directory question Pin
pkfox4-Aug-20 6:52
professionalpkfox4-Aug-20 6:52 
GeneralRe: Azure Active Directory question Pin
Richard Deeming4-Aug-20 23:14
mveRichard Deeming4-Aug-20 23:14 
GeneralRe: Azure Active Directory question Pin
pkfox5-Aug-20 3:12
professionalpkfox5-Aug-20 3:12 
GeneralRe: Azure Active Directory question Pin
pkfox4-Aug-20 23:12
professionalpkfox4-Aug-20 23:12 
QuestionHow to change the aspect ratio of an Windows media player by using C# Pin
Member 1488657531-Jul-20 0:25
Member 1488657531-Jul-20 0:25 
AnswerRe: How to change the aspect ratio of an Windows media player by using C# Pin
Gerry Schmitz31-Jul-20 7:32
mveGerry Schmitz31-Jul-20 7:32 
QuestionName Generator Pin
Brandnoob30-Jul-20 8:04
Brandnoob30-Jul-20 8:04 
AnswerRe: Name Generator Pin
Gerry Schmitz30-Jul-20 8:10
mveGerry Schmitz30-Jul-20 8:10 
AnswerRe: Name Generator Pin
OriginalGriff30-Jul-20 8:55
mveOriginalGriff30-Jul-20 8:55 
QuestionExecuteScalar to cshtml view Pin
Member 1490312930-Jul-20 5:30
Member 1490312930-Jul-20 5:30 
AnswerRe: ExecuteScalar to cshtml view Pin
Gerry Schmitz30-Jul-20 8:02
mveGerry Schmitz30-Jul-20 8:02 
GeneralRe: ExecuteScalar to cshtml view Pin
Member 1490312930-Jul-20 11:42
Member 1490312930-Jul-20 11:42 
GeneralRe: ExecuteScalar to cshtml view Pin
Dave Kreskowiak30-Jul-20 12:47
mveDave Kreskowiak30-Jul-20 12:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.