将方法传递给后台工作者 dowork

发布于 2024-09-14 01:24:36 字数 809 浏览 4 评论 0原文

在下面的代码中,有没有一种方法可以不总是订阅 updateWorker_DoWork 方法,而是向其传递这样的方法

public void GetUpdates(SomeObject blah)
{
    //...
    updateWorker.DoWork += new DoWorkEventHandler(blah);
    //...
}


public void GetUpdates()
{
    //Set up worker
    updateWorker.WorkerReportsProgress = true;
    updateWorker.WorkerSupportsCancellation = true;
    updateWorker.DoWork += new DoWorkEventHandler(updateWorker_DoWork);
    updateWorker.RunWorkerCompleted +=
        new RunWorkerCompletedEventHandler(updateWorker_RunWorkerCompleted);
    updateWorker.ProgressChanged +=
        new ProgressChangedEventHandler(updateWorker_ProgressChanged);

    //Run worker
    _canCancelWorker = true;
    updateWorker.RunWorkerAsync();
    //Initial Progress zero percent event
    _thes.UpdateProgress(0);
}

In the code below, is there a way to instead of always subscribing the updateWorker_DoWork method, pass it a method like this

public void GetUpdates(SomeObject blah)
{
    //...
    updateWorker.DoWork += new DoWorkEventHandler(blah);
    //...
}


