Is there an event for onAbort for the backgroundworker control?
I don't believe so. When you call CancelAsync on the BackgroundWorker you need to handle the cancellation yourself. This then gets passed through to the RunWorkerCompleted event, however you can tell that it was cancelled through e.Cancelled.
there is no specific event for "Cancel event".
You really should look at the MSDN documentation: http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.runworkercompleted.aspx
Related
I have implemented a WPF application using background worker in which I am assigning data from database to combobox.
I have used observable collection as ItemSource for combobox .
When I try to fill up the observable collection in DoWork method of background worker, UI is not getting updated but if I move same code to ProgressChanged or RunWorkerCompleted then it is working fine.
I want to know why the DoWork method is not updating the UI.Also the logic for updating UI should be in ProgressChanged or RunWorkerCompleted?
To quote MSDN : "You must be careful not to manipulate any user-interface objects in your DoWork event handler" - so in your DoWork method call ReportProgress event and update your ui in the ProgressChanged event handler
There are many online examples showing how to correctly utilise a BackgroundWorker. As you haven't bothered to show us your code, all we can do is guess, but I'm guessing that you simply haven't implemented your code properly. Not wanting to duplicate this code once again, I'd rather advise you to take a look at my answer to the Progress Bar update from Background worker stalling question here on Stack Overflow.
It clearly demonstrates how to implement a BackgroundWorker correctly.
Basically we have a huge project, and we have an event handler that sometimes is triggered and some others it isn't. I think this is because somewhere in the jungle of code, we're handling that event, so it doesn't bubble up to where we need it. Is there anyway to find out where is it being handled?
Thanks!
Did you try to use Snoop..... there is an event Tab that tells us where the event is bubbled to and where its handled...
I'm trying to implement a highly responsive UI for my MVVM application, so I've chosen to have all command handlers automatically execute in a BackgroundWorker so they don't block the UI.
But at the same time, I don't want the user to be able to execute the same command while it is still executing in the background. The solution seems obvious:
When Executed handler is invoked, have the CanExecute handler return false
Start the BackgroundWorker async
When BackgroundWorker finishes, have the CanExecute handler return true again.
Problem is, I need to notify WPF after step 1 and again after step 3 that CanExecute has changed and that it should be required. I know I can do it by calling CommandManager.InvalidateRequerySuggested, but that causes CanExecute handlers of all other commands to be requeried as well. Having a lot of commands, that's not good.
Is there a way to ask for requery of a specific command - i.e. the one that is currently being executed?
TIA
I think you only need to raise the ICommand.CanExecuteChanged event for these two cases, and the corresponding control should update its IsEnabled state.
gehho is right, just raise the CanExecuteChanged event. But honestly, I don't know your implementation, but if your CanExecute predicate is simple, I think it will be hard to notice that wpf is requerying. I can't believe you will notice a performance hit. Have you tested performance? I mean let's say you have 20 ICommands in a ViewModel(which is probably too much) if the ICommand.CanExecute points to a boolean field that you are setting to true and false based on the backgroundworker it should take less than nanoseconds. But if the CanExecute is pointed to a method that evaluates, then it may be longer, but I still doubt it would be noticeable.
But hey, I don't know your implementation.
Use MVVM Helper's `DelegatingCommand' which exposes RaiseCanExecuteChanged() for the Command.
So you can subscribe to RunWorkercompleted event on BackgroundWorker and depending on the kind of work logged (maybe you can return as part of the Result) you call Command.RaiseCanExecuteChanged() from ViewModel.
I tried creating one, but the BackgroundWorker in Window1 couldn't access the ProgressBar in Window2 once the reportProgress was activated, because "The calling thread cannot access this object because a different thread owns it".
Seems there's a lower level thread model I could use, but it also seems a lot more complicated.
You just need to get the ProgressBar disptacher.
You can access the ProgressBar with:
Window2.prograssbar.Dispatcher.Invoke(
() => /*the code for modifying the progressbar*/ );
In WPF, UI controls and properties may only be activated from the UI thread. In order to change the progress bar's value from a different thread, you can add a command to the GUI thread's dispatcher queue. You can do this by passing a delegate to the Dispatcher.Invoke() method. See the article at http://msdn.microsoft.com/en-us/magazine/cc163328.aspx for more details.
You need to look into Delegates
In WPF, I am calling
This.Dispatcher.BeginInvoke(DispatcherPriority.SystemIdle, mydelegete);
Is there a way to cancel later that BeginInvoke method?
Thanks
Dispatcher.BeginInvoke returns a DispatcherOperation object. If you retain a reference to it, you can use its Abort method to dequeue the operation if it hasn't started executing yet.