Tag Archive: general


When performing any action on a control which requires the updating of a user interface element (e.g. setting the Text property on almost any class derived from Control, updating the data source behind a DataGrid), these operations MUST take place on the thread that created the UI element.

In order to do this, the Control class provides the Invoke method, which will take a delegate and execute it on the thread that the UI element was created on. In order to use this, one must declare a function that performs the UI operation. For example, say a form has a TextBox on it named m_TextBox. To update the text from another thread, create a method that will update the Text property on the TextBox:

			

Where can I find sample C# code for simple threading?

Refer to the System.Threading namespace on MSDN for full details. Meanwhile here is a quick taste.

using System;
using System.Threading; 

class ThreadTest
{
    public void Runme()
    {
        Console.WriteLine("Runme Called");
        Thread.Sleep(10000);
    }

    public static void Main(String[] args)
    {
        ThreadTest b = new ThreadTest();
        Thread t = new Thread(new ThreadStart(b.Runme));
        t.Start(); 

        Console.WriteLine("Thread 't' started.");
Console.WriteLine("
There is no telling when " +
"'Runme' will be invoked. "); t.Join(); Console.WriteLine("Thread 't' has ended."); } }

How can I subscribe to events exposed by a remoted object?

If you set a property on a Windows Forms control which forces the creation of the control (e.g. the SelectedIndex property on the ComboBox class), the control (and perhaps the rest of the form) will not render with visual styles enabled

The resolution is to place the code that sets these properties in an event handler for the Load event on the form/control.

Raghavendra has got some great tips on how to work with visual styles in Windows Forms at http://blogs.msdn.com/rprabhu/category/2838.aspx

Powered by WordPress | Theme: Motion by 85ideas.