问题描述
我怎么能不阻塞线程,没有第三方物流ContinueWith执行的等待?外汇。 3.5?我了解TPL的移植版本3.5(由RX队),但我很好奇,想知道 - 我可以使用哪些线程原语为(...什么是TPL behing的场景)。什么ContinueWith替代品TPL?
//将异步IO操作过程中该处理程序块线程?
公共类AsyncHandler:IHttpAsyncHandler
{
公共无效的ProcessRequest(HttpContext的CTX)
{
// 不曾用过
} 公共BOOL IsReusable
{
获得{返回false; }
} 公众的IAsyncResult BeginProcessRequest(HttpContext的CTX,
CB的AsyncCallback,
obj对象)
{
AsyncRequestState reqState =
新AsyncRequestState(CTX,CB,OBJ);
AsyncRequest AR =新AsyncRequest(reqState);
的ThreadStart TS =新的ThreadStart(ar.ProcessRequest);
线程t =新主题(TS);
t.Start(); 返回reqState;
} 公共无效EndProcessRequest(IAsyncResult的AR)
{
AsyncRequestState ARS = AR作为AsyncRequestState;
如果(ARS!= NULL)
{
//一些清理
}
}
}
类AsyncRequestState:IAsyncResult的
{
公共AsyncRequestState(HttpContext的CTX,
CB的AsyncCallback,
对象extraData)
{
_ctx = CTX;
_cb = CB;
_extraData = extraData;
} 内部的HttpContext _ctx;
内部的AsyncCallback _cb;
内部对象_extraData;
私人布尔_isCompleted = FALSE;
私人的ManualResetEvent _callCompleteEvent = NULL; 内部空隙CompleteRequest()
{
_isCompleted = TRUE;
锁定(本)
{
如果(_callCompleteEvent!= NULL)
_callCompleteEvent.Set();
}
如果(_cb!= NULL)
_cb(本);
} 公共对象AsyncState
{{返回(_extraData); }}
公共BOOL CompletedSynchronously
{{返回(假); }}
公共BOOL IsCompleted
{{返回(_isCompleted); }}
公众的WaitHandle AsyncWaitHandle
{
得到
{
锁定(本)
{
如果(_callCompleteEvent == NULL)
_callCompleteEvent =新的ManualResetEvent(假); 返回_callCompleteEvent;
}
}
}
}类AsyncRequest
{
私人AsyncRequestState _asyncRequestState; 公共AsyncRequest(AsyncRequestState ARS)
{
_asyncRequestState = ARS;
} 公共无效的ProcessRequest()
{
//调用Web服务或异步执行SQL命令
AsyncIOOperationWithCallback(州=>
{
((AsyncRequestState)state.Context)._ ctx.Response.Write(操作完成);
_asyncRequestState.CompleteRequest();
},_asyncRequestState); }
}
您通常使用异步操作处理而不会阻塞线程的方式是让你的异步操作的支持回调。该TPL真的没有做什么神奇这里;如果底层操作不支持某种形式的回调在某些时候,你还是坚持了东西挡住最终。经典的开始/结束处理这一要求完美的罚款
当TPL真正的亮点是提供了异常处理/汇聚更好的支持,并允许更复杂的延续机型(如ContinueWhenAll或ContinueWhenAny)还为新取消的支持,包括后续的延续prevention。延续本身,不过,实在没有什么比在一个票友,清洁包回调了。
How can I perform wait without blocking thread and without TPL' ContinueWith? For ex. in 3.5? I know about TPL's ported version for 3.5 (by RX team), but i'm curious to know - which threading primitives I can use for that (... what is behing the scenes of TPL). And what ContinueWith alternatives in TPL?
// will this handler block thread during async IO operation?
public class AsyncHandler : IHttpAsyncHandler
{
public void ProcessRequest(HttpContext ctx)
{
// not used
}
public bool IsReusable
{
get { return false; }
}
public IAsyncResult BeginProcessRequest(HttpContext ctx,
AsyncCallback cb,
object obj)
{
AsyncRequestState reqState =
new AsyncRequestState(ctx, cb, obj);
AsyncRequest ar = new AsyncRequest(reqState);
ThreadStart ts = new ThreadStart(ar.ProcessRequest);
Thread t = new Thread(ts);
t.Start();
return reqState;
}
public void EndProcessRequest(IAsyncResult ar)
{
AsyncRequestState ars = ar as AsyncRequestState;
if (ars != null)
{
// Some cleanup
}
}
}
class AsyncRequestState : IAsyncResult
{
public AsyncRequestState(HttpContext ctx,
AsyncCallback cb,
object extraData)
{
_ctx = ctx;
_cb = cb;
_extraData = extraData;
}
internal HttpContext _ctx;
internal AsyncCallback _cb;
internal object _extraData;
private bool _isCompleted = false;
private ManualResetEvent _callCompleteEvent = null;
internal void CompleteRequest()
{
_isCompleted = true;
lock (this)
{
if (_callCompleteEvent != null)
_callCompleteEvent.Set();
}
if (_cb != null)
_cb(this);
}
public object AsyncState
{ get { return (_extraData); } }
public bool CompletedSynchronously
{ get { return (false); } }
public bool IsCompleted
{ get { return (_isCompleted); } }
public WaitHandle AsyncWaitHandle
{
get
{
lock (this)
{
if (_callCompleteEvent == null)
_callCompleteEvent = new ManualResetEvent(false);
return _callCompleteEvent;
}
}
}
}
class AsyncRequest
{
private AsyncRequestState _asyncRequestState;
public AsyncRequest(AsyncRequestState ars)
{
_asyncRequestState = ars;
}
public void ProcessRequest()
{
//calling webservice or executing sql command asynchronously
AsyncIOOperationWithCallback(state =>
{
((AsyncRequestState)state.Context)._ctx.Response.Write("Operation completed");
_asyncRequestState.CompleteRequest();
}, _asyncRequestState);
}
}
The way you typically deal with an asynchronous operation without blocking a thread is to have your asynchronous operation support a callback. The TPL really doesn't do anything magic here; if the underlying operations don't support some form of callback at some point, you're still stuck with something blocking eventually. The classic 'Begin/End' Asynchronous Programming Model handles this requirement perfectly fine.
When the TPL really shines is in providing better support for exception handling/aggregation and allowing more complex continuation models (such as ContinueWhenAll or ContinueWhenAny.) There is also new support for cancellation, including prevention of subsequent continuation. Continuation itself, though, is really nothing more than a callback in a fancier, cleaner package.
这篇关于3.5非阻塞等待而不TPL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!