How to pass numerous arguments to a BackgroundWorker in c++
I've seen examples of this in C# and VB, but i'm really constrained to c++, I'm trying to pass 20 Strings to a background worker so that it can perform a task on them. I've tried pa开发者_如何学JAVAssing the windows form object, but it can't be casted back to a Form from a System::Object without causing compiler errors.
I've also tried passing an object which contains the strings through the DoWork method, however this also doesn't work.
Any help would be much appreciated. Thank You
For a larger number of strings (or, more generally, "tasks"), you would either use a synchronized queue, a lockfree queue, or as the easiest solution, a completion port. Completion ports are surprisingly efficient, if you have no more than a few ten thousand tasks to schedule, it really isn't worth thinking about a more sophisticated solution.
For only 20 tasks, this is usually overkill (unless each task runs for a very long time), putting everthing into one object and passing a pointer to that object is the best solution. Otherwise, the communication/synchronisation overhead is more than the gain from multithreading.
Passing a pointer to a struct to a worker thread should work perfectly fine in C++ (I'm doing that all the time). What exactly is the problem?
精彩评论