मैं Await/Async और रद्दीकरण टोकन के साथ टकरा रहा था। मेरा कोड काम करता है, लेकिन जब इसे रद्द किया जाता है तो कार्य के साथ क्या होता है? क्या यह अभी भी संसाधन ले रहा है या क्या यह कचरा इकट्ठा किया गया है या क्या?जब इसे रद्द किया जाता है तो कार्य में क्या होता है?
यहाँ मेरी कोड है:
private CancellationTokenSource _token = new CancellationTokenSource();
public Form1()
{
InitializeComponent();
}
async Task<String> methodOne()
{
txtLog.AppendText("Pausing for 10 Seconds \n");
var task = Task.Delay(10000, _token.Token);
await task;
return "HTML Returned. \n";
}
private async void button1_Click(object sender, EventArgs e)
{
try
{
var task1 = methodOne();
await task1;
txtLog.AppendText(task1.Result + "\n");
txtLog.AppendText("All Done \n");
}
catch (OperationCanceledException oce)
{
txtLog.AppendText("Operation was cancelled");
}
}
private void button2_Click(object sender, EventArgs e)
{
_token.Cancel();
}