Form freezes during while loop

前端 未结 3 1969
旧巷少年郎
旧巷少年郎 2021-02-10 12:27

I have a piece of code that checks if a certain application is running

while (Process.GetProcessesByName(\"notepad\").Length == 0)
{
     System.Threading.Thread         


        
3条回答
  •  醉梦人生
    2021-02-10 13:06

    The form freezes because your code is running on the UI/Main thread.
    So because you are Sleeping the thread while notepad is not open then your form will lock up.
    If you run your code asynchronously then you move the worker thread away from the UI.
    See here for an C# async overview

提交回复
热议问题