如何跟踪和中止后台工作任务 [英] How do I trace and abort backgroundworker task

查看:59
本文介绍了如何跟踪和中止后台工作任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HI,



我有以下情况:



*我正在研究WPF应用程序,使用导航框架和XAML页面概念在页面之间导航。

*在卸载页面时我创建了一个后台工作程序并以异步方式启动它





  //  创建背景线程 
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);





我有一些事件的实现。



  private   void  bwAsync_DoWork( object  sender,DoWorkEventArgs e)
{
// 一些代码,每5秒检查一次状态更新数据库,并在MainWindow的通知中心部分显示状态消息。
// (通常是一个长时间运行的过程)
}

private void bwAsync_RunWorkerCompleted( object sender,RunWorkerCompletedEventArgs e)
{
// 一些代码
}

private void bwAsync_ProgressChanged( object sender,ProgressChangedEventArgs e)
{
// 部分代码
}







我想跟踪后台工作线程并从任何其他页面中止它,我也不愿意保留页面的对象(启动工作人员任务)处于活动状态。



在我的应用程序中,创建一个可以放置worker对象的全局类的提供也是不可能的。



有没有办法从线程池中跟踪工作任务?



请建议。



谢谢,

Abhishek。

解决方案

让Backgroundworker报告你必须调用的进度 m_AsnycWorker1 .ReportProgress(int percentProgress); 来自withing bw_Async_DoWork。



要取消你的Backgropundworker从外面调用m_AsnycWorker1.CancelAsync() bw_Async_DoWork。

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


 // 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.

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.

解决方案

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.


这篇关于如何跟踪和中止后台工作任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