请教个多线程问题!
时间:2011-12-20
来源:互联网
在windows 服务程序中
namespace CAGuploadService
{
public partial class uploadService : ServiceBase
{
private Thread thread = null;
public uploadService()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
if (thread == null)
{
this.thread = new Thread(this.ThreadRun);
}
this.thread.IsBackground = true;
this.thread.Start();
}
protected override void OnStop()
{
if (this.thread != null)
{
if (this.thread.ThreadState == System.Threading.ThreadState.Running)
{
this.thread.Abort();
}
}
}
private ClassImage CImage = new ClassImage();
private void ThreadRun()
{
while (true)
{
//---------------最终的程序
uploadImage();
Thread.Sleep(1000*60*10);//让线程休眠10分钟
//---------------最终的程序
}
}
}
}
正常10分钟可以执行一次uploadImage();
现在我想在执行uploadImage();的同时在执行另外的uploadData();
就是想再开一个另外的线程,那位给指教下!
namespace CAGuploadService
{
public partial class uploadService : ServiceBase
{
private Thread thread = null;
public uploadService()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
if (thread == null)
{
this.thread = new Thread(this.ThreadRun);
}
this.thread.IsBackground = true;
this.thread.Start();
}
protected override void OnStop()
{
if (this.thread != null)
{
if (this.thread.ThreadState == System.Threading.ThreadState.Running)
{
this.thread.Abort();
}
}
}
private ClassImage CImage = new ClassImage();
private void ThreadRun()
{
while (true)
{
//---------------最终的程序
uploadImage();
Thread.Sleep(1000*60*10);//让线程休眠10分钟
//---------------最终的程序
}
}
}
}
正常10分钟可以执行一次uploadImage();
现在我想在执行uploadImage();的同时在执行另外的uploadData();
就是想再开一个另外的线程,那位给指教下!
作者: liu68634796 发布时间: 2011-12-20
就是说想同时执行uploadImage();和uploadData();
作者: liu68634796 发布时间: 2011-12-20
C# code
new Thread(delegate() { uploadImage(); }).Start(); new Thread(delegate() { uploadData(); }).Start();
作者: wangsunjun 发布时间: 2011-12-20
不好意思,没有明白意思!
作者: liu68634796 发布时间: 2011-12-20
引用 2 楼 wangsunjun 的回复:
C# code
new Thread(delegate()
{
uploadImage();
}).Start();
new Thread(delegate()
{
uploadData();
}).Start();
C# code
new Thread(delegate()
{
uploadImage();
}).Start();
new Thread(delegate()
{
uploadData();
}).Start();
不好意思,没有明白意思!
作者: liu68634796 发布时间: 2011-12-20
引用 4 楼 liu68634796 的回复:
引用 2 楼 wangsunjun 的回复:
C# code
new Thread(delegate()
{
uploadImage();
}).Start();
new Thread(delegate()
{
uploadData();
}).Start();
不好意思,没有明白意思!
引用 2 楼 wangsunjun 的回复:
C# code
new Thread(delegate()
{
uploadImage();
}).Start();
new Thread(delegate()
{
uploadData();
}).Start();
不好意思,没有明白意思!
她的代码就是传了一个委托进去,比较简练的方式,你也可以在复制一遍你写的代码,创建两个线程,去分别执行就可以了!
C# code
private void ThreadRun() { while (true) { //---------------最终的程序 uploadData(); Thread.Sleep(1000*60*10);//让线程休眠10分钟 //---------------最终的程序 } }
作者: pmars 发布时间: 2011-12-20
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28