I have a simple powershell command that appends a variable (string) into a .log file:
powershell.exe -command " \"%V\" | out-file "C:\file.log" -append "
This is stored inside the registry, and so %V equals to a directory path.
Whenever I execute this command, the directory path gets appended to the log file. The problems start happening when I call 15 instances of this command - the results are awful (for example, I'll only see 3 out of 15 entries in my log file). I believe this is called race condition, where multiple instances are trying to access the file at the same time, resulting in overwriting.
I tried to solve this problem by using mutex, so that only one instance will actually be appending the path to the log file at a time:
powershell.exe -command " $mtx = New-Object System.Threading.Mutex($false, \"RCWM\");
$mtx.WaitOne();
\"%V\" | out-file "C:\file.log" -append;
$mtx.ReleaseMutex() "
This works, but it's very slow.
Is there anything else I can do to speed this process up, while only allowing one instance to output to the file at a time?