【问题标题】:BackgroundWorker, MethodsBackgroundWorker,方法
【发布时间】:2014-01-24 16:37:18
【问题描述】:

所以把标准的后台工人废话放在一边。我正在研究如何使用后台工作人员多一点(Worker DoWork { add some commands for it to do })

所以这就是我到目前为止想出的。在这种情况下,它会做一些随机的 WMI 工作

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

    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 Method 中生成的类中的任何内容 这是DoWork的方法

    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。你认为 Struct 是最好的选择吗?
  3. 如何让 ViewModel 知道后台工作程序已完成并更新其公开的属性以供视图更新。

还是我离基地太远了?

【问题讨论】:

标签: c# backgroundworker


【解决方案1】:
  1. 是的,这个概念是可能的。您基本上可以让后台工作人员执行和访问您可以在后台工作人员方法之外执行和访问的任何操作;关键点是没有两个线程应该同时处理没有明确布局的对象,这就是为什么例如你不应该从后台工作线程的工作线程访问你的 UI(UI 通常不断也被 UI 线程的内部方法使用)。

  2. 你使用结构还是类与线程无关;属于给定线程的不是数据,而是操作。唯一的区别可能是,当传递一个结构时,将创建该结构的副本,并且您可以继续在后台工作线程中使用您的结构变量 - 但即便如此,对于在其内部调用回调的方法也是如此在进一步修改本地结构/对象之前拥有自己的线程,所以这个决定实际上与线程无关。

  3. 一旦后台工作人员完成其工作并确保在 UI 线程上调用它,调用一个 UI 方法会导致 UI 更新。后一部分可以通过多种方式完成,具体取决于 UI 工具包,UI 控件可能会提供调度程序对象或允许与 UI 线程同步调用的类似对象。

【讨论】:

  • 甜蜜。很高兴知道我离得太远了,我写了这个概念,希望它可以使用。我的问题是我的后台工作人员和我的视图模型/视图是分开的。假设。如果我使用接口。并且在视图模型中具有与后台工作类相同的公开属性,这会更新我的绑定属性从而更新视图吗?
  • @NewBee:我不确定我是否理解您希望如何连接后台工作人员和视图模型的属性,但是对您的视图模型的任何调用都会导致绑定评估UI 本质上意味着您访问 UI,无论您是否通过视图模型实现的接口访问属性。因此,您需要将任何此类调用与 UI 线程同步,以免发生不希望的事情。
  • 好吧,Background Worker 类是由 ViewModel 发起的,因此 ViewModel 将能够访问 BackgroundWorker 类中的任何属性/方法。但这似乎并不适用。我可以从 backgroundworker 类中的公开属性中回收数据没有问题。那么我的 ViewModel 怎么知道 backgroundworker 类已经完成了数据收集呢?
  • @NewBee:要么设计你的后台工作方法,使其具有对视图模型的引用,并且可以在其工作结束时调用视图模型的方法,要么具有view-model 用后台worker的RunWorkerCompleted event注册这样一个方法。无论哪种情况,您都必须使该视图模型方法线程安全,即确保它与 UI 线程同步。
猜你喜欢
  • 1970-01-01
  • 2011-01-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多