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

语法求助,将方法作为参数传值,并且接收返回值, 我该怎么写?


在线等, 立马结帖. .......

Delegate利用委托,将方法作参数传值,并且接收该方法的返回值, 语法该怎么写?
意思就是model传值进:GetArticlePageContent这个方法, 并且接收GetArticlePageContent方法的返回值。

主程序代码:
  bool bValue = false;
                    int iOverTime = 100000; 
StModel model=bll.GetStModel(5);
                    bValue = XCommon.OverTimeCntrol.CallFuncThread(GetArticlePageContent, TimeSpan.FromMilliseconds(iOverTime), null);

                    if (!bValue)
                    {
                        strStatus = "读取网址超时(" + iOverTime / 1000 + "秒),跳过!";
                    }
                    else
                    {
//处理GetArticlePageContent方法返回值;

}

   public static int GetArticlePageContent()
       {
....
     return stauts;
        }






  public class OverTimeCntrol
    {
        public delegate void Delegate();
        /// <summary>
        /// 执行指定的方法,如果在指定的时间之内没有完成,则中止
        /// </summary>
        /// <param name="func">任务过程</param>
        /// <param name="timeSpan">超时时间</param>
        /// <param name="timeoutCallback">如果超时,则调用该方法</param>
        /// <returns>是否正确执行完毕</returns>
        public static bool CallFuncThread(Delegate func, TimeSpan timeSpan, Delegate timeoutCallback)
        {
            if (func == null)
                throw new ArgumentNullException("func");

            ManualResetEvent resetEvent = new ManualResetEvent(false);
            ManualResetEvent waitThreadEvent = new ManualResetEvent(false);

            Exception error = null;
            Thread thread = null;

            // 将任务加到线程当中
            ThreadPool.QueueUserWorkItem(delegate
            {

                thread = Thread.CurrentThread;
                try { func(); }
                catch (ThreadAbortException) { }
                catch (Exception ex) { error = ex; }

                resetEvent.Set();
                waitThreadEvent.WaitOne();  // 每次线程执行结束都等待后续的处理逻辑
            });

            try
            {
                bool result = resetEvent.WaitOne(timeSpan, false);  // 等待任务的结束

                if (error != null)  // 说明在执行过程中出现异常,直接抛出异常
                    throw error;

                if (!result)
                {
                    if (thread != null)
                    {
                        thread.Abort();  // 此时可以确保该线程没有开始运行新的任务
                        waitThreadEvent.Set();
                    }

                    if (timeoutCallback != null)
                        timeoutCallback();
                }

                return result;
            }
            finally
            {
                waitThreadEvent.Set();  // 最后确保释放线程池线程
            }
        }
    }
delegate 线程池 class --------------------编程问答-------------------- call函数中 用变量接受 func 的值 --------------------编程问答-------------------- CallFuncThread的返回值最少应该是
public struct XXX
{
    public bool IsValue;
    public object Value;
}
--------------------编程问答--------------------
引用
call函数中 用变量接受 func 的值 

+1 --------------------编程问答--------------------
引用 1 楼 bdmh 的回复:
call函数中 用变量接受 func 的值


亲爱的, 能不能详细点? 写写示例神马嘀。 --------------------编程问答-------------------- 没人帮忙啊?
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,