1

I have a FileSystemWatcher_Changed event where I want to check if the state of any of the controls on the form has changed. Then I want to update a json file that something changed.

Problem is when I try to check the change using a Dispatcher inside the changed event I get an exception:

Object synchronization method was called from an unsynchronized block of code.

I referred to this post but couldn't pull any info relating my case. Object synchronization method from unsynchronized block.

private void FileSystemWatcher_Changed(object sender, FileSystemEventArgs e)
        {
            try
            {
                //Deserialize, check length, if len >1 then check both are inactive and if both are inactive then only lock
                //var root = JsonConvert.DeserializeObject<Root>(wc.GetFullJsonText());
                var root = JsonConvert.DeserializeObject<Root>(wc.GetFullJsonText());
                if (root != null && root.AllApplications != null)
                {
                    var item = root.AllApplications.Any(x => x.Status == ActivityStatus.Active.ToString());
                    if (!item)
                    {
                        if (InActivecount == 0)
                        {
                            Program.IdleTimer.Tag = InActivityTimer.Ended;
                            //MessageBox.Show("I am hiding because Main App is InActive");
                            this.Invoke((MethodInvoker)delegate //I think this is causing an issue.
                            {
                                CheckChanged();
                            });
                            this.Hide();
                        }
                        InActivecount++;
                    }
                    else
                    {
                        if ((InActivityTimer)Program.IdleTimer.Tag == InActivityTimer.Ended)
                        {
                            this.Show();
                            UnRegister(sender as FileSystemWatcher);
                            UpdateActivityStatus();
                            Program.StartInActivityMonitor();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.InnerException.ToString());
            }
        }


private void CheckChanged()
        {
            foreach(var item in this.Controls)
            {
                if(item is TextBox)
                {
                    control = item;
                    (control as TextBox).TextChanged += Form1_TextChanged;
                }
            }
        }

        private void Form1_TextChanged(object sender, EventArgs e)
        {
            wc.AddStatusToGeneStudy(true);
            (control as TextBox).TextChanged -= Form1_TextChanged;
        }
awh112
  • 1,466
  • 4
  • 22
  • 34
nikhil
  • 1,578
  • 3
  • 23
  • 52
  • See: [FileSystemWatcher.SynchronizingObject](https://learn.microsoft.com/en-us/dotnet/api/system.io.filesystemwatcher.synchronizingobject?f1url=https%3A%2F%2Fmsdn.microsoft.com%2Fquery%2Fdev15.query%3FappId%3DDev15IDEF1%26l%3DEN-US%26k%3Dk(System.IO.FileSystemWatcher.SynchronizingObject);k(TargetFrameworkMoniker-.NETFramework%26f%3D255%26MSPPError%3D-2147217396&view=netframework-4.7.2)) – Jimi Jun 05 '18 at 23:15
  • Thanks I will take a look. – nikhil Jun 05 '18 at 23:32

0 Answers0