在Delphi中实现C#的BackgroundWorker的最佳方法是什么?

时间:2022-09-02 11:16:42

I use C#'s BackgroundWorker object frequently to start a thread and perform a task. What's the easiest way to accomplish the same thing in Delphi?

我经常使用C#的BackgroundWorker对象来启动线程并执行任务。在Delphi中完成同样事情的最简单方法是什么?

Here's some code in C#:

这是C#中的一些代码:

private void button1_Click(object sender, EventArgs e)
{
  BackgroundWorker bg = new BackgroundWorker();
  bg.DoWork += new DoWorkEventHandler(bg_DoWork);
  bg.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bg_RunWorkerCompleted);
  test_number++;
  object[] arguments = { "test", test_number };
  bg.RunWorkerAsync(arguments);
}

void bg_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
      // done.
}

void bg_DoWork(object sender, DoWorkEventArgs e)
{
     // do actual work here
}

2 个解决方案

#1


Look into OmniThreadLibrary by Primoz Gabrijelcic, or into AsyncCalls by Andreas Hausladen, which should both give you similar functionality.

查看Primoz Gabrijelcic的OmniThreadLibrary,或Andreas Hausladen的AsyncCalls,它们都应该为您提供类似的功能。

#2


for your solution this is: delphi-components/backgroundworker this component like c# backgroundworker's event, property etc.

对于你的解决方案,这是:delphi-components / backgroundworker这个组件就像c#backgroundworker的事件,属性等。

#1


Look into OmniThreadLibrary by Primoz Gabrijelcic, or into AsyncCalls by Andreas Hausladen, which should both give you similar functionality.

查看Primoz Gabrijelcic的OmniThreadLibrary,或Andreas Hausladen的AsyncCalls,它们都应该为您提供类似的功能。

#2


for your solution this is: delphi-components/backgroundworker this component like c# backgroundworker's event, property etc.

对于你的解决方案,这是:delphi-components / backgroundworker这个组件就像c#backgroundworker的事件,属性等。