Cross-thread Winforms control editing [duplicate] - c#

This question already has answers here:
Cross-thread operation not valid: Control accessed from a thread other than the thread it was created on
(22 answers)
Closed 9 years ago.
how can I edit the text in a windows form element if the code that is editing the text 'belongs' to a seperate thread from the one that contains the windows form? I get the exception:
Cross-thread operation not valid: Control 'textBox1' accessed from a thread other than the thread it was created on.
Thank you.

You will need to use Control.Invoke method like this:
textbox1.Invoke((MethodInvoker)(() =>
{
textbox1.Text="some text";
}));
Check this article too: Threading in UIs

Related

How to access control from another thread? C#/WPF [duplicate]

This question already has answers here:
How to deal with cross-thread access exceptions?
(3 answers)
The calling thread cannot access this object because a different thread owns it
(15 answers)
Is it possible to initialize WPF UserControls in different threads?
(3 answers)
Closed 12 months ago.
How to change label in UserControl in UserControl from another thread?
Here is my code:
this.Dispatcher.Invoke(DispatcherPriority.Input,
new DelegateMethod(() => {
App.mainWindow.FirstUserControl.SecondUserControl.txtLbl.Content = "Hello!";
}));
Error:
System.InvalidOperationException: "The thread calling this cannot access the specified object because the object is owned by another thread."

How to change the focus with ListBox in c# [duplicate]

This question already has answers here:
Cross-thread operation not valid: Control accessed from a thread other than the thread it was created on
(22 answers)
Closed 2 years ago.
I'm a beginner with C# so maybe I don't really understand why I have this error.
System.InvalidOperationException: Cross-thread operation not valid:
Control 'lstb_files_r' accessed from a thread other than the thread it
was created on.
This is what I'm doing:
I drop X csv.
I read the first one (lstb_files_r.SelectedIndex = i;)
after finishing I would like to read the 2nd (i++). But I have this error...
Can someone explain to me how can I solve that please ?
When I asked my question I continued to look all the links and I've found something (maybe it's not the best way but I will find an other solution)
I use delegate
lstb_files_r.Invoke((MethodInvoker)delegate
I put all the code inside and it works.
You are performing the cross threaded opertaion which means you are in one thread (which is your algorithm) and want to perform something in another thread (here GUI is another thread) which is not allowed. For that you have to call GUI thread and perform the function. In C# the easiest way is to use backgraoudworker Use of backgroudworker can see here how to use background worker.
In backgroundworker use progressreport to perform the GUI operations. If you need further help I can help you.

Is there a simple way to determine which thread the current method is running on? [duplicate]

This question already has answers here:
Detecting whether on UI thread in WPF and Winforms
(12 answers)
Closed 4 years ago.
The title says it all. I would like to be able to simply detect if work is being executed on the UI thread. Also, if the method is not running on the UI/main thread, can I determine what other thread that it might be? I would like to use this information for debugging purposes. Thanks!
In a UWP app, you can check and store the value of the Environment.CurrentManagedThreadId property when your app starts and then compare the current value of the same property with the stored value to determine whether you are on the same thread.
Or use the CoreDispatcher.HasThreadAccess property to determine whether the current thread can safely update the UI.

How to access a form component with a thread? [duplicate]

This question already has answers here:
How do I update the GUI from another thread?
(47 answers)
Closed 6 years ago.
Simple question, I had an application in console mode. Every thing would write in the console using Console.WriteLine. However, now I have a form and a listbox. I know easily how to do it, however, what is the proper way to do it?
You can access the UI from another thread using delegate, simple and neat:
this.Invoke((MethodInvoker)delegate
{
textBox1.Value = "your text";
});

Accessing a UI thread from a non-UI thread in mvvm wpf [duplicate]

This question already has answers here:
Is it wrong to use the Dispatcher within my ViewModel?
(3 answers)
Closed 6 years ago.
There is a background thread in the viewmodel. It is trying to set a usercontrol property there which is being binded to the view. In simpler terms, accessing a UI thread from a non UI thread. What are the ways to establish this?
Thanks in advance.
in WPF:
Application.Current.Dispatcher.BeginInvoke(
DispatcherPriority.Background, () => { /* UI WORK HERE */ });

Categories