Using .NET BackgroundWorker class in console app

后端 未结 3 805
挽巷
挽巷 2020-12-16 16:40

I am relatively new to .NET programming and multithreading in general, and was wondering if it is ok to use .NET provided BackgroundWorker to spawn off worker threads to do

3条回答
  •  太阳男子
    2020-12-16 17:26

    You are right that BackgroundWorker will not work here. It is indeed designed to Work with GUI environments (WinForms and WPF alike) where the main Thread is permanently busy checking/executing the Message Pump (wich processes all the Windows Events like Click and Resize, plus the ones comming from backgroundWorker). Without the Event Queue the Mainthread will run out. And without the Event queue, BackgroundWorker cannot invoke the Callbacks on the Main Thread either.

    Indeed doing Multithreading on a Console is a dozen times harder because you don't have the Message Pump to solve the two big issues: how to keep Main Thread alive and how to inform the main Thread of Changes needed on the UI. Event based Programming prooved to be the ideal learning ground for Multithreading.

    There are some solutions:

    Expanding your Console App with a Message Pump. Since it is little more then a Thread Shared Collection of Delegates (where other threads only add Stuff) and a while loop it's surprisingly easy: C# Console App + Event Handling

    The ThreadPool and Thread Classes. They have been around as long as the BackgroundWorker (since .NET 2.0) and seem to be designed to solve the issue for Console. You still need to block your Main Thread with a loop. ThreadPool will also limit the Number of Threads to something suiteable on the Real machine. You should avoid fixed Thread limits and instead rely on the ThreadPooling features of the OS to handle the question of "how many Thread should run at once?". For example, fixed Number of 5 Threads might be ideal for your 6 Core Development Machine. But it would be too many for a Single Core Desktop Computer. And would be an pointless Bottleneck on a Server wich has easily between 16 and 64 Cores with GiB's of RAM to spare.

    If you have .NET 4.0 or later, the newly added Task Paralellism might be worth a look. http://msdn.microsoft.com/en-us/library/dd537609.aspx It has built in ThreadPooling, limited Priorization abilities and a can chain and join multiple Tasks easily (everything Thread could do, Task can do). Using Task also enables the use of async and await Keywords: http://msdn.microsoft.com/en-us/library/hh191443.aspx Again, you do not get around blocking the Main Thread in a Console App.

提交回复
热议问题