I have this code in c# .net core. I want to know that can this code corrupt the file if I try to cancel it. If yes how can I solve this issue?
private async Task<string> MoveFile(string sourcePath, string destinationPath, CancellationToken token)
{
Thread threadToCancel = null;
var tcs = new TaskCompletionSource<string>();
Task.Factory.StartNew(() =>
{
//Capture the thread
threadToCancel = Thread.CurrentThread;
File.Move(sourcePath, destinationPath);
}, TaskCreationOptions.LongRunning);
Task.Factory.StartNew(() =>
{
while (true)
{
if (token.IsCancellationRequested && threadToCancel != null)
{
threadToCancel.Abort();//abort long running thread
tcs.SetResult("Cancled");
return tcs.Task.Result;
}
Thread.Sleep(500);
}
});
tcs.SetResult("Completed");
return tcs.Task.Result;
}