How can solve “Cross-thread operation not valid”?

A control created in the UI thread cannot be accessed in another thread in normal fashion. Please create a delegate and invoke the delegate using control. Invoke The method sample provided below can be used to enable visibility on a button regardless of the thread context you are in private void EnableButtonVisibility( Button btn, bool enable) { if (!btn.

InvokeRequired ) { btn. Visible = enable; } else { btn. Invoke( new EnableButtonVisibilityHandler( EnableButtonVisibility ), btn, enable ); } } delegate void EnableButtonVisibilityHandler( Button btn, bool enable).

A control created in the UI thread cannot be accessed in another thread in normal fashion. Please create a delegate and invoke the delegate using control.Invoke. The method sample provided below can be used to enable visibility on a button regardless of the thread context you are in.

Private void EnableButtonVisibility( Button btn, bool enable) { if (!btn. InvokeRequired ) { btn. Visible = enable; } else { btn.

Invoke( new EnableButtonVisibilityHandler( EnableButtonVisibility ), btn, enable ); } } delegate void EnableButtonVisibilityHandler( Button btn, bool enable).

This is a solution I will occasionally use. – Meiscooldude Jun 15 '10 at 14:18 2 No need to create a delegate, you can use Action that is avilable in the base class library. – Fredrik Mörk Jun 15 '10 at 14:22 +1 Yeah.

That can be used to add an inline method as well. – Bharath K Jun 15 '10 at 14:23.

You cannot update a UI component from a non-UI thread. Use TaskScheduler. FromCurrentSynchronizationContext to marshal the call to the UI thread.

You learn something everyday, what I absolutely love about SO. They have snuck some goodies into 4.0 haven't they. – Spence Jun 15 '10 at 14:20.

The thread in background is not allowed to access the UI components. In multithreaded forms, I include code like this: private delegate void InvokeAction(); private void DoUI(InvokeAction call) { if (IsDisposed) { return; } if (InvokeRequired) { try { Invoke(call); } catch (InvalidOperationException) { // Handle error } } else { call(); } } Then from my background thread I can code like this: // Stuff in other thread myVar = GetFromDb(); DoUI(() => { // Access UI components here listBox1.Items. Add(myVar); }); // More other thread.

Trippple points if I could award it for this answer.. Reusable code is the way forward! – Christian yesterday.

A control created in the UI thread cannot be accessed in another thread in normal fashion. Please create a delegate and invoke the delegate using control.Invoke. The method sample provided below can be used to enable visibility on a button regardless of the thread context you are in.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions