public static class Worker
{
public static void Execute(params DoWorkEventHandler[] handlers)
{
foreach (DoWorkEventHandler handler in handlers)
{
BackgroundWorker worker = new BackgroundWorker();
DoWorkEventHandler capturedHandler = handler;
worker.DoWork += (sender, e) =>
{
try
{
capturedHandler(sender, e);
}
finally
{
worker.Dispose();
}
};
worker.RunWorkerAsync();
}
}
}
и затем:
Worker.Execute((s, e) =>
{
// doing work
});
или если вы хотите запланировать несколько событий:
Worker.Execute(
(s, e) =>
{
// doing work
},
(s, e) =>
{
// doing work
},
(s, e) =>
{
// doing work
}
);
UPDATE:
Вот альтернатива, которая позволяет вам указать завершенный обработчик:
public class Worker
{
public Worker Work(DoWorkEventHandler doWork, RunWorkerCompletedEventHandler complete)
{
var worker = new BackgroundWorker();
worker.DoWork += doWork;
worker.RunWorkerCompleted += complete;
worker.RunWorkerAsync();
return this;
}
}
, а затем:
new Worker()
.Work((s, e) => { /** some work **/ }, (s, e) => { /** work completed **/ })
.Work((s, e) => { /** some work **/ }, (s, e) => { /** work completed **/ })
.Work((s, e) => { /** some work **/ }, (s, e) => { /** work completed **/ });