Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Cross thread

ravanegmondravanegmond Posts: 9Member
Hello I have a little program I made a very simple udp server. When the program starts a new thread... waiting for any incoming data.

Now my problem is when the incoming data is proccesed I want to put it in textBox1 located on Form1

because textBox1 is created in a other thread I get this error:

System.InvalidOperationException was unhandled
Cross-thread operation not valid: Control 'tbMessages' accessed from a thread other than the thread it was created on.

So I now the problem but I don't have any idea how to solve it I tried things with modifiers but still the same result.

Please help!


Comments

  • iwilld0itiwilld0it Posts: 1,134Member
    You use a delegate to set the text property on the main thread. Here is an example:

    [code]
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    using System.Threading;

    public delegate void SetTextDeleg(string text);

    namespace WinTestC
    {
    public class Form1 : System.Windows.Forms.Form
    {
    private System.Windows.Forms.TextBox textBox1;

    [STAThread]
    static void Main()
    {
    Application.Run(new Form1());
    }

    private void Form1_Load(object sender, System.EventArgs e)
    {
    Thread t = new Thread(new ThreadStart(DoWork));
    t.Start();

    textBox1.Text = string.Empty;
    }

    private void SetText(string text)
    {
    textBox1.Text = text;
    }

    private void DoWork()
    {
    Thread.Sleep(3000);

    textBox1.Invoke(
    new SetTextDeleg(SetText),
    new object[]{"Some Text"});
    }
    }
    }
    [/code]

    The key functionality is demonstrated in the DoWork thread. A call to the textbox objects Invoke method is used to generate a delegate call (SetTextDeleg) on the main thread to the SetText routine. The second argument is an object array. There should be an object in this array for every argument in the delegate. Finally, the SetText callback does the actual work of setting the textbox text property on the main thread.

    Yes, this is alot of work to set a simple property, but i'm sure you can streamline it.

  • ravanegmondravanegmond Posts: 9Member
    : You use a delegate to set the text property on the main thread. Here is an example:
    :
    : [code]
    : using System;
    : using System.Drawing;
    : using System.Collections;
    : using System.ComponentModel;
    : using System.Windows.Forms;
    : using System.Data;
    : using System.Threading;
    :
    : public delegate void SetTextDeleg(string text);
    :
    : namespace WinTestC
    : {
    : public class Form1 : System.Windows.Forms.Form
    : {
    : private System.Windows.Forms.TextBox textBox1;
    :
    : [STAThread]
    : static void Main()
    : {
    : Application.Run(new Form1());
    : }
    :
    : private void Form1_Load(object sender, System.EventArgs e)
    : {
    : Thread t = new Thread(new ThreadStart(DoWork));
    : t.Start();
    :
    : textBox1.Text = string.Empty;
    : }
    :
    : private void SetText(string text)
    : {
    : textBox1.Text = text;
    : }
    :
    : private void DoWork()
    : {
    : Thread.Sleep(3000);
    :
    : textBox1.Invoke(
    : new SetTextDeleg(SetText),
    : new object[]{"Some Text"});
    : }
    : }
    : }
    : [/code]
    :
    : The key functionality is demonstrated in the DoWork thread. A call to the textbox objects Invoke method is used to generate a delegate call (SetTextDeleg) on the main thread to the SetText routine. The second argument is an object array. There should be an object in this array for every argument in the delegate. Finally, the SetText callback does the actual work of setting the textbox text property on the main thread.
    :
    : Yes, this is alot of work to set a simple property, but i'm sure you can streamline it.
    :
    :

    Thank you very very very much. It's works perfectly and so smoothly.
    It is the first time I work with multiple threads. I don't know exactly how this solution works but it works;) but if you like to tell, can you explain me the following.

    delegate
    and object

    Thanxz in advance.
  • iwilld0itiwilld0it Posts: 1,134Member
    A delegate is merely a managed function pointer. When you declare a delegate such as:

    [code]
    public delegate void DoSomethingDeleg();
    [/code]

    The compiler generates a class that wraps a function that takes no arguments and returns void. Take a look at this example C# Console App:

    [code]
    using System;

    namespace DelegCSharp
    {
    public delegate void DoSomethingDeleg();

    ///
    /// Summary description for Class1.
    ///
    class Class1
    {
    ///
    /// The main entry point for the application.
    ///
    [STAThread]
    static void Main(string[] args)
    {
    DoSomethingDeleg doIt = new DoSomethingDeleg(SaySomething);
    doIt += new DoSomethingDeleg(SaySomethingElse);

    doIt();
    }

    public static void SaySomething()
    {
    Console.Write("Hello");
    }

    public static void SaySomethingElse()
    {
    Console.WriteLine(" World");
    }
    }
    }
    [/code]

    This line:

    [code]
    DoSomethingDeleg doIt = new DoSomethingDeleg(SaySomething);
    [/code]

    Creates a function pointer to the function called SaySomething.

    This line:

    [code]
    doIt += new DoSomethingDeleg(SaySomethingElse);
    [/code]

    looks strange, but to explain, delegates maintain a list of function pointers of the same type. The += operator says to add another function to that list. This is called multicasting and in fact the class that the compiler generates for the delegate, derives from MultiCastDelegate.

    Finally, this line:

    [code]
    doIt();
    [/code]

    Calls all the functions in the delegate, in the order they were added. So basically this single line of code calls the SaySomething function followed by the SaySomethingElse function. Cool huh.

    Delegates are also used for generating call backs:

    [code]
    public static void CallBackSayIt(DoSomethingDeleg sayIt)
    {
    if(null != sayIt)
    sayIt();
    }
    [/code]

    To use this function you could ...

    [code]
    CallBackSayIt(new DoSomethingDeleg(SaySomething));
    [/code]

    Delegates are also used to handle events and such. Also, you are probably wondering why a delagate is needed to set the property of a text box control on the main thread from a secondary thread. The reason is because delegates are always triggered from the main thread.

    Delegates are difficult to get use to, but with practice, you can accomplish some advanced techniques.
    ---------------------------------------------------------------------

    Object
    ------

    All classes derive directly or indirectly from an object variable. You can store anything in an object.

    [code]
    object obj = 17;
    obj = "Hello";
    obj = new StringBuilder();
    // etc ...
    [/code]
  • ravanegmondravanegmond Posts: 9Member
    : A delegate is merely a managed function pointer. When you declare a delegate such as:
    :
    : [code]
    : public delegate void DoSomethingDeleg();
    : [/code]
    :
    : The compiler generates a class that wraps a function that takes no arguments and returns void. Take a look at this example C# Console App:
    :
    : [code]
    : using System;
    :
    : namespace DelegCSharp
    : {
    : public delegate void DoSomethingDeleg();
    :
    : ///
    : /// Summary description for Class1.
    : ///
    : class Class1
    : {
    : ///
    : /// The main entry point for the application.
    : ///
    : [STAThread]
    : static void Main(string[] args)
    : {
    : DoSomethingDeleg doIt = new DoSomethingDeleg(SaySomething);
    : doIt += new DoSomethingDeleg(SaySomethingElse);
    :
    : doIt();
    : }
    :
    : public static void SaySomething()
    : {
    : Console.Write("Hello");
    : }
    :
    : public static void SaySomethingElse()
    : {
    : Console.WriteLine(" World");
    : }
    : }
    : }
    : [/code]
    :
    : This line:
    :
    : [code]
    : DoSomethingDeleg doIt = new DoSomethingDeleg(SaySomething);
    : [/code]
    :
    : Creates a function pointer to the function called SaySomething.
    :
    : This line:
    :
    : [code]
    : doIt += new DoSomethingDeleg(SaySomethingElse);
    : [/code]
    :
    : looks strange, but to explain, delegates maintain a list of function pointers of the same type. The += operator says to add another function to that list. This is called multicasting and in fact the class that the compiler generates for the delegate, derives from MultiCastDelegate.
    :
    : Finally, this line:
    :
    : [code]
    : doIt();
    : [/code]
    :
    : Calls all the functions in the delegate, in the order they were added. So basically this single line of code calls the SaySomething function followed by the SaySomethingElse function. Cool huh.
    :
    : Delegates are also used for generating call backs:
    :
    : [code]
    : public static void CallBackSayIt(DoSomethingDeleg sayIt)
    : {
    : if(null != sayIt)
    : sayIt();
    : }
    : [/code]
    :
    : To use this function you could ...
    :
    : [code]
    : CallBackSayIt(new DoSomethingDeleg(SaySomething));
    : [/code]
    :
    : Delegates are also used to handle events and such. Also, you are probably wondering why a delagate is needed to set the property of a text box control on the main thread from a secondary thread. The reason is because delegates are always triggered from the main thread.
    :
    : Delegates are difficult to get use to, but with practice, you can accomplish some advanced techniques.
    : ---------------------------------------------------------------------
    :
    : Object
    : ------
    :
    : All classes derive directly or indirectly from an object variable. You can store anything in an object.
    :
    : [code]
    : object obj = 17;
    : obj = "Hello";
    : obj = new StringBuilder();
    : // etc ...
    : [/code]
    :

    Thanxz

Sign In or Register to comment.