C# System.InvalidOperationException调用异常
我不知道为什么会有这个例外 System.InvalidoOperationException:“无válida子区域的运营:将子区域的控制“形式2”设置为一个水通道。” 谷歌将此翻译为: System.InvalidOperationException:“通过线程执行的操作无效:“Form2”控件是从与创建它的线程不同的线程访问的。” 例如,如果我从按钮调用invoke,它可以正常工作,但我需要从FileSystemWatcher调用它C# System.InvalidOperationException调用异常,c#,exception,invoke,C#,Exception,Invoke,我不知道为什么会有这个例外 System.InvalidoOperationException:“无válida子区域的运营:将子区域的控制“形式2”设置为一个水通道。” 谷歌将此翻译为: System.InvalidOperationException:“通过线程执行的操作无效:“Form2”控件是从与创建它的线程不同的线程访问的。” 例如,如果我从按钮调用invoke,它可以正常工作,但我需要从FileSystemWatcher调用它 List<Thread> listThrea
List<Thread> listThreads = new List<Thread>();
private void Form1_Load(object sender, EventArgs e)
{
RunFileSystemWatcher();
}
public void RunFileSystemWatcher()
{
FileSystemWatcher fsw = new FileSystemWatcher();
fsw.Path = "C:/Users/Gaming/Documents";
fsw.NotifyFilter = NotifyFilters.LastAccess;
fsw.NotifyFilter = NotifyFilters.LastWrite;
//fsw.NotifyFilter = NotifyFilters.Size;
//fsw.Created += FileSystemWatcher_Created;
fsw.Changed += FileSystemWatcher_Changed;
fsw.Filter = "*.txt";
fsw.EnableRaisingEvents = true;
}
Boolean abrir = true;
private void FileSystemWatcher_Changed(object sender, FileSystemEventArgs e)
{
if (abrir) {
for (int i=0; i<5; i++)
{
Thread hilo = new Thread(() => showForms(new Form2()));
hilo.Start();
listThreads.Add(hilo);
abrir = false;
}
}
else{
for(int i=0; i<listThreads.Count; i++)
{
try
{
Invoke((MethodInvoker)delegate {
listForms[i].Close();
});
listThreads[i].Abort();
}
catch (ThreadAbortException)
{
}
}
}
}
List<Form2> listForms = new List<Form2>();
private void showForms(Form2 form)
{
listForms.Add(form);
form.ShowDialog();
}
List listhreads=new List();
私有void Form1\u加载(对象发送方、事件参数e)
{
RunFileSystemWatcher();
}
public void RunFileSystemWatcher()
{
FileSystemWatcher fsw=新的FileSystemWatcher();
fsw.Path=“C:/Users/Gaming/Documents”;
fsw.NotifyFilter=NotifyFilters.LastAccess;
fsw.NotifyFilter=NotifyFilters.LastWrite;
//fsw.NotifyFilter=NotifyFilters.Size;
//fsw.Created+=创建的文件系统监视程序;
fsw.Changed+=FileSystemWatcher\u已更改;
fsw.Filter=“*.txt”;
fsw.EnableRaisingEvents=true;
}
布尔abrir=true;
私有void FileSystemWatcher\u已更改(对象发送方、filesystemventargs e)
{
如果(abrir){
对于(int i=0;i showForms(new Form2());
hilo.Start();
添加(hilo);
abrir=假;
}
}
否则{
对于(inti=0;i您有一个与主线程同步的UI冲突
必须将对UI控件上的任何操作的调用与主线程同步
您可以使用后台工作人员
或者这个:
static public class SyncUIHelper
{
static public Thread MainThread { get; private set; }
// Must be called from the Program.Main or the Main Form constructor for example
static public void Initialize()
{
MainThread = Thread.CurrentThread;
}
static public void SyncUI(this Control control, Action action, bool wait = true)
{
if ( !Thread.CurrentThread.IsAlive ) throw new ThreadStateException();
Exception exception = null;
Semaphore semaphore = null;
Action processAction = () =>
{
try { action(); }
catch ( Exception except ) { exception = except; }
};
Action processActionWait = () =>
{
processAction();
if ( semaphore != null ) semaphore.Release();
};
if ( control != null
&& control.InvokeRequired
&& Thread.CurrentThread != MainThread )
{
if ( wait ) semaphore = new Semaphore(0, 1);
control.BeginInvoke(wait ? processActionWait : processAction);
if ( semaphore != null ) semaphore.WaitOne();
}
else
processAction();
if ( exception != null ) throw exception;
}
}
用法:
this.SyncUI(listForms[i].Close /*, true or false to wait or not */);
以及:
与:
您需要更正FileSystemWatcher\u中的代码,因为它有缺陷。可能重复的
this.SyncUI(() => form.ShowDialog() /*, true or false to wait or not */);
private void Form1_Load(object sender, EventArgs e)
{
SyncUIHelper.Initialize();
RunFileSystemWatcher();
}