线程
C#中线程应用,线程是什么时候启动的,线程能自动启动吗?谢谢!如果有示例就最好了。嘻嘻 --------------------编程问答-------------------- 线程一般的就是用在异步处理上,通常需要长时间运行的需要用到。比如:导出10万的记录,导入1万以上的记录,大数据的数据处理。
实现异步的处理,不一定用线程,以下是使用异步调用委托实现的代码:
using System;
using System.Collections;
using System.Data;
using System.Configuration;
using System.IO;
namespace NovaNet.Public.AsyncTask
{
/// <summary>
/// 异步任务的根类
/// </summary>
public class AsyncTaskBase
{
public Hashtable CallParam = new Hashtable();
public byte[] Result;
public bool Error;
public AsyncTaskBase()
{
}
public virtual void Execute()
{
AsyncMethodCaller caller = new AsyncMethodCaller(AsyncExecute);
CallParam["Caller"] = caller;
Error = false;
IAsyncResult result = caller.BeginInvoke(new AsyncCallback(TaskCallbackMethod),null);
}
protected virtual byte[] AsyncExecute()
{
return null;
}
protected void TaskCallbackMethod(IAsyncResult ar)
{
try
{
Result = (CallParam["Caller"] as AsyncTaskBase.AsyncMethodCaller).EndInvoke(ar) as byte[];
}
catch(Exception E)
{
MemoryStream AStream = new MemoryStream();
StreamWriter AStreamWriter = new StreamWriter(AStream);
AStreamWriter.Write(E.Message + "\nSource:" + E.Source + "\n" + E.StackTrace);
AStreamWriter.Flush();
Result = AStream.ToArray();
Error = true;
}
if (OnCompleted != null)
OnCompleted(this, new EventArgs());
}
public event EventHandler OnCompleted;
public delegate object AsyncMethodCaller();
}
} --------------------编程问答-------------------- 介绍线程应用的资料,我想随便一搜就一大堆了吧..
补充:.NET技术 , ASP.NET