I have the following function, which runs individually for thousands of files. When it runs, the UI thread locks up due to the synchronous I/O operation. But this source says that using async for many operations is inefficient, so how can I prevent the UI from locking up then?
public string CopyFile(string sourceFile, string fileName, bool forceCopy)
{
fileName = GetSafePathname(GetSafeFilename(fileName));
string DestinationFile = Path.Combine(DestinationFolder, fileName);
if (File.Exists(DestinationFile) && !forceCopy)
{
return DestinationFile = null;
}
else if (!File.Exists(DestinationFile)) //copy the file if it does not exist at the destination
{
File.Copy(sourceFile, DestinationFile);
return DestinationFile;
}
else if (forceCopy) //if forceCopy, then delete the destination file and copy the new one in its place
{
File.Delete(DestinationFile);
File.Copy(sourceFile, DestinationFile);
return DestinationFile;
}
else { throw new GenericException(); }
}