For real world context, I'm trying to work around a somewhat rare issue in an automated process that constructs a C# FileStream like so:
using (var file = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read))
{
...
This process goes through this line thousands of times per day, and on some rare occasions, hangs indefinitely somewhere in the FileStream constructor. I have some suspicion that the cause of the hang could be due to some users' usage of an alternate file system process that runs within Windows as a service inside a specified path, but regardless, I'd like to be able to work around whatever the issue may be by opening the FileStream asynchronously and aborting after a reasonable timeout period. I see that there is documentation for sync FileStream Read/Write, but I cannot find anything for initially acquiring the FileStream object opening the file. I have tried wrapping the FileStream open in a separate thread as well as an async task, and I am able to detect if the operation has hung, which is good, but I am unable to abort the stuck thread if this happens. In particular, Thread.Abort is no longer supported on .Net, and CancellationToken doesn't seem to work for me either. All the API's expect the running thread to terminate gracefully, but I of course have no control over what happens in .Net libraries.