Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to implement C#'s BackgroundWorker in Delphi?

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?

Here's some code in 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
}
like image 205
Dave Avatar asked Jan 09 '09 18:01

Dave


People also ask

How do you implement C?

A standard conforming C implementation consists of a compiler that translates compilation units as mandated by the standard, an implementation of the standard library for all functions required by the standard and something (normally a linker) that puts everything together to build an executable file.

What language is C implemented in?

It was based on CPL (Combined Programming Language), which had been first condensed into the B programming language—a stripped-down computer programming language—created in 1969–70 by Ken Thompson, an American computer scientist and a colleague of Ritchie.


2 Answers

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

like image 76
mghie Avatar answered Oct 22 '22 17:10

mghie


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

like image 20
merşahin Avatar answered Oct 22 '22 17:10

merşahin