<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
  <channel>
    <title>'Cross thread' Thread RSS Feed</title>
    <link>http://www.programmersheaven.com/</link>
    <description>Contains the latest posts from the thread 'Cross thread' posted on the 'C#' forum at Programmer's Heaven.</description>
    <language>en</language>
    <copyright>Copyright 2012 Programmers Heaven</copyright>
    <pubDate>Wed, 23 May 2012 13:23:54 -0700</pubDate>
    <lastBuildDate>Wed, 23 May 2012 13:23:54 -0700</lastBuildDate>
    <generator>Argotic Syndication Framework 2007.3.0.1, http://www.codeplex.com/Argotic</generator>
    <docs>http://www.rssboard.org/rss-specification</docs>
    <ttl>360</ttl>
    <image>
      <url>http://www.programmersheaven.com/images/ph.gif</url>
      <title>Programmers Heaven</title>
      <link>http://www.programmersheaven.com/</link>
      <width>88</width>
      <height>31</height>
    </image>
    <item>
      <title>Cross thread</title>
      <link>http://www.programmersheaven.com/mb/csharp/309997/309997/cross-thread/</link>
      <description>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.&lt;br /&gt;
&lt;br /&gt;
Now my problem is when the incoming data is proccesed I want to put it in textBox1 located on Form1&lt;br /&gt;
&lt;br /&gt;
because textBox1 is created in a other thread I get this error:&lt;br /&gt;
&lt;br /&gt;
System.InvalidOperationException was unhandled&lt;br /&gt;
Cross-thread operation not valid: Control 'tbMessages' accessed from a thread other than the thread it was created on.&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
Please help!&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/csharp/309997/309997/cross-thread/</guid>
      <pubDate>Mon, 25 Jul 2005 00:55:01 -0700</pubDate>
      <category>C#</category>
    </item>
    <item>
      <title>Re: Cross thread</title>
      <link>http://www.programmersheaven.com/mb/csharp/309997/310045/re-cross-thread/#310045</link>
      <description>You use a delegate to set the text property on the main thread. Here is an example:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="sourcecode"&gt;
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"});
        }
    }
}
&lt;/pre&gt;&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
Yes, this is alot of work to set a simple property, but i'm sure you can streamline it.&lt;br /&gt;
&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/csharp/309997/310045/re-cross-thread/#310045</guid>
      <pubDate>Mon, 25 Jul 2005 06:38:41 -0700</pubDate>
      <category>C#</category>
    </item>
    <item>
      <title>Re: Cross thread</title>
      <link>http://www.programmersheaven.com/mb/csharp/309997/310069/re-cross-thread/#310069</link>
      <description>: You use a delegate to set the text property on the main thread. Here is an example:&lt;br /&gt;
: &lt;br /&gt;
: &lt;pre class="sourcecode"&gt;
: 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"});
:         }
:     }
: }
: &lt;/pre&gt;&lt;br /&gt;
: &lt;br /&gt;
: 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.&lt;br /&gt;
: &lt;br /&gt;
: Yes, this is alot of work to set a simple property, but i'm sure you can streamline it.&lt;br /&gt;
: &lt;br /&gt;
: &lt;br /&gt;
&lt;br /&gt;
Thank you very very very much. It's works perfectly and so smoothly.&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
delegate&lt;br /&gt;
and object&lt;br /&gt;
&lt;br /&gt;
Thanxz in advance.&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/csharp/309997/310069/re-cross-thread/#310069</guid>
      <pubDate>Mon, 25 Jul 2005 10:58:25 -0700</pubDate>
      <category>C#</category>
    </item>
    <item>
      <title>Re: Cross thread</title>
      <link>http://www.programmersheaven.com/mb/csharp/309997/310099/re-cross-thread/#310099</link>
      <description>A delegate is merely a managed function pointer. When you declare a delegate such as:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="sourcecode"&gt;
public delegate void DoSomethingDeleg();
&lt;/pre&gt;&lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="sourcecode"&gt;
using System;

namespace DelegCSharp
{
	public delegate void DoSomethingDeleg();

