Background Worker Wrapper (oldie but goodie)

Ran into a need to run a long running process in a Windows Application

Figured writing a wrapper class made calling it much easier

public class BackgroundWorkerWrapper

{

BackgroundWorker _worker;

public Action RunAction{get; set;}

public Action PostAction{get; set;}

public BackgroundWorkerWrapper(Action runAction = null, Action postAction = null)

{

RunAction = runAction;

PostAction = postAction;

_worker = new BackgroundWorker();

_worker.WorkerReportsProgress = true;

_worker.DoWork += _worker_DoWork;

_worker.RunWorkerCompleted += _worker_RunWorkerCompleted;

Run();

}

public void Run()

{

_worker.RunWorkerAsync();

}

void _worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)

{

PostAction.Invoke();

}

void _worker_DoWork(object sender, DoWorkEventArgs e)

{

RunAction.Invoke();

}

}

.

 

 

Share on Facebook

Leave a Reply

Your email address will not be published. Required fields are marked *