PC Review


Reply
Thread Tools Rate Thread

Communication between threads?

 
 
=?Utf-8?B?ZGFzdA==?=
Guest
Posts: n/a
 
      18th Feb 2005
Hi,

I have a background thread that opens a form. But when that thread ends, the
form I opened disappears. This makes me want the background thread to tell my
main thread to open the form instead.

The form should be opened by my main thread when my communication thread
receives a certain message. I’ve been trying to do this using delegates
(whose existence just recently came to my knowledge), but I’m failing and
have started wondering if it couldn’t be done using events instead.

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
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.
/Daniel
 
Reply With Quote
 
 
 
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      19th Feb 2005
dast <(E-Mail Removed)> wrote:
> I have a background thread that opens a form. But when that thread ends, the
> form I opened disappears. This makes me want the background thread to tell my
> main thread to open the form instead.


Yes - that would be a better idea. *All* UI work should be done on the
main UI thread.

> The form should be opened by my main thread when my communication thread
> receives a certain message. I?ve been trying to do this using delegates
> (whose existence just recently came to my knowledge), but I?m failing and
> have started wondering if it couldn?t be done using events instead.
>
> 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
> End Sub


You need to use Control.Invoke here.

See http://www.pobox.com/~skeet/csharp/t...winforms.shtml

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Communication with threads JJ Microsoft C# .NET 0 4th Jul 2009 12:48 AM
Communication between threads =?Utf-8?B?SmNr?= Microsoft VB .NET 6 7th Aug 2006 04:41 PM
Communication between threads Kosmas Nikolaidis Microsoft Dot NET Framework Forms 4 5th May 2005 12:37 PM
Communication between threads? =?Utf-8?B?ZGFzdA==?= Microsoft VB .NET 3 17th Feb 2005 07:53 AM
communication between threads Julia Sats Microsoft VB .NET 2 4th Feb 2004 12:59 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:49 PM.