我怎麼能不阻塞線程,並沒有第三方物流ContinueWith執行的等待?例如。在3.5?我知道TPL的移植版本爲3.5(由RX團隊提供),但我很想知道 - 我可以使用哪些線程原語(...什麼是TPL場景)。在TPL中有什麼ContinueWith替代品?非阻塞在等待而不TPL 3.5
//將異步IO操作過程中該處理程序塊線程?
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);
}
}
有關使用信令如何構造 –
究竟什麼是「非阻斷,等待」? –
你想等什麼? – svick