欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#如何对多线程、多任务管理(demo)

程序员文章站 2023-11-16 23:25:04
下面一段内容有项目需求有项目分析,通过一个小demo给大家展示下c#如何对多线程、多任务管理的。 项目需求:假设多个任务需要执行,每个任务不是一时半会能完成(需要能看到中...

下面一段内容有项目需求有项目分析,通过一个小demo给大家展示下c#如何对多线程、多任务管理的。

项目需求:假设多个任务需要执行,每个任务不是一时半会能完成(需要能看到中间执行状况);

         多个任务 根据条件不同 可能需要不同的处理

项目分析: 多线程并发执行多任务;

         对任务进行管理,追踪中间执行状态;

         运用策略模式抽象执行类;

public enum taskstatus
  {
    wait = 0,
    working = 1,
    stop = 2,
    suspend = 3,
    complete = 4,
    fail = 5,
    other = 99
  }

 public class task
   {
     public taskdelegate<parastatus> taskstatus;
     public taskdelegate<parainfo> taskinfo;
     public taskdelegate<paraprocess> taskprocess;
 
     private taskstatus _status = taskstatus.wait;
     private iexecuter _executer { get; set; }
     private guid _taskid;
     private thread thread;
     private string _name;
 
     public task(guid id, iexecuter executer)
     {
       _taskid = id;
       _executer = executer;
       _executer.taskid = id;
       _executer.task = this;
     }
 
     public void wait()
     {
       changestatus(taskstatus.wait);
     }
 
     public string name
     {
       get
       {
         return _name;
       }
       set
       {
         _name = value;
       }
     }
     
     public guid taskid
     {
       get
       {
         return _taskid;
       }
     }
     
     public taskstatus status
     {
       get
       {
         return _status;
       }
       set
       {
         _status = value;
       }
     }    
 
     public void start()
     {
       if (thread == null)
       {
         thread = new thread(_executer.do);
       }
       else if (thread.threadstate == threadstate.stopped)
       {
         thread = new thread(_executer.do);
       }
       thread.start();
       changestatus(taskstatus.working);
     }
     public void changestatus(taskstatus s)
     {
       status = s;
       parastatus ps = new parastatus(taskid);
       ps.status = status;
       if (taskstatus != null) taskstatus(ps);
     }
     public void stop()
     {
       if (thread != null)
       {
         if (thread.isalive)
         {
           try
           {
             thread.abort();
           }
           catch { }
         }
       }
       changestatus(taskstatus.stop);
     }
     public void suspend()
     {
       try
       {
         thread.suspend();
       }
       catch { }
       changestatus(taskstatus.suspend);
     }
     public void resume()
     {
       if (thread.isalive)
       {
         thread.resume();
       }
       changestatus(taskstatus.working);
     }
   }

public interface iexecuter
  {
    guid taskid { get; set; }

    task task { get; set; }
    void do();
  }



public class executer : iexecuter
  {
    public void execute()
    {

    }
    #region iexecute
    public guid taskid { get; set; }

    public task task { get; set; }

    public void do()
    {
      for (int i = 1; i <= 100; i++)
      {
        if (task.taskprocess != null)
        {
          thread.sleep(1000);
          paraprocess pp = new paraprocess(taskid);
          pp.rate = (double)i / 100;
          task.taskprocess(pp); 
        }
      }
      if (task.taskstatus != null)
      {
        task.changestatus(taskstatus.complete);
      }
    }
    #endregion
  }

public class taskmanager
  {
    public list<task> tasks = new list<task>();
    /// <summary>
    /// 添加任务
    /// </summary>
    public task addtask(task task)
    {
      try
      {
        tasks.add(task);

        task.wait();
      }
      catch (exception e)
      {

      }
      return task;
    }
    public void starttask(task task)
    {
      task.start();
    }
    /// <summary>
    /// 停止任务
    /// </summary>
    /// <param name="downloader"></param>
    public void stoptask(task task)
    {
      task.stop();
    }

    public void suspendtask(task task)
    {
      task.suspend();
    }

    public void resumetask(task task)
    {
      task.resume();
    }

    /// <summary>
    /// 删除任务(自动终止未停止的任务)
    /// </summary>
    /// <param name="downloader"></param>
    public void deletetask(task task, bool deletefile)
    {
      //先停止任务
      task.stop();

      while (task.status == taskstatus.working)
      {
        thread.sleep(50);
      }

      //从任务列表中删除任务
      if (tasks.contains(task))
      {
        tasks.remove(task);
      }
    }

  }


 /// </summary>
  /// <param name="para">泛型参数</param>
  public delegate void taskdelegate<t>(t para);

  /// <summary>
  /// 
  /// </summary>
  public class parastatus
  {
    public parastatus(guid task) { taskid = task; }
    public guid taskid { get; set; }
    public taskstatus status { get; set; }
  }  

  public class parainfo
  {
    public parainfo(guid task) { taskid = task; }
    public guid taskid { get; set; }
    public string info { get; set; }
  }

  public class paraprocess
  {
    public paraprocess(guid task) { taskid = task; }
    public guid taskid { get; set; }
    public double rate { get; set; }    
  }

以上代码内容就是c#对多线程、多任务管理的方法,希望对大家有所帮助。