Text'抛出了类型'Microsoft.VisualStudio.Debugger.Runtime.CrossThreadMessagingException'的异常 [英] Text' threw an exception of type 'Microsoft.VisualStudio.Debugger.Runtime.CrossThreadMessagingException'
问题描述
大家好,
我是事件处理新手。我所做的是
在Class1上制作了一个活动
在Class1的某些功能中触发了这个事件
当我在Form1中使用事件时,我不能在事件处理程序调用的Form1函数中使用Form1控件。
发生以下消息
Cross - 线程操作无效:控制''lblTagID''从其创建的线程以外的线程访问。
如果我不能使用该事件由我在主要形式中创建,然后我们如何很容易地使用表单控件事件。
这个问题让我感到害怕。
如果有人快速回答,我会恭喜。
谢谢
您无法直接从线程访问在其他线程上创建的控件。
您需要调用主线程的方法,它将为您更新控件。
创建一个更新控件所需的方法。从事件处理程序调用该方法。假设您的方法名称为xyz(),然后使用以下方法调用:
SynchronizationContext context = SynchronizationContext.Current;
if(context == null)
context = new SynchronizationContext();
context.Send(new SendOrPostCallback((s)=> ;; xyz()),null);
你也可以使用:
BeginInvoke( new MethodInvoker( delegate
{
xyz();
}));
这对你有用。 :rose:
你可以调用Thread.Sleep Metod
Thread.Sleep(< span class =code-digit> 100 );
this .Invoke((MethodInvoker) delegate
{
myTextBox.Text + = strMessage + \\\\ n;
});
您的问题是您正在使用另一个线程访问表单上的控件。除非你采取措施允许,否则DotNet不会让你这样做。你可以设置一个属性,但这被认为是不好的做法。更合适的方法是使用Invoke。
我会让你做研究以获得详细信息。毕竟,* *应该是一个快速的答案。
提示:谷歌Microsoft.VisualStudio.Debugger.Runtime.CrossThreadMessagingException,看看你得到了什么。
Hi Everybody,
I am new to event handling. what I have done is
Made an event on the Class1
triggered the event in some function in Class1
when I use the event in Form1 , I can''t use the Form1 Controls in the function of Form1 called by the event handler.
the following messege occurs
Cross-thread operation not valid: Control ''lblTagID'' accessed from a thread other than the thread it was created on.
If I can''t use the event created by me in the main form then how we can use the form control events quite easily.
This problem is killing me.
I will appriciate if somebody have a quick answer.
Thank You
You cant access controls created on other thread directly from a thread.
You need to call a method of the main thread which will update controls for you.
Create a method which you need to update the control. Invoke that method from the event handler. Say your method name is xyz() then call using :
SynchronizationContext context = SynchronizationContext.Current; if(context == null) context = new SynchronizationContext(); context.Send(new SendOrPostCallback( (s) =>; xyz()), null);
You might also use :
BeginInvoke(new MethodInvoker(delegate { xyz(); }));
This will work for you. :rose:
You can call Thread.Sleep Metod
Thread.Sleep(100); this.Invoke((MethodInvoker)delegate { myTextBox.Text += strMessage + "\r\n"; });
Your problem is that you''re accessing a control on the form with another thread. DotNet doesn''t let you do that unless you "take steps" to allow it. There is a property you can set, but that''s considered bad practice. The more appropriate method is to use Invoke.
I''ll let you do the research to get the particulars. After all, this *is* supposed to be a quick answer.
Hint: Google "Microsoft.VisualStudio.Debugger.Runtime.CrossThreadMessagingException" and see what you get.
这篇关于Text'抛出了类型'Microsoft.VisualStudio.Debugger.Runtime.CrossThreadMessagingException'的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!