using System;
using System.Threading;// The ThreadWithState class contains the information needed for
// a task, the method that executes the task, and a delegate
// to call when the task is complete.
//
public class ThreadWithState {
    // State information used in the task.
    private string boilerplate;
    private int value;    // Delegate used to execute the callback method when the
    // task is complete.
    private ExampleCallback callback;    // The constructor obtains the state information and the
    // callback delegate.
    public ThreadWithState(string text, int number, 
        ExampleCallback callbackDelegate) 
    {
        boilerplate = text;
        value = number;
        callback = callbackDelegate;
    }
    
    // The thread procedure performs the task, such as
    // formatting and printing a document, and then invokes
    // the callback delegate with the number of lines printed.
    public void ThreadProc() 
    {
        Console.WriteLine(boilerplate, value);
        if (callback != null)
            callback(1);
    }
}// Delegate that defines the signature for the callback method.
//
public delegate void ExampleCallback(int lineCount);// Entry point for the example.
//
public class Example 
{
    public static void Main() 
    {
        // Supply the state information required by the task.
        ThreadWithState tws = new ThreadWithState(
            "This report displays the number {0}.",
            42,
            new ExampleCallback(ResultCallback)
        );        Thread t = new Thread(new ThreadStart(tws.ThreadProc));
        t.Start();
        Console.WriteLine("Main thread does some work, then waits.");
        t.Join();
        Console.WriteLine(
            "Independent task has completed; main thread ends."); 
    }    // The callback method must match the signature of the
    // callback delegate.
    //
    public static void ResultCallback(int lineCount) 
    {
        Console.WriteLine(
            "Independent task printed {0} lines.", lineCount);
    }
}

解决方案 »

  1.   


    vs2003调试通过
    使用一个事件就行了。
    *************************************************************
    Form1.cs
    **************************************************************/private void button1_Click(object sender, System.EventArgs e)
    {
    Class1 c= new Class1();
    c.doSomething += new Class1.DoSomething(DoSome);
    c.Start ();
    }private void DoSome(int i)
    {
    label1.Text = i.ToString();
    }
    /*************************************************************
    Class1.cs
    **************************************************************/using System;
    using System.Threading ;namespace test
    {//public DoSomething doSomething = null;
    /// <summary>
    /// Class1 的摘要说明。
    /// </summary>
    public class Class1
    {
    //定义委托
    private delegate void DoSomething (int i);
    //定义事件
    public event DoSomething doSomething;
    public Class1()
    {
    //
    // TODO: 在此处添加构造函数逻辑
    //
    }private void Thread1()
    {
    for(int i =0 ;i<1000;i++)
    {
    //触发事件
    doSomething(i);
    }
    }public void Start()
    {
    Thread thread = new  Thread(new ThreadStart(Thread1));
    thread.IsBackground =true;
    thread.Name="thread1";
    thread.Start();
    }
    }
    }
      

  2.   

    上面的方法VS2003好使VS2005就不行了不知楼主用的是什么?
      

  3.   

    msdn里有详细的代码啊,就是用委托回调。
      

  4.   

    看一看Backgroudworker.(.Net 2005)新加的组件,专门处理后台线程。
      

  5.   

    using System.Runtime.InteropServices;
     [DllImport("User32.dll")]
     private static extern bool SendMessage(IntPtr hWnd, int wMsg, int wParam, int lParam);把SendMessage()这个函数导进来,就可以用了