Project Description
CancellableTask is an extension of .NET APM(Asynchronous Programming Model) with asynchronous task cancel and timeout feature.

Hightlights
1. Keep APM style, e.g. BeginInvoke, EndInvoke, AsyncCallback, IAsyncResult
2. Support both default and customized cancel callback

Attentions
The default cancel callback is based on Thread.Abort, which is not always reliable and safe. Always provide your own cancel callback when possible.

Sample Snippet
/*tcp connection request with timeout*/ 
public TcpClient Connect(string ip, int port, int timeout)
{
    TcpClient tcpClient = new TcpClient();
 
    WorkCallback workCallback = delegate(object state)
    {
        tcpClient.Connect(ip, port); return null;
    };
 
    CancelCallback cancelCallback = delegate(object cancelState)
    {
        tcpClient.Close(); return null;
    };
			
    CancellableTask task = new CancellableTask(workCallback);
			
    IAsyncResult ar = task.BeginInvoke(null, null, null, cancelCallback, null, timeout);
			
    task.EndInvoke(ar);
 
    return tcpClient;
}

Last edited Feb 22, 2009 at 2:10 PM by dgwei, version 25