背景工人如何处置和确认 [英] Backgroud worker how to dispose and confirm

查看:77
本文介绍了背景工人如何处置和确认的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个关于BackgroundWorker(BGW)的要求

1.我已经启动了一个BGW并调用bg.dispose()方法,之后如果我们检查bg.为什么会发生这种情况,它应该使期望的结果错误.

2.如果不使用bg.dispose()杀死BGW,那么如何杀死并确认呢?

3.如何在特定时间后处置BGW(长时间运行的进程会在5秒后关闭BGW)?

请使用下面的代码来帮助我.

命名空间ConsoleApplication2
{
    班级计划
    {
        静态void Main(string [] args)
        {
            BackgroundWorker bg = new BackgroundWorker();
            bg.DoWork + =新的DoWorkEventHandler(bgw_DoWork);
            bg.RunWorkerCompleted + =新的RunWorkerCompletedEventHandler(bgw_RunWorkerCompleted);
            bg.WorkerSupportsCancellation = true;
            bg.RunWorkerAsync();
            Console.WriteLine("BG已启动");
            Thread.Sleep(1000);
          
            bg.Dispose();
            如果(bg == null)
            {
                Console.WriteLine("false");
            }
            bool bgworking = bg.IsBusy;
            Console.WriteLine(bgworking);
            Console.ReadLine();
        }

        私人静态无效bgw_RunWorkerCompleted(对象发送者,RunWorkerCompletedEventArgs e)
        {
            
        }

        私有静态无效bgw_DoWork(对象发送者,DoWorkEventArgs e)
        {
            Thread.Sleep(50000);
        }
    }
} 

还可以在图像下方找到运行时代码:

请帮助.这样我们就可以在多线程中使用这个概念.

解决方案

处置不会立即发生,但会在处置对象后立即发生


I am having three quires regarding BackgroundWorker(BGW)

1. I have started a single BGW and called bg.dispose() method after that if we check bg.Isbusy it is giving true. Why does this happen it should give false what is expected.

2. If bg.dispose() is not used to kill BGW then how to kill and confirm this?

3. How to dispose a BGW after particular time (for long running processes close BGW after 5 sec)?

Please use the below code to help me out .

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            BackgroundWorker bg = new BackgroundWorker();
            bg.DoWork += new DoWorkEventHandler(bgw_DoWork);
            bg.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgw_RunWorkerCompleted);
            bg.WorkerSupportsCancellation = true;
            bg.RunWorkerAsync();
            Console.WriteLine("BG started");
            Thread.Sleep(1000);
          
            bg.Dispose();
            if (bg == null)
            {
                Console.WriteLine("false");
            }
            bool bgworking = bg.IsBusy;
            Console.WriteLine(bgworking);
            Console.ReadLine();
        }

        private static void bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            
        }

        private static void bgw_DoWork(object sender, DoWorkEventArgs e)
        {
            Thread.Sleep(50000);
        }
    }
}

also find below the image for run time code:

Please help. So that we can use this concept in multithreading.

解决方案

Disposing will not happen straight away but will happen soon after the object has been disposed


这篇关于背景工人如何处置和确认的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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