	/// &amp;lt;summary&amp;gt;
	/// Summary description for Class1.
	/// &amp;lt;/summary&amp;gt;
	class Class1
	{
		/// &amp;lt;summary&amp;gt;
		/// The main entry point for the application.
		/// &amp;lt;/summary&amp;gt;
		[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");
		}
	}
}
&lt;/pre&gt;&lt;br /&gt;
&lt;br /&gt;
This line:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="sourcecode"&gt;
DoSomethingDeleg doIt = new DoSomethingDeleg(SaySomething);
&lt;/pre&gt;&lt;br /&gt;
&lt;br /&gt;
Creates a function pointer to the function called SaySomething.&lt;br /&gt;
&lt;br /&gt;
This line:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="sourcecode"&gt;
doIt += new DoSomethingDeleg(SaySomethingElse);
&lt;/pre&gt;&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
Finally, this line:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="sourcecode"&gt;
doIt();
&lt;/pre&gt;&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
Delegates are also used for generating call backs:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="sourcecode"&gt;
public static void CallBackSayIt(DoSomethingDeleg sayIt)
{
   if(null != sayIt)
      sayIt();
}
&lt;/pre&gt;&lt;br /&gt;
&lt;br /&gt;
To use this function you could ...&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="sourcecode"&gt;
CallBackSayIt(new DoSomethingDeleg(SaySomething));
&lt;/pre&gt;&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
Delegates are difficult to get use to, but with practice, you can accomplish some advanced techniques.&lt;br /&gt;
---------------------------------------------------------------------&lt;br /&gt;
&lt;br /&gt;
Object&lt;br /&gt;
------&lt;br /&gt;
&lt;br /&gt;
All classes derive directly or indirectly from an object variable. You can store anything in an object.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="sourcecode"&gt;
object obj = 17;
obj = "Hello";
obj = new StringBuilder();
// etc ...
&lt;/pre&gt;&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/csharp/309997/310099/re-cross-thread/#310099</guid>
      <pubDate>Mon, 25 Jul 2005 18:59:11 -0700</pubDate>
      <category>C#</category>
    </item>
    <item>
      <title>Re: Cross thread</title>
      <link>http://www.programmersheaven.com/mb/csharp/309997/310115/re-cross-thread/#310115</link>
      <description>: A delegate is merely a managed function pointer. When you declare a delegate such as:&lt;br /&gt;
: &lt;br /&gt;
: &lt;pre class="sourcecode"&gt;
: public delegate void DoSomethingDeleg();
: &lt;/pre&gt;&lt;br /&gt;
: &lt;br /&gt;
: 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:&lt;br /&gt;
: &lt;br /&gt;
: &lt;pre class="sourcecode"&gt;
: using System;
: 
: namespace DelegCSharp
: {
: 	public delegate void DoSomethingDeleg();
: 
: 	/// &amp;lt;summary&amp;gt;
: 	/// Summary description for Class1.
: 	/// &amp;lt;/summary&amp;gt;
: 	class Class1
: 	{
: 		/// &amp;lt;summary&amp;gt;
: 		/// The main entry point for the application.
: 		/// &amp;lt;/summary&amp;gt;
: 		[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");
: 		}
: 	}
: }
: &lt;/pre&gt;&lt;br /&gt;
: &lt;br /&gt;
: This line:&lt;br /&gt;
: &lt;br /&gt;
: &lt;pre class="sourcecode"&gt;
: DoSomethingDeleg doIt = new DoSomethingDeleg(SaySomething);
: &lt;/pre&gt;&lt;br /&gt;
: &lt;br /&gt;
: Creates a function pointer to the function called SaySomething.&lt;br /&gt;
: &lt;br /&gt;
: This line:&lt;br /&gt;
: &lt;br /&gt;
: &lt;pre class="sourcecode"&gt;
: doIt += new DoSomethingDeleg(SaySomethingElse);
: &lt;/pre&gt;&lt;br /&gt;
: &lt;br /&gt;
: 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.&lt;br /&gt;
: &lt;br /&gt;
: Finally, this line:&lt;br /&gt;
: &lt;br /&gt;
: &lt;pre class="sourcecode"&gt;
: doIt();
: &lt;/pre&gt;&lt;br /&gt;
: &lt;br /&gt;
: 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.&lt;br /&gt;
: &lt;br /&gt;
: Delegates are also used for generating call backs:&lt;br /&gt;
: &lt;br /&gt;
: &lt;pre class="sourcecode"&gt;
: public static void CallBackSayIt(DoSomethingDeleg sayIt)
: {
:    if(null != sayIt)
:       sayIt();
: }
: &lt;/pre&gt;&lt;br /&gt;
: &lt;br /&gt;
: To use this function you could ...&lt;br /&gt;
: &lt;br /&gt;
: &lt;pre class="sourcecode"&gt;
: CallBackSayIt(new DoSomethingDeleg(SaySomething));
: &lt;/pre&gt;&lt;br /&gt;
: &lt;br /&gt;
: 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.&lt;br /&gt;
: &lt;br /&gt;
: Delegates are difficult to get use to, but with practice, you can accomplish some advanced techniques.&lt;br /&gt;
: ---------------------------------------------------------------------&lt;br /&gt;
: &lt;br /&gt;
: Object&lt;br /&gt;
: ------&lt;br /&gt;
: &lt;br /&gt;
: All classes derive directly or indirectly from an object variable. You can store anything in an object.&lt;br /&gt;
: &lt;br /&gt;
: &lt;pre class="sourcecode"&gt;
: object obj = 17;
: obj = "Hello";
: obj = new StringBuilder();
: // etc ...
: &lt;/pre&gt;&lt;br /&gt;
: &lt;br /&gt;
&lt;br /&gt;
Thanxz&lt;br /&gt;
&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/csharp/309997/310115/re-cross-thread/#310115</guid>
      <pubDate>Tue, 26 Jul 2005 01:35:20 -0700</pubDate>
      <category>C#</category>
    </item>
  </channel>
</rss>
