Click here to Skip to main content
15,888,018 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
HI,

I've following scenario:

* I'm working on WPF application, with Nav-Frames and XAML Pages concept for navigation between pages.
* On Unload of a page I've created a backgroundworker and started it asynchronously


C#
 // Create a background thread
BackgroundWorker m_AsyncWorker = new BackgroundWorker();
m_AsyncWorker.WorkerReportsProgress = true;
m_AsyncWorker.WorkerSupportsCancellation = true;
m_AsyncWorker.ProgressChanged += new ProgressChangedEventHandler(bwAsync_ProgressChanged);
m_AsyncWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bwAsync_RunWorkerCompleted);
m_AsyncWorker.DoWork += new DoWorkEventHandler(bwAsync_DoWork);

m_AsyncWorker.RunWorkerAsync(processID);


and I've some implementations of events.

C#
private void bwAsync_DoWork(object sender, DoWorkEventArgs e)
{
    //some code that keeps checking Databse for a status update every 5 secs and displays the status message at notification center part of MainWindow.
    //(Generally a long running process)
}

private void bwAsync_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    //some code
}

private void bwAsync_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    //some code
}



I want to trace the backgroundworker thread and abort it from any other page and also I'm not willing to keep the object of the page (where the worker task was started) to be active.

In my application the provision of creating a global class where I could put up the worker object is also not possible.

Is there any way to trace a worker task from threadpool or something?

Kindly suggest.

Thanks,
Abhishek.
Posted

1 solution

to make Backgroundworker report its progress you have to call m_AsnycWorker1.ReportProgress(int percentProgress); from withing bw_Async_DoWork.

To Cancel your Backgropundworker call m_AsnycWorker1.CancelAsync() from outside bw_Async_DoWork.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900