예제 #1
0
        /// <summary>
        ///     在指定域中新建一个看门狗,并在超时后,调用指定的函数,该函数可以为当前域中的实例函数或Action对象
        /// </summary>
        /// <param name="targetDomain">目标域</param>
        /// <param name="softDogName">看门狗名称</param>
        /// <param name="timeOut">超时时间</param>
        /// <param name="action">超时后执行的函数</param>
        /// <returns></returns>
        public static SoftDogInterface CreateSoftDog(AppDomain targetDomain, string softDogName, int timeOut, Action action)
        {
            if (timeOut < 1000)
            {
                throw new ArgumentOutOfRangeException("timeOut", "超时时间不能小于1秒");
            }

            if (string.IsNullOrWhiteSpace(softDogName))
            {
                throw new ArgumentNullException(softDogName);
            }

            if (_cacheCurrentDogs.ContainsKey(softDogName))
            {
                throw new ArgumentOutOfRangeException("softDogName");
            }


            var assemblyName = typeof(SoftDog).Assembly.GetName().FullName;
            var typeName     = typeof(SoftDogServerForInstanceMethod).FullName;

            var call = new CallBackBlock <EventArgs>((sender, o) => action());

            var tempArgs = new object[]
            {
                softDogName
                , timeOut
                , call
            };

            SoftDogInterface dog;

            try
            {
                dog = (SoftDogInterface)targetDomain.CreateInstanceAndUnwrap(assemblyName,
                                                                             typeName
                                                                             , false
                                                                             , BindingFlags.Default
                                                                             , null
                                                                             , tempArgs
                                                                             , Thread.CurrentThread.CurrentCulture
                                                                             , null
                                                                             , null);

                _cacheCurrentDogs.Add(softDogName, dog);

                return(dog);
            }
            catch (Exception)
            {
                throw;
            }
        }
예제 #2
0
 public SoftDogServerForInstanceMethod(string name, int timeout, CallbackTimer.CallbackBlock <EventArgs> func)
     : base(name, timeout)
 {
     _func = func;
 }