vs-threading icon indicating copy to clipboard operation
vs-threading copied to clipboard

Call JoinableTaskFactory(new JoinableTaskContext()).RunAsync() in InitializeComponent caused designer error

Open ctechid opened this issue 1 year ago • 1 comments

Bug description

Call JoinableTaskFactory(new JoinableTaskContext()).RunAsync() in InitializeComponent caused designer error

Repro steps

Call JoinableTaskFactory(new JoinableTaskContext()).RunAsync() in InitializeComponent caused designer error

    private void InitializeComponent()
    {
         ...
         TestButton.Click += (sender, e) => _ = new JoinableTaskFactory(new JoinableTaskContext()).RunAsync(() =>  TestButton_ClickAsync(sender, e));
         ...
    }

Expected behavior

The designer does not error when the event is used in InitializeComponent().

Actual behavior

The designer error when the event was called inside InitializeComponent().

  • Version used: Microsoft.VisualStudio.Threading 17.8.14
  • Application (if applicable):

Additional context

image

SOLUTION Move the event outside InitializeComponent() no error appears in the designer.

ctechid avatar Feb 08 '24 14:02 ctechid

JoinableTaskContext is supposed to be a singleton in a process. You shouldn't be creating one in an event handler just to call RunAsync.

It looks like you're going to all the trouble just to invoke an async method from an event handler. But what are you gaining for your trouble? Why not just do this:

 TestButton.Click += (sender, e) => _ = TestButton_ClickAsync(sender, e);

AArnott avatar Feb 22 '24 02:02 AArnott

JoinableTaskContext is supposed to be a singleton in a process. You shouldn't be creating one in an event handler just to call RunAsync.

It looks like you're going to all the trouble just to invoke an async method from an event handler. But what are you gaining for your trouble? Why not just do this:

 TestButton.Click += (sender, e) => _ = TestButton_ClickAsync(sender, e);

Thank You, What you said also works well. I used the previous one because it was recommended when a warning appeared in the error list window of VS2022

ctechid avatar Mar 04 '24 09:03 ctechid