If task is not completed in certain time, stop the task and release its resources(not working for me)

  • Thread starter Thread starter SarbeshB
  • Start date Start date
S

SarbeshB

Guest
Hello, I have a task and if the task does not completes until 4 seconds I want to open a file used by a task and dispose its stream, while trying to dispose its stream, I am getting an exception thrown which means the file is still not being released. I have got this code so far:

Task task= Task.Run(() =>
{
dssPut_Command("Compile " + "abc.dss");

if (ct.IsCancellationRequested)
ct.ThrowIfCancellationRequested();
}, ct);
tokenSource2.Cancel();
try
{
if (!task.Wait(TimeSpan.FromSeconds(4)))
{
var sr = new StreamReader(abc.dss");
Debug.WriteLine("error with the file.");
sr.ReadToEnd();
sr.Dispose();//getting an exception thrown in this part

//stream is still not close/disposed }

}
catch (AggregateException ae)
{
Console.WriteLine("Task cancelled exception detected", ae.Message);


}
finally
{
tokenSource2.Dispose();


}

Continue reading...
 
Back
Top