如何跟踪和中止后台工作任务

编程入门 行业动态 更新时间:2024-10-11 03:18:58
本文介绍了如何跟踪和中止后台工作任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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.

更多推荐

如何跟踪和中止后台工作任务

本文发布于:2023-11-13 04:29:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1583430.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:工作任务   后台

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!