I'm trying to access the ListBox from two threads. I am using two lock statements but this doesn't work.
My code:
   public partial class Form1 : Form
   {
        private List<LogInfo> logs = new List<LogInfo>();
        private static Object lockObj = new Object();
        private static Object lockObj0 = new Object();
        /* ... */
        void fileSystemWatcher_Renamed(object sender, RenamedEventArgs e)
        {
            try
            {
                ToggleWatcher(false);
                LogInfo logInfo = new LogInfo(e.ChangeType, GetCurrentTime(), e.FullPath, e.OldName, e.Name);
                lock (lockObj)
                {
                    logs.Add(logInfo);
                    listBox1.Items.Add(logInfo.ToString());
                }
            }
            finally
            {
                ToggleWatcher(true);
            }
        }
        void fileSystemWatcher_Detect(object sender, FileSystemEventArgs e)
        {
            try
            {
                ToggleWatcher(false);
                LogInfo logInfo = new LogInfo(e.ChangeType, GetCurrentTime(), e.FullPath);
                lock (lockObj)
                {
                    logs.Add(logInfo);
                    // Here in below line i get error: invalidoperationexception was unhandled
                    listBox1.Items.Add(logInfo.ToString());
                }
            }
            finally
            {
                ToggleWatcher(true);
            }
        }
    }
I don't know why it doesn't work (i have two lock statements), but i am getting error: invalidoperationexception was unhandled
I tried to change lockObj to static or use Monitor class but i it still get this same error
 
    