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 at 3:10 PM by dgwei, version 25

 

Want to leave feedback?
Please use Discussions or Reviews instead.

Updating...
© 2006-2009 Microsoft | About CodePlex | Privacy Statement | Terms of Use | Code of Conduct | Advertise With Us | Version 2009.10.27.15987