public void GetUpdates()
{
    //Set up worker
    updateWorker.WorkerReportsProgress = true;
    updateWorker.WorkerSupportsCancellation = true;
    updateWorker.DoWork += new DoWorkEventHandler(updateWorker_DoWork);
    updateWorker.RunWorkerCompleted +=
        new RunWorkerCompletedEventHandler(updateWorker_RunWorkerCompleted);
    updateWorker.ProgressChanged +=
        new ProgressChangedEventHandler(updateWorker_ProgressChanged);

    //Run worker
    _canCancelWorker = true;
    updateWorker.RunWorkerAsync();
    //Initial Progress zero percent event
    _thes.UpdateProgress(0);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

简美 2024-09-21 01:24:36

对于您的 RunWorkerAsync(),您可以传递您喜欢的任何参数。您只需将 Func()Action() 放入其中,然后在 DoWork() 中将对象强制转换回此特定的输入并调用它。

示例如下此处此处

private void InitializeBackgroundWorker()
{
    _Worker = new BackgroundWorker();

    // On a call cast the e.Argument to a Func<TResult> and call it...
    // Take the result from it and put it into e.Result
    _Worker.DoWork += (sender, e) => e.Result = ((Func<string>)e.Argument)();

    // Take the e.Result and print it out
    // (cause we will always call a Func<string> the e.Result must always be a string)
    _Worker.RunWorkerCompleted += (sender, e) =>
    {
        Debug.Print((string)e.Result);
    };
}

private void StartTheWorker()
{
    int someValue = 42;

    //Take a method with a parameter and put it into another func with no parameter
    //This is called currying or binding
    StartTheWorker(new Func<string>(() => DoSomething(someValue)));

   while(_Worker.IsBusy)
       Thread.Sleep(1);

   //If your function exactly matches, just put it into the argument.
   StartTheWorker(AnotherTask);
}

private void StartTheWorker(Func<string> func)
{
    _Worker.RunWorkerAsync(func);
}

private string DoSomething(int value)
{
    return value.ToString("x");
}

private string AnotherTask()
{
    return "Hello World";
}

For your RunWorkerAsync() you can pass any argument you like. You can just put a Func() or Action() into it and in your DoWork() you just cast the object back to this specific type and call it.

Examples are here and here.

private void InitializeBackgroundWorker()
{
    _Worker = new BackgroundWorker();

    // On a call cast the e.Argument to a Func<TResult> and call it...
    // Take the result from it and put it into e.Result
    _Worker.DoWork += (sender, e) => e.Result = ((Func<string>)e.Argument)();

    // Take the e.Result and print it out
    // (cause we will always call a Func<string> the e.Result must always be a string)
    _Worker.RunWorkerCompleted += (sender, e) =>
    {
        Debug.Print((string)e.Result);
    };
}

private void StartTheWorker()
{
    int someValue = 42;

    //Take a method with a parameter and put it into another func with no parameter
    //This is called currying or binding
    StartTheWorker(new Func<string>(() => DoSomething(someValue)));

   while(_Worker.IsBusy)
       Thread.Sleep(1);

   //If your function exactly matches, just put it into the argument.
   StartTheWorker(AnotherTask);
}

private void StartTheWorker(Func<string> func)
{
    _Worker.RunWorkerAsync(func);
}

private string DoSomething(int value)
{
    return value.ToString("x");
}

private string AnotherTask()
{
    return "Hello World";
}
滿滿的愛 2024-09-21 01:24:36

如果我没有理解错的话,你需要 lambda 表达式来构造匿名方法。

updateWorker.DoWork += (sender,e)=>
  {
      //bla
  }

现在您不必总是编写一个方法并将其传递给new DoWorkEventHandler(myMethod)

If I didn't misunderstand you, you need lambda expressions to construct anonymous method.

updateWorker.DoWork += (sender,e)=>
  {
      //bla
  }

Now you needn't always to write a method and pass it to new DoWorkEventHandler(myMethod)

月下客 2024-09-21 01:24:36

解决了,比我想象的要简单得多。只需为 DoWork 上调用的方法创建一个委托即可。也许应该更好地表达我原来的问题。

    public delegate void DoWorkDelegate(object sender,DoWorkEventArgs e);

     public void GetUpdates()
     {
         StartWorker(new DoWorkDelegate(updateWorker_DoWork));
     }

     public void StartWorker(DoWorkDelegate task)
     {
         //Set up worker
         updateWorker.WorkerReportsProgress = true;
         updateWorker.WorkerSupportsCancellation = true;
         updateWorker.DoWork += new DoWorkEventHandler(task);
         updateWorker.RunWorkerCompleted +=
             new RunWorkerCompletedEventHandler(updateWorker_RunWorkerCompleted);
         updateWorker.ProgressChanged +=
             new ProgressChangedEventHandler(updateWorker_ProgressChanged);

         //Run worker
         _canCancelWorker = true;
         updateWorker.RunWorkerAsync();
         //Initial Progress zero percent event
         _thes.UpdateProgress(0);
     }

      private void updateWorker_DoWork(object sender, DoWorkEventArgs e)
      {
          BackgroundWorker worker = sender as BackgroundWorker;
          e.Result = GetUpdatesTask(worker, e);
      }

Worked it out, was way simpler than I was thinking. Just had to make a delegate for the method called on DoWork. Probably should have phrased my original question better.

    public delegate void DoWorkDelegate(object sender,DoWorkEventArgs e);

     public void GetUpdates()
     {
         StartWorker(new DoWorkDelegate(updateWorker_DoWork));
     }

     public void StartWorker(DoWorkDelegate task)
     {
         //Set up worker
         updateWorker.WorkerReportsProgress = true;
         updateWorker.WorkerSupportsCancellation = true;
         updateWorker.DoWork += new DoWorkEventHandler(task);
         updateWorker.RunWorkerCompleted +=
             new RunWorkerCompletedEventHandler(updateWorker_RunWorkerCompleted);
         updateWorker.ProgressChanged +=
             new ProgressChangedEventHandler(updateWorker_ProgressChanged);

         //Run worker
         _canCancelWorker = true;
         updateWorker.RunWorkerAsync();
         //Initial Progress zero percent event
         _thes.UpdateProgress(0);
     }

      private void updateWorker_DoWork(object sender, DoWorkEventArgs e)
      {
          BackgroundWorker worker = sender as BackgroundWorker;
          e.Result = GetUpdatesTask(worker, e);
      }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文