Thread for Windows form

前端 未结 3 992
攒了一身酷
攒了一身酷 2021-01-26 08:25

When I\'m creating a simple Windows form, is it starting in a new thread automatically? Or there is only one thread for all forms?

3条回答
  •  耶瑟儿~
    2021-01-26 09:28

    There is one thread for all forms.

    In fact, Windows Forms (and most windowing technologies), require that all of your forms and controls be generated on a single thread. If you try to use a control from a different thread, it will cause a problem.

    The UI thread in a Windows application actually spends most of its time idle. There is a message queue that is processed, and which causes the events you handle to be raised. If you want to access the UI from another thread, you have to invoke (using Control.Invoke) the method you want to run back onto the UI's thread, or you will receive exceptions.

提交回复
热议问题