+ -
当前位置:首页 → 问答吧 → 请教个多线程问题!

请教个多线程问题!

时间: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();
就是想再开一个另外的线程,那位给指教下!

作者: 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();


不好意思,没有明白意思!

作者: liu68634796   发布时间: 2011-12-20

引用 4 楼 liu68634796 的回复:

引用 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

相关阅读 更多