Mon*_*RPG 0 c# wpf multithreading task-parallel-library windows-8.1
好吧,就我从线程中读到的而言,这是不可能的,但在我的情况下肯定会发生.
取决于我开始执行多少后台任务,即使它们与ui线程有0关系,也肯定会影响我的gui响应
所以我的问题是,是否有人知道其他线程如何使ui变得反应迟钝?
我100%确定这些非ui线程导致其缓慢,因为它发生,即使我禁用所有gui更新事件.它肯定受我的案例中有多少个线程(抓取网址任务和处理这些抓取的页面任务)的影响
这是我的ui线程以及我如何开始后台任务:
InitializeComponent();
this.DataContext = this;
ThreadPool.SetMaxThreads(10000, 10000);
ThreadPool.SetMinThreads(10000, 10000);
PublicVariables.initPublicVariables();
PublicStaticFunctions.func_initLists();
PublicSettings.func_init_Settings_Messages();
Task.Factory.StartNew(() =>
{
CheckCrawlURLs.func_StartCrawlingWaitingUrls();
AddUrlsToDB.func_StartUrlAddProcess();
LoadCrawlingUrlsFromDatabase.func_StartLoadingUrlsFromDB();
GlobalStats.startUpdatingGlobalStatValues();
PagesProcessor.func_StartProcessingWaitingPages();
}, CancellationToken.None,
TaskCreationOptions.LongRunning,
TaskScheduler.Default);
AppDomain currentDomain = AppDomain.CurrentDomain;
Application.Current.DispatcherUnhandledException +=
new DispatcherUnhandledExceptionEventHandler(CloseCrashHandlers.AppDispatcherUnhandledException);
currentDomain.UnhandledException +=
new UnhandledExceptionEventHandler(CloseCrashHandlers.CrashCloseHandler);
Closing += new CancelEventHandler(CloseCrashHandlers.CloseHander);
set_Buttons_Status();
_timer = new Timer(updateGlobalStatistics,
null,
PublicSettings.irTimers_Delayed_Start_MiliSeconds,
PublicSettings.ir_RefreshUI_MS);
WebConnectionStats.Init();
Run Code Online (Sandbox Code Playgroud)
您的计算机无法同时运行无限数量的线程.它实际上只能实际运行一些.然后它需要在各个线程中旋转,为每个线程提供一小段时间,以便在更大程度上"伪造"并行化.
你拥有的线程越多,每个人得到的馅饼就越小.如果你有足够的线程,你最终会得到"饥饿",每个线程的时间都很少,以至于它无法做任何有效的事情,整个机器就会陷入停滞状态.切换线程的成本加剧了这一点; 一台机器可以达到最终花费大部分时间只需在线程之间切换而不是进行高效工作的程度.
为了防止这种情况,您应该将创建的线程数限制为相当小的数量.如果您依赖于线程池,那么它的调度程序通常可以有效地创建比在您的机器上有效的线程更多的线程.