If i have a set of actions i want to run in a background worker based on a certain condition and i have 10 conditions, for example
if(a)
BackgroundWorker doA = new backgroundworker()
if(b)
BackgroundWorker doB = new backgroundworker()
if(c)
BackgroundWorker doC = new backgroundworker()
if(d)
BackgroundWorker doD = new backgroundworker()
...
...
each of those background workers will require a dowork, runworkercompleted etc.... is there anyway to avoid that so it makes the code less messy/clutered as some of those methods might be quite big?
thanks
You should use Task
from the System.Threading.Tasks
namespace instead, it's very simple and easy to use.
To start a task, you can simply use: Task.Factory.StartNew()
passing a method or a lambda expression as a parameter. you get back a Task
object that you can use for continuation, getting the result back, etc.
You can consider using a single backgroundworker and passing an argument to it.Using this argument in the DoWork method you can determine which block of code to work.Check this thread
为什么不将需要评估的对象传递给BackgroundWorker,而BackgroundWorker可以使用它来确定要做什么?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.