小弟刚刚学.net,现在不知道怎样在web application中用c#写多线程。谁能给我段多线程例子。俺马上散分。谢谢。
解决方案 »
- repeater中DropDownList三级联动
- media player 网页播放器
- a.master中获取a.master.cs中变量的值
- web服务器连接oracle数据库需要安装什么
- 关于Session过期的问题?
- 北京急招
- vs.net2003部署安装程序能否做成可以浏览选择物理安装路径,谢谢!
- 怎样更新sqlDataAdapter的命令?
- .net,java,pb,还是delphi?
- 急问:在datagrid中有一模板列,编辑时放入dropdownlist控件,要求在点击编辑按钮后,能用程序更改dropdownlist的默认值,如何实现
- 100分:求datagrid模板列问题!!!
- 江雨大哥??急事,看到请进
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.
看看这两篇文章:
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
另外你帮我看看这个:
建立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);
}
}
}
}
看MSDN,有关于异步回调的处理介绍;