Is it possible to prevent the WPF CommandManager from executing it's attached CanExecute delegates?
I have an Unhandled Exception Handler routine that displays an error window for the user. I have a situation at the moment where an exception is being thrown as part of a CanExecute chain. This invokes the exception handler, which displays the window. At this point, CommandManager kicks in and requeries the CanExecute, throwing another exception, invoking the handler, showing another window, requerying CanExecute, throwing another exception... etc. etc. etc.
I somehow need to prevent this rerunning of CanExecute. I have tried to simply ignore subsequent errors in the handler, but this means that the exception information in the error window is blank.
The best resource is the to look off is from the ICommand Interface which has the CanExecuteChanged Event. Operations which subscribe to that event will be notified to disable itself.
It is unclear what your structure is setup as, but I would surmise that the originating operation could fire this event upon starting the process, then when it is done fire the event again to announce that other command processed can run.
Example of the originating command operation to notify others to inspect status before turning on their commands:
private void DoTheCommand()
{
_isProcessingCabExecute = false; // Turn off all related for this
// command (is Execute).
_opCommand.RaiseCanExecuteChanged(); // Inform all subscribed to check status
// and disable where possible.
// The operation
_isProcessingCabExecute = true; // All done turn it back on
_opCommand.RaiseCanExecuteChanged(); // Resume operations.
}
Related
The Whole GUI is loaded. After clicked on certain button, the GUI is not responsive: any buttons are no response. If I switch to other app and switch back, the GUI is OK immediately.
Behind the clicked button, it is a handle sending requests to back end. I debugged app, and data has always been returned.
Since the app is huge and I'm not familiar with it, I cannot extract a simple model of sending requests and processing data.I wonder possible causes, since I have no idea now.
Best regards,
-----Add More-----
The back end request sending is in a threadpool thread; when getting data, no UI controls are updated directly. Only presenters (view model) are updated, which are binding to UI control.
I think as sa-ddam213 have suggested and I too believe that you are executing a code block in background Thread or Task.
I think problem is that
- On button click, start execution in background.
- You have kept a flag for checking if background process is running for CanExecute() for the Button's Command.
- UI checks CanExecute() for the Button's Command for a while then does not as mentioned in another question here - Is Josh Smith's implementation of the RelayCommand flawed?.
- Process returns in background.
- UI does not knows the background process is completed and as it has stopped checking the CanExecute() for the Button's Command, UI will not come to know itself.. (you need to tell it)
- Therefore UI is not responding but when user will click somewhere in the application or do an in-out as you said, it will back again.
Solution
- By using InvalidateRequerySuggested method you can invoke the command requery on UI thread so UI will recheck the CanExecute() for each Control and/or Button Command.
// Forcing the CommandManager to raise the RequerySuggested event
CommandManager.InvalidateRequerySuggested();
The InvalidateRequerySuggested method forces the CommandManager to raise the RequerySuggested event. The RequerySuggested event informs a command source to query the command it is associated with to determine whether or not the command can execute.
For more refer:
- How does CommandManager.RequerySuggested work?
- MSDN - CommandManager
- MSDN - CommandManager.InvalidateRequerySuggested Method
Its just a wild guess but try dropping the priority of the logic inside your click event
Example
private void Button_Click(object sender, RoutedEventArgs e)
{
Dispatcher.BeginInvoke(DispatcherPriority.Background, (Action)delegate
{
// all your code here.
});
}
I have a WPF app that has a WindowsFormsHost in which a 3rd party WinForms control is hosted. Sometimes, because of a bug in the 3rd party WinForms control I get a NullReferenceException.
Although I had set up a DispatcherUnhandledException handler I can't catch the exception there and continue the execution.
Only in the AppDomain.CurrentDomain.UnhandledException handler I can "catch" it but I cannot do much from then on, the application simply exits.
Then I found a stackoverflow question (with an answer; can't find it now) which stated to try to do this :
System.Windows.Forms.Application.ThreadException += (sender, args) => { /* Catch it here */};
System.Windows.Forms.Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
That didn't help either because the (inline) handler did not get called ever.
Am I going the wrong way?
I am not sure why that handler never gets called in your case, probably because the exception was not thrown on a Windows Forms thread (the thread on which your forms and controls have been created), but generally, setting up handlers for DispatcherUnhandledException, AppDomain.UnhandledException and/or Application.ThreadException doesn't allow you to prevent the termination of the process. They are event handlers, not exception handlers. The unhandled exception is still an unhandled exception even if you have setup those event handlers. Usually they are used to add some final logging or present a meaningful message to the user. Once an unhandled exception is raised there is nothing you can do to prevent the termination of the process.
Maybe the exception thrown is a CSE (Corrupted State Exception). To handle these kinds of exceptions, use [HandleProcessCorruptedStateExceptions] attribute for that function.
When i open a window it first has false title(still not updated) after several seconds title updates, is there any function or a practical solution to wait for this so then i could send a message somewhere when this happens...
What i have is a cbt hook that gets wparam when window activates, i need to wait for this wparam to become real title(not the fake one)
I can put a sleep for every activation but that would slow everything down.
Provided your target window is following the standard method to update its caption, you can add a hook to catch the precise moment when the caption has changed.
To do this, you will need to add a windows message return hook with SetWindowsHookEx, passing it a CallWndRetProc handler from your code to receive notices upon completion of windows messages by the target window's message handler.
SetWindowsHookEx() # MSDN
CallWndRetProc # MSDN
The windows message you need to trap is WM_SETTEXT, which is sent to trigger the change in the default text/caption for a window.
WM_SETTEXT # MSDN
If you would like to test this in isolation, WM_SETTEXT is an implicit message sent upon calling SetWindowText to change the window text/caption.
SetWindowText() # MSDN
Also, you might also be able to get rid of your cbt hook, as WM_ACTIVATE, WM_MOUSEACTIVATE, and WM_SETFOCUS can be trapped with the same hook used to trap WM_SETTEXT. These messages cover various levels of activation activity and type for windows.
WM_ACTIVATE # MSDN
WM_MOUSEACTIVATE # MSDN
WM_SETFOCUS # MSDN
Last, if the target window is a window you create and control in your own process, you could simply intercept those windows messages instead of using hooks. If it is impossible to use your available frameworks to intercept those messages, SetClassLongPtr is also another alternative, which may be used to implement a subclass for that type of window to allow you to intercept window messages. If you use this method, you would delegate all calls to the original windows message handler, and only act during a return from the original procedure upon receiving a message of interest.
SetClassLongPtr() # MSDN
WindowProc # MSDN
Here's how I would do it in c#:
Set up a listener for the textchanged event.
this.something.TextChanged += new System.EventHandler(something_TextChanged);
Then setup the code that will do whatever you need when the text changes
void something_TextChanged(object sender, System.EventArgs e)
{
// your code
}
I have a window with a single button within.
The code-behind is
private void Button_Click(object sender, RoutedEventArgs e)
{
Trace.TraceInformation("Button ThreadId: {0}", Thread.CurrentThread.ManagedThreadId);
Thread w = new Thread((ThreadStart) Worker);
w.SetApartmentState(ApartmentState.STA); // removing/adding this doesn't make effect
w.Start();
MessageBox.Show("Direct");
}
void Worker()
{
Trace.TraceInformation("Worker ThreadId: {0}", Thread.CurrentThread.ManagedThreadId);
this.Dispatcher.Invoke((Action)delegate
{
Trace.TraceInformation("Invoked ThreadId: {0}", Thread.CurrentThread.ManagedThreadId);
MessageBox.Show("Invoked");
});
}
Clicking the button results in 2 message boxes.
At the same time, trace shows same numbers for Button ThreadId and Invoked ThreadId.
The Dispatcher always carries out work on the GUI thread. That is why your ThreadId's match up. You are asking the GUI thread- "what's your ThreadId?" and then performing some work through the Dispatcher, which is again going to the GUI thread.
After taking a walk I understood what was happening.
Here is my explanation idea what's wrong with the code (or the reason the question was posted).
Button_Click is executed in the Dispatcher thread. The Dispatcher thread, as I know, is a single one for a window and its children.
Even if Button_Click takes more than a second (long enough), and user manages to click the button again (or somehow else interact with UI), the next Button_Click (or other appropriate handler) is not executed immediately, but placed in the dispatching queue.
Dispatcher.Invoke executes a delegate in the UI thread. Invoke, I suppose, sends a message to the delegate GetMessage() loop and blocks the calling thread till the message completes.
I expected the delegate to begin execution only after Button_Click exits.
MessageBox.Show() is a blocking call. Next statement won't be executed before the user clicks 'Ok'.
What is actually happening is the Dispatcher actually distinguishes between different windows and knows that Button_Click has called a modal dialog and therefore any interaction with the window should produce a beep and the message box should flash.
But is goes on dispatching messages. After all, this why all user clicks are translated into Button.Click messages and the message box gets closed.
This is why the invoked delegate is executed before Button_Click exits.
The invoked delegate breaks into the Button_Click.
P.S. As you see in the code, the delegate also calls MessageBox.Show(). This leads to a new message box which is modal to the previous one. I noted I cannot click 'Ok' on the "Direct" msgBox before "Invoked" one.
I'm new to this WPF Dispatcher stuff so downvote me if I am totally wrong, but it looks like your call to this.Dispatcher is really your Window's (UI thread) Dispatcher. Thus, any code will be executed by the UI thread, not your application instantiated thread.
[Edit]
Here is the console output I received from running the above code
DispatcherQuestion.vshost.exe Information: 0 : Button ThreadId: 9 // UI Thread
DispatcherQuestion.vshost.exe Information: 0 : Worker ThreadId: 11 // Thread w
DispatcherQuestion.vshost.exe Information: 0 : Invoked ThreadId: 9 // UI Thread
ThreadID 9 is the user interface thread. Your Button and your Invoke call are both being performed, as designed, by the UI thread.
I found this article today on MSDN that really clarified things about the WPF Threading/Dispatcher model.
I have a databound WPF CheckBox control that appears to be eating exceptions thrown by the corresponding property setter when the value is toggled in the UI. I know this can happen if I provide a ExceptionValidationRule on the Binding instance, but I double checked that the ValidationRules for the Binding instance has count zero. I also checked the call stack for intervening exception handlers and none exist. Nonetheless, the thrown exception does not bubble to the top and produce a crash in the app as I would expect.
If I throw an exception from a button click handler in the same UI, the exception does bubble up and cause an application crash, ruling out some sort of global exception handler.
Any ideas?
Thanks!
To add to itowlson's answer, the Binding class provides the UpdateSourceExceptionFilter property, which allows you to provide logic that runs when an exception occurs updating the source. It is used in conjunction with the ExceptionValidationRule class, and allows you to do something other than adding a ValidationError when the update fails.
No, this is expected behaviour: the WPF data binding infrastructure catches exceptions caused by saving the value from a binding target back to the source. I suspect this is because there is no way for the app to set up an exception handler around the save operation (because it is called from WPF code rather than from app code), so if WPF did not do this, the app would crash without the chance to handle the exception.
(By contrast, in a button click handler, you are writing the code so you do have the opportunity to handle exceptions. Therefore WPF thinks it's okay to let the exception propagate if you decide not to handle it.)