您的位置:首页 > 其它

限时等待某一任务完成示例

2014-08-06 17:03 246 查看
<pre name="code" class="csharp">        /// <summary>
/// 获取客户端 1.5s 超时
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
private IRedisClient GetClientByTimeOut(int index, bool throwException = false)
{
if (_redisClients == null || index < 0 || index >= _redisClients.Length)
{
return null;
}

var cm = _redisClients[index];
if (cm == null)
{
return null;
}

int timeout = 1500;//超时ms
var t = System.Threading.Tasks.Task.Factory.StartNew((c) =>
{
var prcm=((PooledRedisClientManager)c);
var cc= prcm.GetClient();
return cc;
}, cm);
bool right = t.Wait(timeout);
if (right) return t.Result;

if (throwException)
{
string info = string.Format("无法获取第{0}个Redis客户端,可能该Redis实例已挂掉。", index);
throw new Exception(info);
}

//logger.Info(info);
return null;
}



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: