gpt4 book ai didi

c# - 从父线程到子线程的通信

转载 作者:太空宇宙 更新时间:2023-11-03 13:52:22 25 4
gpt4 key购买 nike

我有一个线程列表,通常有 3 个线程,每个线程都引用一个与父控件通信以填充数据 GridView 的 Web 浏览器控件。我需要做的是当用户单击 datagridviewButtonCell 中的按钮时,相应的数据将被发送回最初与主线程通信的子线程中的 webbrowser 控件。但是当我尝试这样做时,我收到以下错误消息

'无法使用与其底层 RCW 分离的 COM 对象。'

我的问题是我不知道如何引用相关的 webbrowser 控件。如果有人能给我任何帮助,我将不胜感激。

使用的语言是c# winforms.Net 4.0 目标

代码示例:

当用户点击主线程中的开始按钮时执行以下代码

private void StartSubmit(object idx){

/*

新线程用于初始化从 webbrowser 控件继承的“myBrowser”的方法每个提交者对象都是一个名为“myBrowser”的自定义控件其中包含有关对象功能的详细信息,例如:

*/

//index:是一个整数值,代表线程id

int 索引 = (int)idx;

//submitters[index] 是'myBrowser'控件的一个实例

submitters[index] = new myBrowser();

//线程整数id

submitters[index]._ThreadNum = index;

//命名约定使用'浏览器'+线程索引

submitters[index].Name = "browser" + index;

//在“myBrowser”类中设置列表以保存在主线程中找到的列表的副本

submitters[index]._dirs = dirLists[index];

//抑制 'myBrowser' 控件中可能出现的 javascript 错误

submitters[index].ScriptErrorsSuppressed = true;

//执行事件处理器

submitters[index].DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(DocumentCompleted);

//前进到数据 GridView 中下一个未打开的地址并导航到该地址

//在“我的浏览器”控件中。

SetNextDir(submitters[index]);

private void btnStart_Click(object sender, EventArgs e){

  // used to fill list<string> for use in each thread.

fillDirs();

//connections is the list<Thread> holding the thread that have been opened
//1 to 10 maximum

for (int n = 0; n < (connections.Length); n++)

{

//initialise new thread to the StartSubmit method passing parameters

connections[n] = new Thread(new ParameterizedThreadStart(StartSubmit));

// naming convention used conn + the threadIndex ie: 'conn1' to 'conn10'

connections[n].Name = "conn" + n.ToString();

// due to the webbrowser control needing to be ran in the single
//apartment state

connections[n].SetApartmentState(ApartmentState.STA);

//start thread passing the threadIndex

connections[n].Start(n);

}

“myBrowser”控件完全加载后,我将表单数据插入网页中的网络表单中,这些网页通过数据加载到数据 GridView 中的行中。一旦用户将相关详细信息输入到行中的不同区域,便可以单击具有 tha 的 DataGridViewButtonCell 收集输入的数据,然后必须发送回在子线程上找到的相应“myBrowser”对象。

谢谢

最佳答案

该错误表明包装 COM 对象(可能是 WebBrowser 控件,但没有更多信息我不能确定)的托管对象已被释放。这意味着托管对象仍然存在(它尚未被垃圾回收),但已对其调用 IDisposable.Dispose()(释放 WebBrowser 控件,它是一个 COM 对象)。

顺便说一下,R​​CW 代表运行时可调用包装器。

确保您尝试通过它的托管包装器引用的 COM 对象没有对其调用 IDisposable.Dispose(直接调用,或者例如通过离开 using block 的范围)。

关于c# - 从父线程到子线程的通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13350356/

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