
主线程由于控制主程序,并且可以显示下载进度;子线程用于做文件的下载,并且要每隔一段时间与主线程通信,把进度汇报给主线程。
至于线程的控制、以及文件的下载(涉及到所采用的网络通信协议),需要结合你所使用的 *** 作系统提供的API了。
using Systemusing System.Threading
namespace ConsoleApplication2
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
class Class1
{
static object obj = new object()
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
Class1 c = new Class1()
System.Threading.Thread.CurrentThread.Name = "主线程"
Thread t = new Thread(new ThreadStart(c.method))
t.Name = "子线程"
t.Start()//启动子线程调用方法
c.method()//主线程调用方法
}
int i = 0
void method()
{
// lock(this)
// {
for( i <10 i ++)
{
Console.WriteLine("线程:{0},结果:{1}",Thread.CurrentThread.Name,i)
}
// }
}
}
}
老师上课给的例子 -..-
不明白可以来问我哈
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)