asp.net 可以使用多线程吗?

x_stream 2009-01-30 11:01:49
asp.net 可以使用多线程吗?
如果可以,怎么实现同时运行N个线程,某个线程运行结束,主动在放列队线程去运行。
...全文
234 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
Joson.e8love 2009-03-12
  • 打赏
  • 举报
回复
受益匪浅
king19840811 2009-01-31
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 winner2050 的回复:]
C# code
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 p…
[/Quote]

winner你还没到3星啊
chaye12 2009-01-31
  • 打赏
  • 举报
回复
楼上说的很多,顶下!!
winner2050 2009-01-31
  • 打赏
  • 举报
回复

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 有点不一样,不能实时显示进度。

可以通过在多线程里面进度写入到缓存,再由网页读取缓存,显示进度。
conan19771130 2009-01-31
  • 打赏
  • 举报
回复
mark
cat_hsfz 2009-01-31
  • 打赏
  • 举报
回复
可以,你要在哪裡實現多線程?
宝_爸 2009-01-31
  • 打赏
  • 举报
回复
可以,最好用线程池。
参见

Multithreading in ASP.NET
http://www.beansoftware.com/ASP.NET-Tutorials/Multithreading-Thread-Pool.aspx

ASP.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-CN

good luck
niitnanfeng 2009-01-31
  • 打赏
  • 举报
回复
顶下
winner2050 2009-01-31
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 king19840811 的回复:]
winner你还没到3星啊
[/Quote]

过年太忙了,没有时间JF。

一个星期才得100多分。
ZJ159 2009-01-31
  • 打赏
  • 举报
回复
cs78799662 2009-01-31
  • 打赏
  • 举报
回复
UP
wfyfngu 2009-01-31
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 findcaiyzh 的回复:]
可以,最好用线程池。
[/Quote]

这个最好,楼主可以多看看异步操作方面的例子。
CutBug 2009-01-30
  • 打赏
  • 举报
回复
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);

}
}
}

}
fengjian_428 2009-01-30
  • 打赏
  • 举报
回复
可以 视语言不同而定

62,266

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