Improve Lua `FileSystemWatcher` thread safety

Make FSW invoke the entire event handler on main thread.

Avoids theoretical race condition and thread safety issues with the linq query.
This commit is contained in:
kalimag 2022-11-27 19:54:02 +01:00 committed by James Groom
parent 733a8bee88
commit 9ee788195a
1 changed files with 6 additions and 2 deletions

View File

@ -285,7 +285,8 @@ namespace BizHawk.Client.EmuHawk
Path = dir,
Filter = file,
NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName,
EnableRaisingEvents = true
EnableRaisingEvents = true,
SynchronizingObject = this, // invoke event handlers on GUI thread
};
// TODO, Deleted and Renamed events
@ -308,7 +309,10 @@ namespace BizHawk.Client.EmuHawk
private void OnChanged(object source, FileSystemEventArgs e)
{
var script = LuaImp.ScriptList.FirstOrDefault(s => s.Path == e.FullPath && s.Enabled);
if (script != null) Invoke((MethodInvoker) (() => RefreshLuaScript(script)));
if (script is not null)
{
RefreshLuaScript(script);
}
}
public void LoadLuaFile(string path)