C#异步执行代码,BS/CS通用代码

  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
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值