0

Here is my overridden method (which capture every pressed button on the keyboard on my form):

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
    if (btnTarget.Focused && keyData == Keys.Enter)
    {
        // doing something in another thread and waiting for its result (async awaiting)
        if (result)
           return true;
    }
    return base.ProcessCmdKey(ref msg, keyData);
}

But I do not know how to make it async-await. I tried to use Task.Run or SynchronizationContext.Current.Post or other tricks to make a clean async-await which waits for user interaction and then continue the rest.

So how to solve this issue in a clean way?

Inside Man
  • 4,194
  • 12
  • 59
  • 119
  • 1
    `var result = Task.Run(() =>{ "dosomething" }).GetAwaiter().GetResult();` this is how you would be allowed to call an async method in one which isnt marked as async, otherwise change do..https://stackoverflow.com/questions/35817558/why-does-c-sharp-allow-making-an-override-async/35817661 – Seabizkit Feb 10 '21 at 05:38
  • 2
    The basic way is to change `protected override bool` to `protected override async Task`. Of course, that will probably break other code, which you may need to make async as well, and when you change that.... etc. This is sometimes referred to as ["async all the way down"](https://stackoverflow.com/questions/29808915/why-use-async-await-all-the-way-down). – John Wu Feb 10 '21 at 05:47
  • 1
    @Seabizkit I can not use `.GetAwaiter().GetResult();`. it will cause `Cross-thread` exception :( – Inside Man Feb 10 '21 at 05:50
  • @InsideMan no it wont but, without seeing more code... only you know best then. why dont you just include the `// doing something in another thread and waiting for its result (async awaiting)` code... make it easier to be able to have information. – Seabizkit Feb 10 '21 at 05:57
  • @Seabizkit `.GetAwaiter().GetResult()` is a really good way to create deadlocks. – Aron Feb 10 '21 at 06:42
  • If the base method doesn't already return a `Task`, you cannot make the override `async`. See duplicate. – Peter Duniho Feb 10 '21 at 06:46
  • See also closely related https://stackoverflow.com/questions/35817558/why-does-c-sharp-allow-making-an-override-async – Peter Duniho Feb 10 '21 at 06:46
  • 2
    It seems by design, what you are asking isn't possible. `Form.ProcessCmdKey` is used for UI, the result, seems to be for bubbling. The assumption for bubbling would be that this must process extremely fast for your UI to remain responsive. async/await explicitly states that you are doing something that ISN'T fast nor responsive. – Aron Feb 10 '21 at 06:47
  • @Aron you will find you wrong and right... if he want to call an async in non async method then its correct if not then its not.... – Seabizkit Feb 10 '21 at 09:29
  • @Seabizkit calling that combo is basically the same as .Result(). Depending on the async context, synchronization context, IAsyncAwaitable, the phase of the moon, the colour of my socks, this could cause a deadlock, as the awaiter tries to marshall back to a some thread, whilst the thread is waiting for the awaiter to complete.... – Aron Feb 15 '21 at 19:57
  • @Aron https://medium.com/@deep_blue_day/long-story-short-async-await-best-practices-in-net-1f39d7d84050 "the phase of the moon, the colour of my socks, this could cause a deadlock" yes yes but you could just read more and then maybe you may know these moon phases and the colour of your socks. Enlighten is a rare gift. – Seabizkit Feb 15 '21 at 20:15

0 Answers0