Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#42 Fix Dispatcher Suspend Exception issue #43

Merged

Conversation

vkotenev-sc
Copy link
Contributor

For resolve issue "System.InvalidOperationException: Cannot perform this operation while dispatcher processing is suspended." make async subscribing instead of sync subscribing.

@Dirkster99
Copy link
Owner

Hi,

thanks for you feedback. I am just not sure what the problem is that you are trying to resolve. Can you please submit a small test application with test steps to better understand your problem.

As for the suggested change:
I am not sure whether replacing Invoke with BeginInvoke is such a great idea. Your suggestion does not make the code work async in the sense of the async keyword. It only schedules something to be executed in the context of the UI thread but its completey undetermined when the event will be invoked in the other context - which usually causes problems in other parts of the app.

I've been doing similar things in other applications just to find out that the card house comes down on a sequence of 'un-synchronist' things like this. So, I need to be sure this is the right solution and be convinced there is no better way to resolve this (which requires understanding the problem in the first place).

There might be a better solution to this but I cannot suggest one right now because I don't have this problem - so, I cannot test it, either :-(

My understanding is that you want to replace
Application.Current.Dispatcher.Invoke(new Action(() => Application.Current.Exit += new ExitEventHandler(Current_Exit)));
with

Application.Current.Dispatcher.BeginInvoke(new Action(() => Application.Current.Exit += new ExitEventHandler(Current_Exit)));

@scdmitryvodich
Copy link
Contributor

Maybe we could check status of Dispatcher first. Something like this:

var dispatcherType = typeof(Dispatcher);
var countField = dispatcherType.GetField("_disableProcessingCount", BindingFlags.Instance | BindingFlags.NonPublic);
var count = (int)countField.GetValue(Dispatcher.CurrentDispatcher);
var suspended = count > 0;

And in case if it is suspended use BeginInvoke.

@vkotenev-sc
Copy link
Contributor Author

Hi,
Thanks for your suggestion. I checked it, and it looks good. I updated PR.
Could you review it?

@Dirkster99 Dirkster99 merged commit 41321d0 into Dirkster99:master Jun 20, 2019
@Dirkster99
Copy link
Owner

Thanx guys :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants