- Visitors can check out the Forum FAQ by clicking this link. You have to register before you can post: click the REGISTER link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below. View our Forum Privacy Policy.
- Want to receive the latest contracting news and advice straight to your inbox? Sign up to the ContractorUK newsletter here. Every sign up will also be entered into a draw to WIN £100 Amazon vouchers!
Reply to: .NET 2 Multi Threaded UI
Collapse
You are not logged in or you do not have permission to access this page. This could be due to one of several reasons:
- You are not logged in. If you are already registered, fill in the form below to log in, or follow the "Sign Up" link to register a new account.
- You may not have sufficient privileges to access this page. Are you trying to edit someone else's post, access administrative features or some other privileged system?
- If you are trying to post, the administrator may have disabled your account, or it may be awaiting activation.
Logging in...
Previously on ".NET 2 Multi Threaded UI"
Collapse
-
Bugger. Still seem to end up in the position where the UI doesn't properly see WM_PAINT. New plan. Retrofit dos 6.1 to the entire estate. Wonder if they'd like text mode windows.
-
Originally posted by VectraMan View PostOutside of .NET, I've found that updating Windows windows from threads other than the one used to create them often causes unpleasentness and is best avoided. I guess .NET is just enforcing that best practice.
My approach to multithreading and GUIs would always be to update all the controls in the one GUI thread. The GUI thread can monitor the data to look for changes and update, or better still wait for an event which the worker thread(s) set to indicate the GUI needs to be updated.
BTW you might want to use BeginInvoke, as Invoke will block until the GUI thread has completed the call. If the GUI thread is waiting on the worker thread, then you're in deadlock country.
Leave a comment:
-
Outside of .NET, I've found that updating Windows windows from threads other than the one used to create them often causes unpleasentness and is best avoided. I guess .NET is just enforcing that best practice.
My approach to multithreading and GUIs would always be to update all the controls in the one GUI thread. The GUI thread can monitor the data to look for changes and update, or better still wait for an event which the worker thread(s) set to indicate the GUI needs to be updated.
BTW you might want to use BeginInvoke, as Invoke will block until the GUI thread has completed the call. If the GUI thread is waiting on the worker thread, then you're in deadlock country.
Leave a comment:
-
Originally posted by DimPrawn View PostWhy not create a set of thread safe controls?
e.g.
Code:public class SafeLabel : Label { delegate void SetString(string text); delegate string GetString(); override public string Text { set { if(InvokeRequired) { SetString setTextDel = delegate(string text) { Text = text; }; Invoke(setTextDel,new object[]{value}); } else base.Text = value; } get { if(InvokeRequired) { GetString getTextDel = delegate(){ return Text; }; return (string)Invoke(getTextDel,null); } else return base.Text; } } }
I may well give it another go at some point soon.
Leave a comment:
-
Why not create a set of thread safe controls?
e.g.
Code:public class SafeLabel : Label { delegate void SetString(string text); delegate string GetString(); override public string Text { set { if(InvokeRequired) { SetString setTextDel = delegate(string text) { Text = text; }; Invoke(setTextDel,new object[]{value}); } else base.Text = value; } get { if(InvokeRequired) { GetString getTextDel = delegate(){ return Text; }; return (string)Invoke(getTextDel,null); } else return base.Text; } } }
Leave a comment:
-
Problem is the threads aren't created by the UI in question so background worker is a bit yucky (but not impossible). I'm producing a bridge between two disparate sytems and theres a lot of bodging, I mean elegantly crafted and well though out principles, between them bridging the gap.
I guess I'm stuck with the moethod I came up with earlier. Though I can probably improve it a bit with generics. It's just a lot of boring grunge to knock up.
Of course I could always set Control.CheckForIllegalCrossThreadCalls to false and say "well it worked on the development box"
Leave a comment:
-
Can you use BackgroundWorker instead of creating your own threads?
http://msdn.microsoft.com/en-us/libr...undworker.aspx
Makes creating responsive multithreaded UI's easier.
Alternatively, if you've had plenty of coffee this morning.
http://blog.quantumbitdesigns.com/20...ler-revisited/
Leave a comment:
-
.NET 2 Multi Threaded UI
I have never exactly understood why UI controls are not thread safe. I guess there are perfectly good reasons, but it does strike me as unfortunate that there is no way (at least that I know of) of running the standard UI controls in a thread-safe mode.
Predictably I now have a UI which needs to be thread safe.
Typically of course I would want to write:-
SomeTextBox.AppendText(someString)
But of course I can't do that. So I can abstract the UI Updates into a different class and write instead:-
XTUI.AppendText(someTextBox,someString)
And:-
Delegate Sub TSAppendTextCallback(ByVal t As TextBox, ByVal s As String)
Shared Sub AppendText(byval someTextBox,someString)
If t.InvokeRequired Thent.Invoke(New TSAppendTextCallback(AddressOf TSAppendText), t, s)Elset.AppendText(s)End If
However, this is obviously a right royal pain in the butt. There must be a better way. The world and his dog must be writing multi threaded UI's. However, I have been unable as yet to find a better approach. Any suggestions?Last edited by ASB; 12 June 2009, 06:06.Tags: None
- Home
- News & Features
- First Timers
- IR35 / S660 / BN66
- Employee Benefit Trusts
- Agency Workers Regulations
- MSC Legislation
- Limited Companies
- Dividends
- Umbrella Company
- VAT / Flat Rate VAT
- Job News & Guides
- Money News & Guides
- Guide to Contracts
- Successful Contracting
- Contracting Overseas
- Contractor Calculators
- MVL
- Contractor Expenses
Advertisers
Contractor Services
CUK News
- Streamline Your Retirement with iSIPP: A Solution for Contractor Pensions Sep 1 09:13
- Making the most of pension lump sums: overview for contractors Sep 1 08:36
- Umbrella company tribunal cases are opening up; are your wages subject to unlawful deductions, too? Aug 31 08:38
- Contractors, relabelling 'labour' as 'services' to appear 'fully contracted out' won't dupe IR35 inspectors Aug 31 08:30
- How often does HMRC check tax returns? Aug 30 08:27
- Work-life balance as an IT contractor: 5 top tips from a tech recruiter Aug 30 08:20
- Autumn Statement 2023 tipped to prioritise mental health, in a boost for UK workplaces Aug 29 08:33
- Final reminder for contractors to respond to the umbrella consultation (closing today) Aug 29 08:09
- Top 5 most in demand cyber security contract roles Aug 25 08:38
- Changes to the right to request flexible working are incoming, but how will contractors be affected? Aug 24 08:25
Leave a comment: