I heared about semaphore slim to limit thread access to something but I dont know its exact usage in async context?
if (!LOGS_FILE.Equals(string.Empty) && File.Exists(LOGS_FILE))
{
using (StreamWriter STREAM_WRITER = new StreamWriter(LOGS_FILE, true))
{
STREAM_WRITER.WriteLine(LOG);
}
}
if (IsDebuggerAttached())
{
System.Diagnostics.Debug.WriteLine(LOG);
}
ALL_LOGS += LOG + "\n";
>Solution :
Try this:
try
{
SEMAPHORE_SLIM.Wait();
if (!LOGS_FILE.Equals(string.Empty) && File.Exists(LOGS_FILE))
{
using (StreamWriter STREAM_WRITER = new StreamWriter(LOGS_FILE, true))
{
STREAM_WRITER.WriteLine(LOG);
}
}
if (IsDebuggerAttached())
{
System.Diagnostics.Debug.WriteLine(LOG);
}
ALL_LOGS += LOG + "\n";
}
finally
{
SEMAPHORE_SLIM.Release();
}
EDIT: Added try finally block