Skip to content

DialogService: Fix thread is not associated with the Dispatcher#9306

Merged
henon merged 3 commits into
MudBlazor:devfrom
maxwell60701:Multi-thread-dialogservice-bugfix
Jul 12, 2024
Merged

DialogService: Fix thread is not associated with the Dispatcher#9306
henon merged 3 commits into
MudBlazor:devfrom
maxwell60701:Multi-thread-dialogservice-bugfix

Conversation

@maxwell60701

@maxwell60701 maxwell60701 commented Jul 3, 2024

Copy link
Copy Markdown
Contributor

Description

DialogService: The current thread is not associated with the Dispatcher.
Use InvokeAsync() to switch execution to the Dispatcher when triggering rendering or component state

How Has This Been Tested?

Unit test.

Type of Changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation (fix or improvement to the website or code docs)

Checklist

  • The PR is submitted to the correct branch (dev).
  • My code follows the code style of this project.
  • I've added relevant tests.

…atcher. Use InvokeAsync() to switch execution to the Dispatcher when triggering rendering or component state
@github-actions github-actions Bot added bug Unexpected behavior or functionality not working as intended PR: needs review labels Jul 3, 2024
@codecov

codecov Bot commented Jul 3, 2024

Copy link
Copy Markdown

Codecov Report

Attention: Patch coverage is 75.00000% with 1 line in your changes missing coverage. Please review.

Project coverage is 90.60%. Comparing base (28bc599) to head (02691f3).
Report is 336 commits behind head on dev.

Files Patch % Lines
src/MudBlazor/Services/Dialog/DialogService.cs 0.00% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##              dev    #9306      +/-   ##
==========================================
+ Coverage   89.82%   90.60%   +0.77%     
==========================================
  Files         412      403       -9     
  Lines       11878    12661     +783     
  Branches     2364     2447      +83     
==========================================
+ Hits        10670    11472     +802     
+ Misses        681      633      -48     
- Partials      527      556      +29     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@ScarletKuro ScarletKuro requested a review from henon July 3, 2024 16:32
@ScarletKuro

Copy link
Copy Markdown
Member

Hi,
Thanks for the PR.
Unfortunately, this would be a breaking change. You either need to call InvokeAsync without awaiting it, or this PR will have to stay on hold.

@ScarletKuro ScarletKuro added the breaking change This change will require consumer code updates (ex: removes/changes an API) label Jul 3, 2024
@maxwell60701

Copy link
Copy Markdown
Contributor Author

Thanks,But I guess it will cause other bugs if i call InvokeAsync without awaiting it

Comment thread src/MudBlazor/Components/Dialog/MudDialogProvider.razor.cs Outdated
@henon

henon commented Jul 4, 2024

Copy link
Copy Markdown
Contributor

Maybe we can make the change in an unbreaking way. How about adding public event Func<IDialogReference, Task>? DialogInstanceAdded; (Note: without the On prefix, which I think should not be added to events anyway) without changing the existing OnDialogInstanceAdded and marking it [Obsolete("Please use DialogInstanceAdded instead!")]

Of course both need to be fired for backwards compatibility. Either that or we don't change the event signature and fire it only after awaiting.

@maxwell60701

Copy link
Copy Markdown
Contributor Author

thanks for your suggestions,i will try to make the change,by the way,how to test it?

@henon

henon commented Jul 4, 2024

Copy link
Copy Markdown
Contributor

Not sure if it can be tested

@ScarletKuro

Copy link
Copy Markdown
Member

Do you have an example of code when you get that exception?

@maxwell60701

Copy link
Copy Markdown
Contributor Author

dialogReference = dialog.Show<T>(title, parameters, options);
dialog is IDialogService
open two windows in browser,each window open a same dialog,it reports
The current thread is not associated with the Dispatcher. Use InvokeAsync() to switch execution to the Dispatcher when triggering rendering or component state
the stacktrace points to the function StateHasChanged in function AddInstance,
it works for me if i write code like this
await InvokeAsync(() => dialogReference = dialog.Show<T>(title, parameters, options))

@maxwell60701

Copy link
Copy Markdown
Contributor Author

I found the problem,the dialog parameter is a singleton,it causes some errors when opening multiple windows

@ScarletKuro

Copy link
Copy Markdown
Member

I found the problem,the dialog parameter is a singleton,it causes some errors when opening multiple windows

Do you mean if you reuse existing DialogParameters it will cause problem?

@maxwell60701

Copy link
Copy Markdown
Contributor Author

yes

@ScarletKuro

Copy link
Copy Markdown
Member

Maybe we can make the change in an unbreaking way. How about adding public event Func<IDialogReference, Task>? DialogInstanceAdded; (Note: without the On prefix, which I think should not be added to events anyway) without changing the existing OnDialogInstanceAdded and marking it [Obsolete("Please use DialogInstanceAdded instead!")]

Of course both need to be fired for backwards compatibility. Either that or we don't change the event signature and fire it only after awaiting.

Did in a non breaking fashion. I tried to write a test, that would create dialog in a non UI thread but in bUnit it still passes regardless, so I guess let's continue without a test.

@ScarletKuro

Copy link
Copy Markdown
Member

I tried to write a test, that would create dialog in a non UI thread but in bUnit it still passes regardless, so I guess let's continue without a test.

Nvm, I wrote a test that seems to work. If we remove the return InvokeAsync(StateHasChanged); it will crash somewhere and the invoked = true will not fire.

@ScarletKuro ScarletKuro changed the title #bug DialogService The current thread is not associated with the Disp… DialogService: Fix thread is not associated with the Dispatcher Jul 12, 2024
@ScarletKuro

Copy link
Copy Markdown
Member

@ScarletKuro ScarletKuro removed the breaking change This change will require consumer code updates (ex: removes/changes an API) label Jul 12, 2024
@henon henon merged commit 9449650 into MudBlazor:dev Jul 12, 2024
@henon

henon commented Jul 12, 2024

Copy link
Copy Markdown
Contributor

Awesome! Thank you both!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Unexpected behavior or functionality not working as intended

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants