程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#若何對多線程、多義務治理(demo)

C#若何對多線程、多義務治理(demo)

編輯:C#入門知識

C#若何對多線程、多義務治理(demo)。本站提示廣大學習愛好者:(C#若何對多線程、多義務治理(demo))文章只能為提供參考,不一定能成為您想要的結果。以下是C#若何對多線程、多義務治理(demo)正文


上面一段內容有項目需求有項目剖析,經由過程一個小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#對多線程、多義務治理的辦法,願望對年夜家有所贊助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved