当前位置:编程学习 > C#/ASP.NET >>

C#异步执行的线程回收

public void AddLog(string logMsg, int operationType)
        {
            try
            {
                AsyncAddLog asy = new AsyncAddLog(AddLogdele);
                IAsyncResult ia = asy.BeginInvoke(logMsg, operationType, new AsyncCallback(CallbackMethodAddLog), asy);
            }
            catch { }
        }

        private void AddLogdele(string logMsg, int operationType)
        {
            if (ConfigurationManager.AppSettings["LogSwitch"]!=null&&ConfigurationManager.AppSettings["LogSwitch"] == "on")
            {
                BasePage basePage = new BasePage();
                int userID = basePage.CurrentUser.UserID;
                string userName = basePage.CurrentUser.LoginName;
                string bussinessName = base.CurrentModule.FULLNAME;
                DateTime opertionTime = DateTime.Now;
                DateTime responseTime = DateTime.Now;
                DateTime recordTime = DateTime.Now;
                string com_Code = string.Empty;
                if (ConfigurationManager.AppSettings["Com_Code"] != null)
                {
                    com_Code = ConfigurationManager.AppSettings["Com_Code"].ToString();
                }
                string log_Mark = "";
                HttpRequest Request = HttpContext.Current.Request;
                string client_IP = Request.ServerVariables.Get("Remote_Addr").ToString();
                LogHelperController log = new LogHelperController();
                log.AddLog(userID, userName, bussinessName, operationType, opertionTime, responseTime, recordTime, com_Code, log_Mark, client_IP, logMsg);
            }
        }

        /// <summary>
        /// 异步执行完以后回收线程
        /// </summary>
        /// <param name="ar"></param>
        private void CallbackMethodAddLog(IAsyncResult ar)
        {
            AsyncAddLog asy = (AsyncAddLog)ar.AsyncState;
            asy.EndInvoke(ar);
        }

        public delegate void AsyncAddLog(string logMsg, int operationType);

我参考网上的代码写的C#异步程序,关于线程回收的问题,帮我看看我写的对吗?
主要是关于回调函数那块我不太理解它的参数的意思
asy.BeginInvoke(logMsg, operationType, new AsyncCallback(CallbackMethodAddLog), asy); --------------------编程问答-------------------- 路过,谢谢下 --------------------编程问答-------------------- 什么叫做“回收”?这可不是搞什么c语言编程。 --------------------编程问答-------------------- 除 --------------------编程问答--------------------
引用 楼主 zaocha321 的回复:
主要是关于回调函数那块我不太理解它的参数的意思


如果你需要回调则回调,如果不需要则不必写。直接写
public void AddLog(string logMsg, int operationType)
{
    AsyncAddLog asy = new AsyncAddLog(AddLogdele);
    asy.BeginInvoke(logMsg, operationType, null, null);
}

这就足够了。 --------------------编程问答-------------------- 回调运行在用来执行AddLogdele的子线程内部,就好像普通的方法调用一样。你可以给自己异步“通知”,就好像接力一样,你可以在回调方法内部去处理其它方法,这样就可以把你的代码模块化地分为两个部分。

像你写的那样,回调过程中什么也没有执行,完全没有必要写它。

而且异步执行使用 Thread.Start 方法或者其它封装好的方法就够了,使用 Delegate.BeginInvoke 其实没有什么意义。尽管BeginInvoke几乎是运行最慢的异步方法,但是性能不是重点,而是它的参数中多余搞个回调,你的程序中实在是没有必要使用这种东西。

就算是需要在 AddLogdele 完毕之后再干点什么,你也可以直接写在AddLogdele 方法最后,而不需要使用回调。  --------------------编程问答-------------------- 1.首先楼主需要明确,你的代码异步属于是异步委托概念可以谷歌了解一下
2.这个其实很容易理解,执行begininvoke意味着主线程开启一条新线程异步去执行你的委托
3.异步线程同时对你的委托进行监听,当监听到你的委托执行完毕时就会执行回调函数
4.也就是说这个回调函数类似于注册事件,意味着你在执行异步委托时不需要先注册事件再执行。 --------------------编程问答-------------------- 首先,使用 BeginInvoke 多余地多出来一个 Callback 参数以及一个 object 参数,画蛇添足比较累赘。

其次,非常令人惊讶地是,它比其它任何一种异步调用方法都慢许多许多。因此没有人愿意用它。

你的程序如果单单为了“异步”执行可以这样写
public void AddLog(string logMsg, int operationType)
{
   new Thread(()=> AddLogdele(logMsg, operationType)).Start();
}
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,