gpt4 book ai didi

file-io - 跨线程操作无效: Control accessed from a thread other than the thread it was created on

转载 作者:行者123 更新时间:2023-12-02 15:58:35 25 4
gpt4 key购买 nike

我正在编写一个文件观察器 Windows 应用程序,它将查找指定文件夹中的更改,然后将详细信息记录在 txt 文件中。

我完全按照下面这篇文章中提到的内容进行操作 http://www.codeproject.com/KB/dotnet/folderwatcher.aspx

当我从应用程序中按 F5,然后在正在监视的文件夹中创建或修改文件时,它会抛出下面提到的错误。

请帮忙

跨线程操作无效:控制“txtFolderActivity”是从创建它的线程以外的线程访问的。

最佳答案

您必须在表单上使用 Invoke 方法,例如与匿名代表一起根据事件进行更改。

事件处理程序由另一个线程引发。第二个线程无法访问表单中的控件。它必须“调用”它们,让线程完成最初创建它们的所有控制工作。

而不是:

myForm.Control1.Text = "newText";

你必须写:

myForm.Invoke(new Action(
delegate()
{
myForm.Control1.Text = "newText";
}));

关于file-io - 跨线程操作无效: Control accessed from a thread other than the thread it was created on,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1353857/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com