c# - BackgroundWorker,方法

标签 c# backgroundworker

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

这就是我到目前为止的想法。在这种情况下,它会做一些随机的 WMI 事情

View / View 模型/模型 该模型称为 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 方法中生成的类中的任何内容 这是 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 知道后台工作程序已完成并更新其公开的属性以更新 View 。

还是我离基地太远了?

最佳答案

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

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

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

关于c# - BackgroundWorker,方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21337803/

相关文章:

c# - BackgroundWorker:从 DoWorkEventHandler 调用的每个方法是否都在后台线程中运行?

vb.net - 后台工作程序 CancelAsync() 不起作用

c# - 在后台 worker 中处理dialogwindow

c# - 请求的 URI 对于此 FTP 命令无效

c# - 使用 NTLM 身份验证的基于 HTTP 的 Web 服务

c# - 这两个异步函数之间的区别

javascript - 在 CRM 2015 中禁用表单上的所有字段

c# - 您是否需要后台 worker 或多个线程来触发多个异步 HttpWebRequest?

c# - Azure Fluent API 创建 SQL Server 时出错 - 缺少 x-ms-request-id header

c# - 嵌套的 BackgroundWorkers : RunWorkerCompleted calls on wrong thread?