Explanation of need for Multi Threading GUI programming

后端 未结 3 2104
清歌不尽
清歌不尽 2021-02-08 15:44

I\'m looking for a good explanation of the need to use multi-threading in graphical applications. In the examples below Python is used but the question is not python specific it

相关标签:
3条回答
  • 2021-02-08 16:24

    The main reason is, that GUI toolkit process all events (mouse movement, button click, keyboard input, system events, etc) in the mainloop, which is not part of the code you write for you GUI application.

    This main loop call all the event handlers and other functions you provide. Now, if one of those functions take too long (e.g. > 100ms), then there will be a very noticeable effect on UI responsiveness, as the main loop will not be able to process more events.

    This topic should be discussed in details in the "advanced concepts" section of any book on GUI programming, regardless of programming language of toolkit used.

    0 讨论(0)
  • 2021-02-08 16:33

    Your understanding is correct. If an application isn't muli-threaded the application waits for every operation to finish. When your application is multi-threaded you use one thread to handle GUI actions and another to process the files.

    I don't have an article or a reference to something similar. Maybe it helps if you think of threads as people, each has it's own job and each can only do one thing at a time.

    0 讨论(0)
  • 2021-02-08 16:33

    Multithreading is one way of approaching this, but not necessarily the only (or best) way. As a python example, greenlets provide a way of running concurrent processes within the same thread, avoiding the problems with locking associated with multithreading. I would certainly consider greenlets to be a preferred solution in most cases simply because of the relative ease of coding them.

    0 讨论(0)
提交回复
热议问题