线程之间的通信? [英] Communication between threads?

查看:91
本文介绍了线程之间的通信?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我很难让我的后台主题告诉我的主线程

做什么。

我试图告诉我的主线程打开一个表单,但是当我的背景

线程结束时,我认为我的主线程打开的表单消失了。

显然这里有一些我不明白的东西。


后台线程在另一个表上的另一个类中运行。我如何,从那里获得
,让我的主线程做点什么?这是我的场景:


StartForm(我的主要表格)

CommForm(处理我的串口通讯)

FormA(我想要的) StartForm打开此表单)


当我的serialport上有数据时,下面的子程序在我的CommForm中作为单独的

线程运行。当读取所有数据时,线程结束。


私有共享子接收事件(ByVal发送者为System.Object,ByVal e As

System.EventArgs)句柄SerialPort.DataAvailable

''在这里我想告诉我的主线程(StartForm)打开FormA


''这就是我尝试这样做的方法

Dim del As Start.MyDelegate

del = New Start.MyDelegate(AddressOf Start.OpenFormA)

del.Invoke()


结束子


我该如何解决这个问题?事件是否更适合使用而不是

代表?

我一直在寻找代表和事件,但我无法弄清楚

如何让它们在线程之间工作。我找到的所有例子都在

相同的帖子中。


非常感谢您提供任何帮助。

我已经好几天了...

/ Daniel

Hi,

I''m having trouble letting my background thread tell my main thread what to
do.

I''m trying to tell my main thread to open a form, but when my background
thread ends, the form that I thought my main thread had opened disappears.
Obviously there''s something that I don''t understand here.

The background thread is run in another class on another form. How do I,
from there, get my main thread to do something? This is my scenario:

StartForm (my main form)
CommForm (handles my serial communication)
FormA (I want StartForm to open this form)

When data is available on my serialport, the sub below is run as a separate
thread in my CommForm. When all data is read, the thread ends.

