views:

104

answers:

4

I'm looking at creating a tabbed interface which has user controls (possibly written by plug-in developers) within a tabbed or MDI interface. These plug-in controls could unintentionally freeze their GUI thread, and I'd prefer that they not influence user controls in other tabs. Much like Google Chrome creates a process for each tab; but in this case, just threads.

Or perhaps even an MDI interface where the child MDI forms are owned by separate threads?

I've found that while I can run multiple GUI threads at once, the Form level is where they MUST be separated. Any workarounds/ideas?

For those saying this shouldn't be needed, I call bullshit. Google's Chrome browser runs tabs in separate processes for security and UI reasons. I'm merely trying to duplicate this behavior. When the people writing the user controls are sucky plug-in developers, this is important.

+2  A: 

No it is not possible to do this in the way you are describing. A control which is owned / affinitized to another GUI thread cannot be directly contained within a control which is owned / affinitized to a different thread in such a way that it's paint function runs on the other thread.

JaredPar
Do not like. They should marshal the call if they need to damnit! :). How about in WPF?
TheSoftwareJedi
@TheSoftwareJedi, same for WPF :(. At least for all of the mutable types. It may be possible with the few immutable / frezzable types. Don't know enough to say definitively though.
JaredPar
@Downvoter, care to explain?
JaredPar
Why do you "not like"? What use case do you have that can only be solved with your appraoch and cannot be solved by invoking a method on another thread?
Ed Swangren
@Ed When you work for a large organization, developers design controls which can be faulty. I'm not forgiving it, but accepting it. I'd like to be able to isolate them. It's not a perfect development world...
TheSoftwareJedi
@Ed This application allows users to install plugins written outside of my control. If these plugins cause GUI delays - my application is blamed. It's very similar to the Chrome issue. I'd rather see "Aw Snap" like Google shows when a tab goes bad then have the whole application go bad. When a given plugin dies/locks up, it's clearer who the blame lies with if the entire application doesn't die. Don't be so fast to jump to conculsions. I'm not an idiot.
TheSoftwareJedi
A: 

Instead of having or owning different GUI threads, you should view the whole issue from a different angle. Why would you want a thread associated to tab's child control to be freezed? If it does freeze and everything else feezes too, threading aside, that's not done right from ground up.

What JaredPar pointed out is correct, but that doesn't mean you cannot achieve what you want. I assume you want stuff running within a tab to continue running/stopping without affecting other controls and user-experience.

I've done it before in a complex WinForm app. Here are some readings which might give you more insights:
Threading out tasks in a C#.NET GUI
Thread and GUI
Updating GUI from Other Threads in C#
Advanced Techniques To Avoid And Detect Deadlocks In .NET Apps

o.k.w
When you work for a large organization, developers design controls which can be faulty. I'm not forgiving it, but accepting it. I'd like to be able to isolate them. It's not a perfect development world...
TheSoftwareJedi
+1  A: 

The right way to fix this situation is to write UserControls that don't perform long-running tasks on the UI thread. If the control is blocking and waiting on some computational task, fix that. Make that task run in the background, and have the control display some non-compute-intensive content until it's done. If that task freezes, the control will be frozen in its "I'm waiting..." state, but it won't intrude on the rest of the UI.

If you're using a third-party control that you can't fix, well, in the immortal words of Jay-Z, I feel bad for you, son.

Robert Rossney
When you work for a large organization, developers design controls which can be faulty. I'm not forgiving it, but accepting it. I'd like to be able to isolate them. It's not a perfect development world...
TheSoftwareJedi
No, it's seriously not, and in this case I'm afraid there's probably nothing you can do.
Robert Rossney
+1  A: 

For the most part, controls shouldn't be performing any processing. Their purpose is to provide interactivity between the user and the application. For example, it is not the job of a button to fetch data from a database and present it to the user. That being said, hopefully you are doing your processing in a controls event handler, such as the Click event on the Button control. In your event handler, you can prevent the UI from appearing "hung" by processing tasks in a background thread. The BackgroundWorker is often useful in these situations.

I suggest reading up on Threading. The Microsoft® .NET Framework Application Development Foundation book has a section on threading (even if no other certification books are read, I at least recommend all .NET developers read this book). Just remember not to update the UI from a child thread. Read an example on how to make a thread-safe call to Windows controls if you're not familiar with this approach.

senfo
When you work for a large organization, developers design controls which can be faulty. I'm not forgiving it, but accepting it. I'd like to be able to isolate them. It's not a perfect development world...
TheSoftwareJedi
I do not need to read up on threads. I've written a ton of multi-threaded software. I know that the original question IS possible - but perhaps not in .NET. I am allowing users to dynamically load plugins, and drop them onto a new tab. Thus, said plugin should be isolated. I am not asking for tips on threading and control development - just advice on how to solve the problem stated.
TheSoftwareJedi