Hello,
Welcome to Microsoft Q&A!
You are using TaskFactory, then you can choose to abort the task after timeout.
CancellationToken is required to abort the task, you can write like this:
CancellationTokenSource source = new CancellationTokenSource();
CancellationToken token = source.Token;
TaskFactory factory = new TaskFactory(token);
var task = factory.StartNew(() =>
{
// read file
}, token);
try
{
if (task.Wait(4000))
{
EbookCbz.LoadEbook = EbookLoad.Ok;
}
else
{
EbookCbz.LoadEbook = EbookLoad.Timeout;
source.Cancel();
}
}
catch (Exception ex)
{
}
finally{
source.Dispose();
// do something...
}
The current running task can be aborted through the call of source.Cancel().
You can refer to this document to learn more about the usage of CancellationToken.
Thanks.