c# Block access to file after a timer
<p>I need to write a file that is located on a remote directory. I need to create a log application: every time an error occurs, I want to access this remote file and write a line. But if the remote directory is not available, I don't care. I simply want the main application keep running.</p>

<p>Usually accessing this remote directory takes 1 second. If the WriteLine method takes more than 1 second, I want to block it.</p>

<p>I tried with a try catch but then the entire application is blocked for a long time. I need to interrupt this attempt to write sooner.</p>

<p>This is the main code used to write on the txt file:</p>

<pre><code>try 
{           
    string TargetFile = "F:\\file.txt";
    using (StreamWriter sw = File.AppendText(TargetFile))
    {
        sw.WriteLine("LOG: an error occured\n");
        sw.Close();
    }
}
catch (Exception)
{

    throw;
}
</code></pre>

<p>I can't figure out how to solve this problem. Maybe by running the write operation inside a separate thread? Thanks for your help!</p>