asp.net 可以使用多线程吗?
如果可以,怎么实现同时运行N个线程,某个线程运行结束,主动在放列队线程去运行。

解决方案 »

  1.   

    using System;
    using System.Collections.Generic;
    using System.Threading;
    using System.Collections;
    namespace Test
    {
        class Program 
        {
            static ArrayList arraylist = new ArrayList();
            static Thread[] threads = new Thread[5];
            /// <summary>
            /// clear arraylist finished self-defined method
            /// </summary>
            static event EventHandler OnFinished;
            static void Main()
            {
                #region 初始化数据
                Random r = new Random();
                for (int i = 0; i < 50; i++)
                {
                    arraylist.Add(r.Next(1000));
                }
                #endregion             Console.WriteLine("clear arraylist begin!");
                for(int i=0;i<threads.Length;i++)
                {
                    threads[i] = new Thread(new ThreadStart(DelMorethen50));
                    threads[i].Name = "thread" + i.ToString();
                    threads[i].Start();
                }
                OnFinished += new EventHandler(Program_OnFinished);            
            }        /// <summary>
            /// 清除完成执行事件
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            static void Program_OnFinished(object sender, EventArgs e)
            {
                Console.WriteLine("clear arraylist finished!");
                for (int i = 0; i < threads.Length; i++)
                {
                    threads[i].Abort();//终止线程
                }
                
            }        /// <summary>
            /// 线程调用方法
            /// </summary>
            static void DelMorethen50()
            {
                while (true)
                {
                    Monitor.Enter(arraylist.SyncRoot);
                    if (arraylist.Count == 0) Program_OnFinished(arraylist.SyncRoot, new EventArgs());
                    int num = (int)arraylist[0];
                    arraylist.RemoveAt(0);
                    Console.WriteLine("{0}\t{1}\t{2}", Thread.CurrentThread.Name, "deleted", num);
                    Monitor.Exit(arraylist.SyncRoot);
                    Thread.Sleep(20);            }
            }
        }
        
    }
      

  2.   

    可以,最好用线程池。
    参见Multithreading in ASP.NET 
    http://www.beansoftware.com/ASP.NET-Tutorials/Multithreading-Thread-Pool.aspxASP.NET多线程范例1
    http://blog.csdn.net/gloomyboyo/archive/2006/08/09/1042187.aspx还有一个ms的教程:
    ASP.NET多线程编程(一)
    http://msevents.microsoft.com/CUI/EventDetail.aspx?EventID=1032327989&Culture=zh-CNgood luck
      

  3.   


    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Threading;
    using System.Net;
    namespace 多线程列队
    {
        public partial class Form4 : Form
        {
            public Form4()
            {
                InitializeComponent();
            }
            static public ManualResetEvent synchro = null;
            private int n  ;     //并发数   
            private int num = -1;
            private int TT ;        private void Start()
            {
                for (int m = 0; m < TT; m++)//共有100个   
                {
                    ThreadStart myThreadDelegate = new ThreadStart(this.go);
                    Thread myThread = new Thread(myThreadDelegate);                if (n > 0)
                    {
                        n--;
     
                        Add("@启动线程-   " + m);
                        myThread.Start();
                    }
                    else
                    {
                        synchro = new ManualResetEvent(false);
                        synchro.WaitOne();     //等待   
                        n--;
                        Add("@启动线程-   " + m);
                        myThread.Start();
                    }
                }
            }        public void go()
            {
                int i = 0;
                i = Interlocked.Increment(ref num);
                Add2("执行线程-   " + i + " " + DateTime.Now);
                
                //模拟任务执行时间
                //Thread.Sleep(RandomTime());
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.163.com");
                request.ProtocolVersion = HttpVersion.Version11;
                request.Credentials = CredentialCache.DefaultCredentials;
                request.Timeout = 60 * 1000;//60秒钟
                request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)";            HttpWebResponse response = (HttpWebResponse)request.GetResponse();            n++;
                if (n > 0)
                {
                    synchro.Set();//激活等待
                }
                Add2("结束线程-   " + i + " " + DateTime.Now);
                Remove("@启动线程-   " + i);
            }
            private int RandomTime()
            {
                Random R = new Random();            return R.Next(1000, 12000);
            }                #region 跨线程控制控件
            public delegate void OutPutDelegate(object outputStr);
            public void Add(object outputStr)
            {
                if (this.InvokeRequired)
                {
                    OutPutDelegate opd = new OutPutDelegate(Add);
                    this.BeginInvoke(opd, new object[] { outputStr });
                }
                else
                {
                    listBox1.Items.Add(outputStr);
                }
            }
            public void Remove(object outputStr)
            {
                if (this.InvokeRequired)
                {
                    OutPutDelegate opd = new OutPutDelegate(Remove);
                    this.BeginInvoke(opd, new object[] { outputStr });
                }
                else
                {
                    listBox1.Items.Remove(outputStr);
                }
            }        public delegate void OutPutDelegate2(object outputStr);
            public void Add2(object outputStr)
            {
                if (this.InvokeRequired)
                {
                    OutPutDelegate2 opd = new OutPutDelegate2(Add2);
                    this.BeginInvoke(opd, new object[] { outputStr });
                }
                else
                {
                    listBox2.Items.Add(outputStr);
                }
            }
            public void Remove2(object outputStr)
            {
                if (this.InvokeRequired)
                {
                    OutPutDelegate2 opd = new OutPutDelegate2(Remove2);
                    this.BeginInvoke(opd, new object[] { outputStr });
                }
                else
                {
                    listBox2.Items.Remove(outputStr);
                }
            }
            #endregion        private void button1_Click(object sender, EventArgs e)
            {
                n = Convert.ToInt32(textBox1.Text.Trim());
                TT = Convert.ToInt32(textBox2.Text.Trim());
                Thread th = new Thread(new ThreadStart(Start));
                th.Start();        }
        }
    }
    asp.net 跟Winfrom 有点不一样,不能实时显示进度。可以通过在多线程里面进度写入到缓存,再由网页读取缓存,显示进度。
      

  4.   

    http://www.cnblogs.com/zhangaz1/articles/1073175.html
    http://www.cnblogs.com/GavinCome/archive/2008/06/03/1212966.html
      

  5.   

    http://www.cnblogs.com/GavinCome/archive/2008/06/03/1212966.html