.net c#在线程上调用函数,尝试发送无效线程的命令

Calling function on thread, trying to send command for invalid thread
2020-10-17
  •  译文(汉语)
  •  原文(英语)

我有一个使用c#开发的控制台应用程序.
该控制台运行一些线程.它总是可以正常工作,但是今天我需要调用一个网络浏览器并从页面中获取一些信息.我遇到了一些错误,因此决定将我的代码的这一部分添加到线程上.我将代码放入名为MyBrowserCall的方法中.
我这样打电话:

var t = new Thread(MyBrowserCall);
 t.SetApartmentState(ApartmentState.STA);
 t.Start();

这个函数工作正常,一切都很好,但是当我想用这个方法调用另一个函数时,我遇到了这个错误:

未处理的异常:System.SystemException:尝试从无效线程发送命令

你知道我该怎么解决吗?

速聊1:
好吧,发布引发该异常的代码并发布ToString输出.
速聊2:
您好@usr,您想例外吗?我在这里写:未处理的异常:System.SystemException:试图从无效线程发送命令
解决过程1

我无法解决问题,但可以更改代码.
我在webbrowser部分之后调用了有问题的函数.因此,我调用了GetData函数,该问题在Web浏览器线程运行后引起问题.所以现在工作正常.

I have a console application that I developed with c#.
This console run some threads. It always works fine but today I need to call a webbrowser and get some information from a page. I faced some errors and I decided to add this part of my code on a thread.I put my code in a method that named MyBrowserCall.
I called like this :

var t = new Thread(MyBrowserCall);
 t.SetApartmentState(ApartmentState.STA);
 t.Start();

This function works fine and everything is good but when i want to call another function in this method I faced this error:

Unhandled Exception: System.SystemException: Trying to send command from an invalid thread

Do you know how can I fix this ?

Talk1:
Well, post the code that threw that exception and post the ToString output.
Talk2:
hi @usr , You want to exception ? I wrote here : Unhandled Exception: System.SystemException: Trying to send command from an invalid thread
Solutions1

I could not fix the issue but I could change my code.
I called the functions that has issue after webbrowser part. So I called GetData function that cause problem after thread of webbrowser. So it is working fine now.

转载于:https://stackoverflow.com/questions/30816826/calling-function-on-thread-trying-to-send-command-for-invalid-thread

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入