472,787 Members | 1,346 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,787 software developers and data experts.

Communication between threads?

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
Jul 21 '05 #1
1 2369
dast <da**@discussions.microsoft.com> 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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Ayende Rahien | last post by:
Is it possible to use events as a communication mechanism between threads?
1
by: Noel | last post by:
Hi, I am a tad confused about if there are any benefits from using asynchronous vs synchronous network communication. As my example, I have been writing a dns lookup stack with a network...
9
by: Emmanuel Charruau | last post by:
Hi, I am looking for a class or any information which would allow me to make communicate mini-module in c++. I have been looking on the net for some examples of such implementation, but I did...
3
by: Bryan Christopher | last post by:
Hello All! I have a rather abstract question for some genius out there to answer. I want to integrate communication tracking, for customer relations, into an existing Access DB. What I was going...
2
by: WTH | last post by:
with a C# client (and/or server, but server not important)? I've got a scalable high speed (uses completion ports) C++ TCP/IP communication server but I'd like to write a C# client that other C#...
21
by: dast | last post by:
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...
8
by: Roodie | last post by:
Hello, I have a quite specific problem, and after half a day spent with Google, still no solid answer. The task: I have to write a C# class Library ( DLL ) to handle TCP socket...
2
by: ne.seri | last post by:
In short, I'm building a kind of server which is supposed to handle open connections with clients. E.g. client connects to the server, the connection stays open, client sends a request to the...
6
by: Jck | last post by:
Could someone tell me how a thread can send data / post message to another thread in vb.net please? I have a main thread and a socket thread. I need a way to let the main thread know when my...
1
by: batista | last post by:
Hello all, I need suggestions and possibly solutions to the problem stated below: I have an application written purely in .NET ( Windows Form Application) and another application that is...
0
by: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.