public class AsyncThread { #region public AsynsProp asyprop { get; set; } public int isStart { get; set; } //0 使用完毕 1 正在使用 2 初始化 public class AsynsProp { /// 用于线程加锁 /// </summary> public string objid { get; set; } public delegate string ProcessTask(); public object asynclock { get; set; } //存放执行状态 public IDictionary<string, int> ProcessStatus { get; set; } public AsynsProp() { if (objid == null) //objid = guidid; objid = System.Guid.NewGuid().ToString(); if (ProcessStatus == null) { ProcessStatus = new Dictionary<string, int>(); } if (asynclock == null) { asynclock = new object(); } } public void Add() { string id = objid; lock (asynclock) { if (ProcessStatus != null) { if (!ProcessStatus.ContainsKey(id)) { ProcessStatus.Add(id, 0); } } } } public void Remove() { string id = objid; lock (asynclock) { if (ProcessStatus != null) { if (ProcessStatus.ContainsKey(id)) { ProcessStatus.Remove(id); } } } } public void Add(string id) { lock (asynclock) { if (ProcessStatus != null) { if (!ProcessStatus.ContainsKey(id)) { ProcessStatus.Add(id, 0); } } } } public void Remove(string id) { lock (asynclock) { if (ProcessStatus != null) { if (ProcessStatus.ContainsKey(id)) { ProcessStatus.Remove(id); } } } } /// <summary> /// Gets the status. /// </summary> /// <param name="id">The id.</param> public int GetStatus() { string id = objid; lock (asynclock) { if (ProcessStatus != null) { if (!ProcessStatus.ContainsKey(id)) { return -1; } if (ProcessStatus.Keys.Count(x => x == id) == 1) { return ProcessStatus[id]; } else { return -1; } } else { return -1; } } } public int GetStatus(string id) { lock (asynclock) { if (ProcessStatus != null) { if (ProcessStatus.ContainsKey(id)) { if (ProcessStatus.Keys.Count(x => x == id) == 1) { return ProcessStatus[id]; } else { return -1; } } else return -1; } else { return -1; } } } } /// <summary> public AsyncThread() { if (asyprop == null) { asyprop = new AsynsProp(); isStart = 2; } } /// <summary> /// 使用委托做参数执行外面的代码 /// </summary> /// <param name="processTask"></param> public void StartLongRunningProcess(AsynsProp.ProcessTask processTask) { //id = System.Guid.NewGuid().ToString(); int rs = asyprop.GetStatus(); if (rs != -1) { return; } if (processTask != null) { asyprop.Add(asyprop.objid); processTask.BeginInvoke(new AsyncCallback(EndLongRunningProcess), processTask); isStart = 1; } } /// <summary> /// Ends the long running process. /// </summary> /// <param name="result">The result.</param> public void EndLongRunningProcess(IAsyncResult result) { AsynsProp.ProcessTask processTask = (AsynsProp.ProcessTask)result.AsyncState; string id = processTask.EndInvoke(result); var currentProgress = asyprop.GetStatus(id).ToString(); asyprop.Remove(id); isStart = 0; } public string GetPercentage(int value, int maxvalue) { if (value > maxvalue|| value==-1) { value = maxvalue; } string per = ((double)value / maxvalue).ToString("P"); return per; } public int GetStatus() { return asyprop.GetStatus(); }
public int GetPercentageStatus(int maxvalue) { int value = GetStatus(); if (value > maxvalue || value == -1) { value = maxvalue; } return value; }
public string GetPercentageStatus(int maxvalue) { int value = GetStatus(); if (value > maxvalue || value == -1) { value = maxvalue; } string per = ((double)value / maxvalue).ToString("P"); return per; } #endregion #region 使用例子 //声明 //AsyncThread asyn = new AsyncThread(); /****************** 执行代码 StartRun*************************/ //2 初始化 1 正在使用 //if (asyn.isStart != 1 || asyn.isStart==2) // { // asyn = new AsyncThread(); // asyn.StartLongRunningProcess(StartRun); // } //public string StartRun() //{ // for (int i = 1; i <= 99999999; i++) // { // //System.Threading.Thread.Sleep(1000); // lock (asyn.asyprop.asynclock) // { // asyn.asyprop.ProcessStatus[asyn.asyprop.objid] = i; // } // } // return asyn.asyprop.objid; //} /*******************************************/ /*******************得到当前执行状态************************/ //asyn.GetPercentageStatus(99999999) #endregion } 尊重原创:转载请帖出出处 :http://blog.csdn.net/osmeteor/article/details/24454391
C#异步执行代码,BS/CS通用代码
最新推荐文章于 2023-09-14 09:51:11 发布