#C# FSW

1 messages · Page 1 of 1 (latest)

plush gull
#

I am making a thread because this is likely to be a long convo

#

So anyways, the initial message was already too long.
I have to mention that in the code I attached, the fsw events are disabled in order for me to test the reload on a bind.

#

Now, I'll provide all the screenshots displaying proper execution, and execution triggered by the fsw.
Difference will be obvious, one gets executed mutliple times and keeps cutting off at the same part of the code, whilst the other one executes flawlessly each and every time.

#
  1. Proper execution
  2. FSW execution
wintry topaz
#

It's likely a multithreading issue. Each callback you get is probably called on a new thread.

plush gull
#

I have an idea of what is happening(if my assumption is correct), I just don't know how to go about fixing it, and I can't say that it's well documented at all

plush gull
uncut inlet
plush gull
#

since when does unity not log errors happening on a different thread?

uncut inlet
#

never did as far as I know, you always need to capture the exception and log it manually

plush gull
#

oh wow, I've been using it for 3 years, even wrote some multi threaded core, and never knew this, that's actually such a useful piece of knowledge

#

thanks for pointing it out, I'll see what I can come up with

#

who woulda thought, you nailed it!

uncut inlet
#

had that issue so many times 😂

plush gull
#

🤦‍♂️

#

well, at least I learned arguably one of the most useful things so far

#

just to make sure, the solution now is to execute all that stuff on the main thread, and implement deduplication logic too ofc, right?

uncut inlet
#

I didn't follow too much the rest of the thread, but that sounds correct
you'll certainly need to jump back to the main thread to call those Unity functions

plush gull
#

any suggestions for the api I should look into?

uncut inlet
#

for moving back to the main thread you mean?
you can most likely find some implementations on Github or something like that, but it boils down to having some kind of Task queue being executed in the Update loop

wintry topaz
#

More sophisticated solutions would use async await or event queue or something.

plush gull
#

Hey, thanks a lot for your feedback, it truly means a lot!
I just tried this asset, but due to (mainly) it's Apache 2.0 license, I decided to not use it because my project uses GPL 3.0 and I wanna avoid any conflicts and complications.
It's also got a lot of bloat that I just cba to remove.
I decided to implement the simplest form of this, a bool check, I just don't know how safe it is?
Like, is it possible that the following code:

        private bool _configChanged;
        
        public void Update()
        {
            if (!_configChanged) return;
            _configChanged = false;
            HandleConfigChanged();
        }

        private FileSystemWatcher _configWatcher;
        
        /// Sets up the FileSystemWatcher to monitor changes to the config file
        private void WatchForConfigChanges()
        {
            _configWatcher = new()
            {
                Path = Path.GetDirectoryName(_configPath),
                Filter = Path.GetFileName(_configPath),
                NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.Size
            };

            _configWatcher.Changed             += AcknowledgeConfigChanges;
            _configWatcher.Renamed             += AcknowledgeConfigChanges;
            _configWatcher.Deleted             += AcknowledgeConfigChanges;
            _configWatcher.EnableRaisingEvents =  true;
        }

        private void AcknowledgeConfigChanges(object sender, FileSystemEventArgs args) => _configChanged = true;
        
        /// Handles config file being changed
        private void HandleConfigChanged() { }

Could cause the Update to do the if check, see that the config has changed, then another change is acknowledged and _configChanged should be set to true but Update overwrites it with false due to a race condition?

#

sorry if this has a fairly obvious answer, I've done a fairly small amount of multi threading in the past so I don't have a lot of experience

uncut inlet
#

sincerely I'm not sure, but if you want to be extra careful with that you can look into locks or similar strategies to keep things synchronized

wintry topaz