这是indexloc提供的服务,不要输入任何密码
Skip to content

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 vkotenev-sc force-pushed the DispatcherSuspendException branch from 137b6f9 to 14409d2 Compare June 20, 2019 12:01
@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?

@vkotenev-sc vkotenev-sc force-pushed the DispatcherSuspendException branch from 14409d2 to c2cf465 Compare June 20, 2019 13:18
@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