小弟刚刚学.net,现在不知道怎样在web application中用c#写多线程。谁能给我段多线程例子。俺马上散分。谢谢。

解决方案 »

  1.   

    seeExecuting Long Running Operations - Part I/IIhttp://www.dotnetbips.com/displayarticle.aspx?id=271http://www.dotnetbips.com/displayarticle.aspx?id=272
      

  2.   

    MSDN中的多线程例子
    using System;
    using System.Threading;// Simple threading scenario:  Start a static method running
    // on a second thread.
    public class ThreadExample {
        // The ThreadProc method is called when the thread starts.
        // It loops ten times, writing to the console and yielding 
        // the rest of its time slice each time, and then ends.
        public static void ThreadProc() {
            for (int i = 0; i < 10; i++) {
                Console.WriteLine("ThreadProc: {0}", i);
                // Yield the rest of the time slice.
                Thread.Sleep(0);
            }
        }    public static void Main() {
            Console.WriteLine("Main thread: Start a second thread.");
            // The constructor for the Thread class requires a ThreadStart 
            // delegate that represents the method to be executed on the 
            // thread.  C# simplifies the creation of this delegate.
            Thread t = new Thread(new ThreadStart(ThreadProc));
            // Start ThreadProc.  On a uniprocessor, the thread does not get 
            // any processor time until the main thread yields.  Uncomment 
            // the Thread.Sleep that follows t.Start() to see the difference.
            t.Start();
            //Thread.Sleep(0);        for (int i = 0; i < 4; i++) {
                Console.WriteLine("Main thread: Do some work.");
                Thread.Sleep(0);
            }        Console.WriteLine("Main thread: Call Join(), to wait until ThreadProc ends.");
            t.Join();
            Console.WriteLine("Main thread: ThreadProc.Join has returned.  Press Enter to end program.");
            Console.ReadLine();
        }
    }
    [C++] // [C++]
    // Compile using /clr option.
    #using <mscorlib.dll>
     using namespace System;
     using namespace System::Threading;
     // Simple threading scenario:  Start a Shared method running
     // on a second thread.
     public __gc class ThreadExample 
     {
     public:
         // The ThreadProc method is called when the thread starts.
         // It loops ten times, writing to the console and yielding 
         // the rest of its time slice each time, and then ends.
         static void ThreadProc()
         {
             for (int i = 0; i < 10; i++) 
             {
                 Console::Write("ThreadProc: ");
                 Console::WriteLine(i);
                 // Yield the rest of the time slice.
                 Thread::Sleep(0);
             }
         }
     };
     
     int main() 
     {
         Console::WriteLine(S"Main thread: Start a second thread.");
         // Create the thread, passing a ThreadStart delegate that
         // represents the ThreadExample::ThreadProc method.  For a 
         // delegate representing a static method, no object is
         // required.
         Thread *oThread = new Thread(new ThreadStart(0, &ThreadExample::ThreadProc));
     
         // Start the thread.  On a uniprocessor, the thread does not get 
         // any processor time until the main thread yields.  Uncomment
         // the Thread.Sleep that follows t.Start() to see the difference.
         oThread->Start();
         //Thread::Sleep(0);     for (int i = 0; i < 4; i++) {
             Console::WriteLine("Main thread: Do some work.");
             Thread::Sleep(0);
         }     Console::WriteLine("Main thread: Call Join(), to wait until ThreadProc ends.");
         oThread->Join();
         Console::WriteLine("Main thread: ThreadProc.Join has returned.  Press Enter to end program.");
         Console::ReadLine();
         return 0;
     }
    [Visual Basic, C#, C++] This code produces output similar to the following:
    [VB, C++, C#]
    Main thread: Start a second thread.
    Main thread: Do some work.
    ThreadProc: 0
    Main thread: Do some work.
    ThreadProc: 1
    Main thread: Do some work.
    ThreadProc: 2
    Main thread: Do some work.
    ThreadProc: 3
    Main thread: Call Join(), to wait until ThreadProc ends.
    ThreadProc: 4
    ThreadProc: 5
    ThreadProc: 6
    ThreadProc: 7
    ThreadProc: 8
    ThreadProc: 9
    Main thread: ThreadProc.Join has returned.  Press Enter to end program.
      

  3.   

    我想用的是web应用程序,也就是web页面中的多线程。
      

  4.   

    那可不能随便开线程了,你这个要求实现起来不算容易
    看看这两篇文章:
    Building a Better Wait Page
    http://www.codeproject.com/aspnet/wait_page.aspProcessing Long Running Tasks With Asynchronous Handlers and XMLHTTP
    http://www.codeproject.com/aspnet/asynctransactionhandler.asp
      

  5.   

    第二个好像和我的好像阿,但是太长了,我看不明白
    另外你帮我看看这个:
    建立web应用程序的聊天室,本来我想实现在发送按钮点击后,把输入的字符串发送给所有的连接上了web服务器的客户端,但现在点击发送按钮时,只有自己能看到,其他的客户端的页面没有变化,为什么?// Content.aspx.cs ///////////////////////////////////////////////////////
    public class Content : System.Web.UI.Page
    {
    private void Page_Load(object sender, System.EventArgs e)
    {
    Application[Session.SessionID] = Response;

    }
    }
    // Send.aspx.cs //////////////////////////////////////////////////////////
    public class Send : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.Button ButtonSend;
    protected System.Web.UI.WebControls.TextBox TextBox1;                   private void Page_Load(object sender, System.EventArgs e)
    {
    Application[Session.SessionID] = Response;

    }
    private void ButtonSend_Click(object sender, System.EventArgs e)
    {
                               String text=TextBox1.Text;
             foreach(string name in Application.AllKeys)
    {
    HttpResponse Response = Application[name] as HttpResponse;
    if(Response!=null && Response.IsClientConnected)
    {
    Response.Write(text+ "<br>\n");
    Response.Flush();
    }
    else
    {
    Application.Remove(name);
    }
    }
    }
    }
      

  6.   

    在WEB中,多线程是不能实现的,但是可以模拟多线程,那就是异步回调;
    看MSDN,有关于异步回调的处理介绍;