首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >BackgroundWorker,方法

BackgroundWorker,方法
EN

Stack Overflow用户
提问于 2014-01-24 16:37:18
回答 1查看 260关注 0票数 2

所以把标准的背景工作者垃圾放在一边。我在研究如何使用后台工作人员,而不是(Worker DoWork {添加一些命令让它做})

这就是我到目前为止想出来的。在这个场景中,它执行一些随机的WMI内容。

视图/视图模型/模型--模型称为ManagementModel

代码语言:javascript
运行
复制
    public void Start(String Args)
    {
        if (!Worker.IsBusy)
        {
            //Objectivly here you can spawn an instance of a class and perform a method.. or put the function in the background worker itself depending on what you want the thing to do
            ManagementModel BackgroundManagementTask = new ManagementModel();
            Worker.RunWorkerAsync(BackgroundManagementTask); //Starts the background worker.
            //If you specify the class to shove into the background worker, you need to put the commands of what to do in the DoWork section.
        }
    }

我有一个方法,不管在Start方法中生成的类中的什么东西,这里都是DoWork的方法

代码语言:javascript
运行
复制
    private void Workers_DoWork(object sender, DoWorkEventArgs Args)
    {
        //This is run on a completly seperate thread, you cannot make any changes to anything outside in this method.
        //you instead pass data through the Args.ReportProgress or Args.Result
        if (Worker.CancellationPending)
        {
            Worker.ReportProgress(100, "Cancelled By User");
            return;
        }
        else
        {
            //if you passed a method here, you will need to convert Args.Arguments back to what ever you passed it in as
            ManagementModel Internal = Args.Argument as ManagementModel;

            //bunch of stuff in the class that already works
            Internal.ComputerName = System.Environment.MachineName;
            Internal.Connect();
            Internal.ChangeWmiLocation("cimv2",null);
            ManagementObjectCollection ResultCollection = Internal.Query("Win32_Process");
            ClassProperties ResultProperties = Internal.DisplayProperties;

            //now return the results to the program thread
            Args.Result = ResultProperties;
            //now you need to deal with the data in the WorkerCompleted Event

            Worker.ReportProgress(100, "Completed");
            Thread.Sleep(60); //this is required at the end of each iteration of function
        }
    }

所以我的简单问题是。

  1. 这个概念是否可能,我是否可以启动整个类的实例并将其抛到后台处理程序中,并让后台工作人员在类内执行方法和功能?
  2. 如果我必须将数据传回UI。你认为结构会是最好的方法吗。
  3. 我如何让ViewModel知道后台工作人员已经完成,并更新其公开的属性,以便视图更新。

还是我离基地很远?

EN

回答 1

Stack Overflow用户

发布于 2014-01-24 16:46:46

  1. 是的,这个概念是可能的。基本上,您可以让后台工作人员执行和访问后台工作人员方法之外的任何操作和访问;关键的一点是,不应该有两个线程同时使用未显式布局的对象,这就是为什么不应该从后台工作者的工作线程访问UI ( UI通常也经常被UI线程的内部方法使用)。
  2. 是否使用结构或类与线程无关;不是属于给定线程的数据,而是操作。唯一的区别可能是,当传递一个struct时,将创建该结构的一个副本,并且您可以在后台辅助线程中继续使用struct变量--但即便如此,在进一步修改本地struct /对象之前在它自己的线程中调用回调的方法也是如此,因此这个决定实际上与线程无关。
  3. 在后台工作完成后调用UI方法,该方法将导致UI的更新,并确保在UI线程上调用它。后一部分可以通过各种方式完成,取决于UI工具包,UI控件可能提供dispatcher对象或类似的东西,允许与UI线程同步调用。
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21337803

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档