超出时间跳出

需求:因项目中出现一个调用和获取数据库中数据 产生时间差异的问题()

参考别人 自己写的一个超出时间跳出的方法

// timeoutMilliseconds 需要等待的时间

//List<Record> 返回的数据类型 (本人是自定义的一种Record类型)

 public static List<Record> CallWithTimeout(, int timeoutMilliseconds)
        {
            Thread thread = null;
            List<Record> resulta = new List<Record>();
            Func<List<Record>> wrappedFunc = () =>
            {
                thread = Thread.CurrentThread;
                while (!resulta.Any())
                {
                    //resulta = 调用数据库的方法();
                }
                return resulta;
            };

            IAsyncResult result = wrappedFunc.BeginInvoke(null, null);

//根据时间筛选跳出整体,返回数据
            if (result.AsyncWaitHandle.WaitOne(timeoutMilliseconds))
            {
                return wrappedFunc.EndInvoke(result);    //方法成功执行返回值
            }
            else
            {
                thread.Abort();
                return new List<Record>();    //方法中止返回值
            }
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值