I have a BackgroundWorker DoWork function as follows
private void WorkerGetFeedData(object sender, DoWorkEventArgs args)
{
_feed.FetchUserData(_userNameCollection);
}
The FetchUserData is a function in another class(whose object is _feed) in another project in the same solution. The data fetch process takes considerable time and I’d like for the user to be able to cancel the process if necessary. How do I convey a cancel operation from the user to a function call elsewhere and just stop it?
You can use
BackgroundWorker.CancelAsyncmethod. Here’s more info with example: MSDNTo be more exact to your problem, pass the worker to
FetchUserData. It is the sender parameter. Then in theFetchUserDatafunction you can check if the flagBackgroundWorker.CancellationPendingis set and finish your method.And the WorkerGetFeedData method: