Access to Object (TextBox or ListBox) from a Thread
-
Example:
.... Thread a = new Thread(method1); a.Start(); .... .... void method1() { textBox1.Text = "Hello"; }
if i execute that code, they give me an InvalidOperationException error (access to textbox1 from another process) i try to call that method using an delegate, but not works another way is add this code:CheckForIllegalCrossThreadCalls = false;
but i don't need to deactivated that how i can fixed? -
Example:
.... Thread a = new Thread(method1); a.Start(); .... .... void method1() { textBox1.Text = "Hello"; }
if i execute that code, they give me an InvalidOperationException error (access to textbox1 from another process) i try to call that method using an delegate, but not works another way is add this code:CheckForIllegalCrossThreadCalls = false;
but i don't need to deactivated that how i can fixed?you can access a Control only from the thread that created it, normally the main thread. Read up on Control.InvokeRequired and Control.Invoke() :)
Luc Pattyn [Forum Guidelines] [My Articles]
this months tips: - use PRE tags to preserve formatting when showing multi-line code snippets - before you ask a question here, search CodeProject, then Google
-
you can access a Control only from the thread that created it, normally the main thread. Read up on Control.InvokeRequired and Control.Invoke() :)
Luc Pattyn [Forum Guidelines] [My Articles]
this months tips: - use PRE tags to preserve formatting when showing multi-line code snippets - before you ask a question here, search CodeProject, then Google
need some tutorial or example please (to understand)
-
need some tutorial or example please (to understand)
MSDN, Google and CodeProject are full of examples: almost every app that uses extra threads needs it! Here is an example in VB.NET[^], the last code snippet shows the principle.
Luc Pattyn [Forum Guidelines] [My Articles]
this months tips: - use PRE tags to preserve formatting when showing multi-line code snippets - before you ask a question here, search CodeProject, then Google
-
MSDN, Google and CodeProject are full of examples: almost every app that uses extra threads needs it! Here is an example in VB.NET[^], the last code snippet shows the principle.
Luc Pattyn [Forum Guidelines] [My Articles]
this months tips: - use PRE tags to preserve formatting when showing multi-line code snippets - before you ask a question here, search CodeProject, then Google
Y readed and works fine my C# Code:
public delegate void updatevalores(string dato); private void actualizarv(string datos) { if (this.InvokeRequired) { this.Invoke(new updatevalores(actualizarv),new object[]{dato}); return; } listBox1.Items.Add(dato.ToString()); }
Thanks for all :rolleyes: