开发者

Put a GUI control on a different thread?

开发者 https://www.devze.com 2022-12-21 08:03 出处:网络
I\'m assuming it\'s possibly to do so as programs like google chrome have gone one better and put each tab on a sepa开发者_Python百科rate process.

I'm assuming it's possibly to do so as programs like google chrome have gone one better and put each tab on a sepa开发者_Python百科rate process. So how can i put a GUI control such as a datagridview on a separate thread?


That is not the control that is in a seperate thread, but the request launched through the control.

Let's say you have two tabs opened on different addresses. You click on a link that directs you to another site on the first tab. Meanwhile, you click another link on the second tab.

First, you need to know that a thread might be launched when you click the link on the first tab, making it GUI responsive. This is the same with the second tab, when you clicked the link in it.

Second, we now have one thread (the main thread, also called the GUI-thread) which job is to handle the user interaction. Another thread processing the request from the link on the first tab, and another thread from the link on the second tab.

Third, while your link requests for both tabs are being processed, you might let's say open a new tab and doing a search on Google! Then, this requires your GUI to be responsive, even while the application, the browser, is occupied with your requests.

Fourth, on the background-threads' return, they will return to the main thread reporting the result of their work, that is, the Web response they obtained requesting to solve the DNS linked the the links clicked.

An interesting way to make it possible in C#, and keeping it as easy as possible, is through the BackgroundWorker class.

Each instance of a BackgroundWorker represents a thread. So, you need to instantiate as much of BackgroundWorker as it is required by the application when a mouse-click is captured, when a user clicks a link. The request is then sent to the DoWork() event which is raised when a call to RunWorkerAsync() method is made.

Here's an interesting tutorial on how to use the BackgroundWorker


This Microsoft article explains roughly how Windows Explorer does exactly what you ask:

http://msdn.microsoft.com/en-us/library/ms741870.aspx#multi_browser

You basically have one thread per top level window and all of the events for that window are fired on the same thread.


I don't think that you'll need a Thread for every single control else you'll have a huge number of threads and then you may start to run into all sorts of trouble.

Here's a couple of websites to get you started: http://www.albahari.com/threading/

http://msdn.microsoft.com/en-us/library/aa645740(VS.71).aspx

0

精彩评论

暂无评论...
验证码 换一张
取 消