Private Shared Sub ReceiveEvent(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles SerialPort.DataAvailable
''here I want to tell my main thread (StartForm) to open FormA

''this is how I try to do that
Dim del As Start.MyDelegate
del = New Start.MyDelegate(AddressOf Start.OpenFormA)
del.Invoke()

End Sub

How should I solve this? Is an event more appropriate to use instead of a
delegate?
Iv''e been searching a bit on delegates and events, but I can''t figure out
how to get them working between threads. All examples I''ve found are within
the same thread.

Many thanks in advance for any help on this.
I''ve been trying for days now...
/Daniel

推荐答案

Daniel,


你的代码现在的工作方式是OpenFormA方法是在与SerialData.DataAvailable事件

处理程序相同的线程上执行的
。要在主GUI线程上运行代码,您必须使用

Control.Invoke。修改你的代码,使它看起来如下所示。

私有共享Sub ReceiveEvent(ByVal发送者As System.Object,ByVal e

As System.EventArgs)处理SerialPort.DataAvailable

''在这里我想告诉我的主线程(StartForm)打开FormA


''这是我尝试这样做的方式

Dim del As Start.MyDelegate

del = New Start.MyDelegate(AddressOf Start.OpenFormA)


''这将执行代理当前线程。

''del.Invoke()


''这将在主GUI线程上执行委托。

Me.Invoke(del)


End Sub


Brian


dast写道:
Daniel,

The way your code works right now is that the OpenFormA method is
executed on the same thread as the SerialData.DataAvailable event
handler. To run code on the main GUI thread you must use
Control.Invoke. Modify your code so that it looks like the following.
Private Shared Sub ReceiveEvent(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles SerialPort.DataAvailable
''here I want to tell my main thread (StartForm) to open FormA

''this is how I try to do that
Dim del As Start.MyDelegate
del = New Start.MyDelegate(AddressOf Start.OpenFormA)

'' This executes the delegate on the current thread.
''del.Invoke()

'' This executes the delegate on the main GUI thread.
Me.Invoke(del)

End Sub

Brian

dast wrote:


我在让背景线程告诉我的主线程时遇到了什么问题

我是试图告诉我的主线程打开一个表单,但是当我的
后台线程结束时,我认为我的形式在线程已打开消失。显然,这里有一些我不理解的东西。

后台线程在另一个表上的另一个类中运行。从那里开始,我是如何让我的主线程做点什么的?这是我的场景:

StartForm(我的主要形式)
CommForm(处理我的串口通信)
FormA(我希望StartForm打开这个表单)

当我的serialport上有数据时,下面的子程序在我的CommForm中作为单独的线程运行。当读取所有数据时,线程结束。

私有共享Sub ReceiveEvent(ByVal sender As System.Object,ByVal
e As System.EventArgs)处理SerialPort.DataAvailable
''这里我想告诉我的主线程(StartForm)打开FormA

''这就是我尝试这样做的方式
Dim del As Start.MyDelegate del = New Start.MyDelegate(AddressOf Start.OpenFormA)
del.Invoke()

End Sub

我该如何解决这个问题?是否更适合使用代表呢?
我一直在寻找代表和事件,但我无法想象如何让他们工作线程之间。我发现的所有例子都在同一个帖子中。

非常感谢您提供任何帮助。
我已经尝试了好几天了.. 。
/ Daniel
Hi,

I''m having trouble letting my background thread tell my main thread
what to do.

I''m trying to tell my main thread to open a form, but when my
background thread ends, the form that I thought my main thread had
opened disappears. Obviously there''s something that I don''t
understand here.

The background thread is run in another class on another form. How
do I, from there, get my main thread to do something? This is my
scenario:

StartForm (my main form)
CommForm (handles my serial communication)
FormA (I want StartForm to open this form)

When data is available on my serialport, the sub below is run as a
separate thread in my CommForm. When all data is read, the thread
ends.

Private Shared Sub ReceiveEvent(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles SerialPort.DataAvailable
''here I want to tell my main thread (StartForm) to open FormA

''this is how I try to do that
Dim del As Start.MyDelegate
del = New Start.MyDelegate(AddressOf Start.OpenFormA)
del.Invoke()

End Sub

How should I solve this? Is an event more appropriate to use instead
of a delegate?
Iv''e been searching a bit on delegates and events, but I can''t figure
out how to get them working between threads. All examples I''ve found
are within the same thread.

Many thanks in advance for any help on this.
I''ve been trying for days now...
/Daniel






嗨Brian,


谢谢你的回复!最后是我的问题的精确答案。很多

赞赏。

但遗憾的是我无法得到你的建议,因为

这个子是共享的。


''我''仅在实例方法中有效。


我认为我需要让子共享,因为它处理一个共享

控件的事件。我认为控件(我的.NET框架组件

处理串行通信)需要共享,因为我希望能够从我的程序的所有部分发送消息



或者可以找出另一种解决方案吗?也许所有传输都来自一个

单线程,让我的代码的其他部分发送消息给那个

线程?


有任何建议或意见吗?


祝你好运

/ Daniel Strigard


" Brian Gideon"写道:
Hi Brian,

Thank you for the reply! Finally a precise answer to my questions. Much
appreciated.
But unfortunately I can''t get your suggestion to work in this case, since
the sub is shared.

''Me'' is valid only within an instance method.

I think I need to have the sub shared, since it handles an event of a
control that is shared. I think that control (my .NET framework component
that handles serial communication) needs to be shared since I want to be able
to send messages from all parts of my program.

Or could one figure out another solution? Maybe do all transmits from a
single thread, and let the other parts of my code send messages to that
thread?

Any suggestions or comments?

Best regards
/Daniel Strigard

"Brian Gideon" wrote:
Daniel,

您的代码现在的工作方式是OpenFormA方法在与SerialData相同的线程上执行。 DataAvailable事件
处理程序。要在主GUI线程上运行代码,您必须使用
Control.Invoke。修改你的代码,使它看起来如下所示。

私有共享Sub ReceiveEvent(ByVal sender As System.Object,ByVal e /> As System.EventArgs)处理SerialPort.DataAvailable
''在这里我想告诉我的主线程(StartForm)打开FormA

''这就是我尝试这样做的方式
Dim del As Start.MyDelegate
del = New Start.MyDelegate(AddressOf Start.OpenFormA)

''这将在当前线程上执行委托。
''del.Invoke()

''这将在主GUI线程上执行委托。
Me.Invoke(del)

结束子

Brian

dast写道:
Daniel,

The way your code works right now is that the OpenFormA method is
executed on the same thread as the SerialData.DataAvailable event
handler. To run code on the main GUI thread you must use
Control.Invoke. Modify your code so that it looks like the following.
Private Shared Sub ReceiveEvent(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles SerialPort.DataAvailable
''here I want to tell my main thread (StartForm) to open FormA

''this is how I try to do that
Dim del As Start.MyDelegate
del = New Start.MyDelegate(AddressOf Start.OpenFormA)

'' This executes the delegate on the current thread.
''del.Invoke()

'' This executes the delegate on the main GUI thread.
Me.Invoke(del)

End Sub

Brian

dast wrote:


我无法让我的后台主题告诉我的主要内容

我'我试图告诉我的主线程打开一个表单,但是当我的
后台线程结束时,我认为我的主线程打开的表单消失了。显然,这里有一些我不理解的东西。

后台线程在另一个表上的另一个类中运行。从那里开始,我是如何让我的主线程做点什么的?这是我的场景:

StartForm(我的主要形式)
CommForm(处理我的串口通信)
FormA(我希望StartForm打开这个表单)

当我的serialport上有数据时,下面的子程序在我的CommForm中作为单独的线程运行。当读取所有数据时,线程结束。

私有共享Sub ReceiveEvent(ByVal sender As System.Object,ByVal
e As System.EventArgs)处理SerialPort.DataAvailable
''这里我想告诉我的主线程(StartForm)打开FormA

''这就是我尝试这样做的方式
Dim del As Start.MyDelegate del = New Start.MyDelegate(AddressOf Start.OpenFormA)
del.Invoke()

End Sub

我该如何解决这个问题?是否更适合使用代表呢?
我一直在寻找代表和事件,但我无法想象如何让他们工作线程之间。我发现的所有例子都在同一个帖子中。

非常感谢您提供任何帮助。
我已经尝试了好几天了.. 。
/ Daniel
Hi,

I''m having trouble letting my background thread tell my main thread
what to do.

I''m trying to tell my main thread to open a form, but when my
background thread ends, the form that I thought my main thread had
opened disappears. Obviously there''s something that I don''t
understand here.

The background thread is run in another class on another form. How
do I, from there, get my main thread to do something? This is my
scenario:

StartForm (my main form)
CommForm (handles my serial communication)
FormA (I want StartForm to open this form)

When data is available on my serialport, the sub below is run as a
separate thread in my CommForm. When all data is read, the thread
ends.

Private Shared Sub ReceiveEvent(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles SerialPort.DataAvailable
''here I want to tell my main thread (StartForm) to open FormA

''this is how I try to do that
Dim del As Start.MyDelegate
del = New Start.MyDelegate(AddressOf Start.OpenFormA)
del.Invoke()

End Sub

How should I solve this? Is an event more appropriate to use instead
of a delegate?
Iv''e been searching a bit on delegates and events, but I can''t figure
out how to get them working between threads. All examples I''ve found
are within the same thread.

Many thanks in advance for any help on this.
I''ve been trying for days now...
/Daniel




再次嗨。


我已经能够使子和组件取消共享。


这样Me.Invoke(del)就被接受了。

但我还是得到了一旦线程结束,同样的崩溃。它似乎并不像在另一个线程上运行那样b $ b ...?


可能是我指向我的班级/表格沟通,其中放置了

RecieveEvent子,并且我的代表被放在我的班级/表单中开始?

但是我的班级Start在其load事件中打开了Communication表单。 ..


祝你好运

/ Daniel
Hi again.

I have been able to make the sub and component "unshared".

This way the Me.Invoke(del) gets accepted.
But I still get the same crash as soon as the thread ends. It doesn''t seem
like it is run on another thread...?

Could it be that Me points at my class/form Communication, in which the
RecieveEvent sub is placed, and my delegate is placed in my class/form Start?
But my class Start is opening the Communication form in its load event...

Best regards
/Daniel
" Brian Gideon"写道:
"Brian Gideon" wrote:
Daniel,

您的代码现在的工作方式是OpenFormA方法在与SerialData相同的线程上执行。 DataAvailable事件
处理程序。要在主GUI线程上运行代码,您必须使用
Control.Invoke。修改你的代码,使它看起来如下所示。

私有共享Sub ReceiveEvent(ByVal sender As System.Object,ByVal e /> As System.EventArgs)处理SerialPort.DataAvailable
''在这里我想告诉我的主线程(StartForm)打开FormA

''这就是我尝试这样做的方式
Dim del As Start.MyDelegate
del = New Start.MyDelegate(AddressOf Start.OpenFormA)

''这将在当前线程上执行委托。
''del.Invoke()

''这将在主GUI线程上执行委托。
Me.Invoke(del)

End Sub

Brian
Daniel,

The way your code works right now is that the OpenFormA method is
executed on the same thread as the SerialData.DataAvailable event
handler. To run code on the main GUI thread you must use
Control.Invoke. Modify your code so that it looks like the following.
Private Shared Sub ReceiveEvent(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles SerialPort.DataAvailable
''here I want to tell my main thread (StartForm) to open FormA

''this is how I try to do that
Dim del As Start.MyDelegate
del = New Start.MyDelegate(AddressOf Start.OpenFormA)

'' This executes the delegate on the current thread.
''del.Invoke()

'' This executes the delegate on the main GUI thread.
Me.Invoke(del)

End Sub

Brian





这篇关于线程之间的通信?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