我有一个FileSystemWatcher_Changed
事件,我想检查表单上任何控件的状态是否已更改。然后我想更新一个更改了某些内容的json文件。
问题是当我尝试在更改的事件中使用Dispatcher检查更改时,我得到一个例外:
从非同步块调用对象同步方法 代码。
我提到了这篇文章,但无法提取任何有关我案件的信息。 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;
}