473,385 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Firing events from multiple threads

I have a class that writes data to the serial port and has a separate thread
to read data from the same port. Typically the main thread writes requests
to the serial port and then returns to the caller. The read thread waits for
a response while the application continues running. When a response is
received, I want to fire an event. But, I don't want the read thread to
raise the event because the event could spawn a whole set of new processing
by the application. I'd like the read thread to buffer the data and then go
back to reading. How can I signal the main thread to raise the event once
the data is buffered?
Nov 21 '05 #1
11 1187
Look in ManualResetEvent or AutoResetEvent

http://msdn.microsoft.com/library/de...classtopic.asp

"TrtnJohn" <Tr******@discussions.microsoft.com> wrote in message
news:5F**********************************@microsof t.com...
I have a class that writes data to the serial port and has a separate
thread
to read data from the same port. Typically the main thread writes
requests
to the serial port and then returns to the caller. The read thread waits
for
a response while the application continues running. When a response is
received, I want to fire an event. But, I don't want the read thread to
raise the event because the event could spawn a whole set of new
processing
by the application. I'd like the read thread to buffer the data and then
go
back to reading. How can I signal the main thread to raise the event once
the data is buffered?

Nov 21 '05 #2
Thanks Some Guy. But, if I don't want my main thread to block waiting for
the event. I'd like the notification to be asynchonous. Otherwise, the
application will no longer process window messages while my class is waiting
for a response.

"Some Guy" wrote:
Look in ManualResetEvent or AutoResetEvent

http://msdn.microsoft.com/library/de...classtopic.asp

"TrtnJohn" <Tr******@discussions.microsoft.com> wrote in message
news:5F**********************************@microsof t.com...
I have a class that writes data to the serial port and has a separate
thread
to read data from the same port. Typically the main thread writes
requests
to the serial port and then returns to the caller. The read thread waits
for
a response while the application continues running. When a response is
received, I want to fire an event. But, I don't want the read thread to
raise the event because the event could spawn a whole set of new
processing
by the application. I'd like the read thread to buffer the data and then
go
back to reading. How can I signal the main thread to raise the event once
the data is buffered?


Nov 21 '05 #3
Hint: Control.Invoke Method (Delegate).
I think you can figure it out if you understand Control.Invoke Method
(Delegate).

"TrtnJohn" wrote:
I have a class that writes data to the serial port and has a separate thread
to read data from the same port. Typically the main thread writes requests
to the serial port and then returns to the caller. The read thread waits for
a response while the application continues running. When a response is
received, I want to fire an event. But, I don't want the read thread to
raise the event because the event could spawn a whole set of new processing
by the application. I'd like the read thread to buffer the data and then go
back to reading. How can I signal the main thread to raise the event once
the data is buffered?

Nov 21 '05 #4
You can use another thread to process the data. This way your main thread
won't be blocked. Or you can call BeginInvoke on your main form to call a
function asynchronously. Be aware that using Invoke instead of BeginInvoke
will block until it's finished.

"TrtnJohn" <Tr******@discussions.microsoft.com> wrote in message
news:47**********************************@microsof t.com...
Thanks Some Guy. But, if I don't want my main thread to block waiting for
the event. I'd like the notification to be asynchonous. Otherwise, the
application will no longer process window messages while my class is
waiting
for a response.

"Some Guy" wrote:
Look in ManualResetEvent or AutoResetEvent

http://msdn.microsoft.com/library/de...classtopic.asp

"TrtnJohn" <Tr******@discussions.microsoft.com> wrote in message
news:5F**********************************@microsof t.com...
>I have a class that writes data to the serial port and has a separate
>thread
> to read data from the same port. Typically the main thread writes
> requests
> to the serial port and then returns to the caller. The read thread
> waits
> for
> a response while the application continues running. When a response is
> received, I want to fire an event. But, I don't want the read thread
> to
> raise the event because the event could spawn a whole set of new
> processing
> by the application. I'd like the read thread to buffer the data and
> then
> go
> back to reading. How can I signal the main thread to raise the event
> once
> the data is buffered?


Nov 21 '05 #5
Rulin,

This looks like a good suggestion. But, does this mean my class must
inherit from System.Windows.Forms.Control? You can't do this type of call
otherwise?

"Rulin Hong" wrote:
Hint: Control.Invoke Method (Delegate).
I think you can figure it out if you understand Control.Invoke Method
(Delegate).

"TrtnJohn" wrote:
I have a class that writes data to the serial port and has a separate thread
to read data from the same port. Typically the main thread writes requests
to the serial port and then returns to the caller. The read thread waits for
a response while the application continues running. When a response is
received, I want to fire an event. But, I don't want the read thread to
raise the event because the event could spawn a whole set of new processing
by the application. I'd like the read thread to buffer the data and then go
back to reading. How can I signal the main thread to raise the event once
the data is buffered?

Nov 21 '05 #6
Use your forms BeginInvoke.
"TrtnJohn" <Tr******@discussions.microsoft.com> wrote in message
news:29**********************************@microsof t.com...
Rulin,

This looks like a good suggestion. But, does this mean my class must
inherit from System.Windows.Forms.Control? You can't do this type of call
otherwise?

"Rulin Hong" wrote:
Hint: Control.Invoke Method (Delegate).
I think you can figure it out if you understand Control.Invoke Method
(Delegate).

"TrtnJohn" wrote:
> I have a class that writes data to the serial port and has a separate
> thread
> to read data from the same port. Typically the main thread writes
> requests
> to the serial port and then returns to the caller. The read thread
> waits for
> a response while the application continues running. When a response is
> received, I want to fire an event. But, I don't want the read thread
> to
> raise the event because the event could spawn a whole set of new
> processing
> by the application. I'd like the read thread to buffer the data and
> then go
> back to reading. How can I signal the main thread to raise the event
> once
> the data is buffered?

Nov 21 '05 #7
Thanks a lot for the help. I am now making good progress on this issue.
But, I am not sure I can just use the Form.BeginInvoke because I am
developing a class library that will be used by multiple Window's
applications. So, I don't really have a form. So far, I can't seem to find
anyway for a class library to do this. Maybe I should be providing a
UserControl instead? But, this can be a problem because I would also like to
expose the same class library to COM because some applications I need to
support are COM based. The plot thickens :(
"Some Guy" wrote:
Use your forms BeginInvoke.
"TrtnJohn" <Tr******@discussions.microsoft.com> wrote in message
news:29**********************************@microsof t.com...
Rulin,

This looks like a good suggestion. But, does this mean my class must
inherit from System.Windows.Forms.Control? You can't do this type of call
otherwise?

"Rulin Hong" wrote:
Hint: Control.Invoke Method (Delegate).
I think you can figure it out if you understand Control.Invoke Method
(Delegate).

"TrtnJohn" wrote:

> I have a class that writes data to the serial port and has a separate
> thread
> to read data from the same port. Typically the main thread writes
> requests
> to the serial port and then returns to the caller. The read thread
> waits for
> a response while the application continues running. When a response is
> received, I want to fire an event. But, I don't want the read thread
> to
> raise the event because the event could spawn a whole set of new
> processing
> by the application. I'd like the read thread to buffer the data and
> then go
> back to reading. How can I signal the main thread to raise the event
> once
> the data is buffered?


Nov 21 '05 #8
Will the applications that use the library have a form?

"TrtnJohn" <Tr******@discussions.microsoft.com> wrote in message
news:D2**********************************@microsof t.com...
Thanks a lot for the help. I am now making good progress on this issue.
But, I am not sure I can just use the Form.BeginInvoke because I am
developing a class library that will be used by multiple Window's
applications. So, I don't really have a form. So far, I can't seem to
find
anyway for a class library to do this. Maybe I should be providing a
UserControl instead? But, this can be a problem because I would also like
to
expose the same class library to COM because some applications I need to
support are COM based. The plot thickens :(
"Some Guy" wrote:
Use your forms BeginInvoke.
"TrtnJohn" <Tr******@discussions.microsoft.com> wrote in message
news:29**********************************@microsof t.com...
> Rulin,
>
> This looks like a good suggestion. But, does this mean my class must
> inherit from System.Windows.Forms.Control? You can't do this type of
> call
> otherwise?
>
> "Rulin Hong" wrote:
>
>> Hint: Control.Invoke Method (Delegate).
>> I think you can figure it out if you understand Control.Invoke Method
>> (Delegate).
>>
>> "TrtnJohn" wrote:
>>
>> > I have a class that writes data to the serial port and has a
>> > separate
>> > thread
>> > to read data from the same port. Typically the main thread writes
>> > requests
>> > to the serial port and then returns to the caller. The read thread
>> > waits for
>> > a response while the application continues running. When a response
>> > is
>> > received, I want to fire an event. But, I don't want the read
>> > thread
>> > to
>> > raise the event because the event could spawn a whole set of new
>> > processing
>> > by the application. I'd like the read thread to buffer the data and
>> > then go
>> > back to reading. How can I signal the main thread to raise the
>> > event
>> > once
>> > the data is buffered?


Nov 21 '05 #9
Sometimes they will be. But, I cannot assume that they always will. In some
cases, they may just be window-less processes or even NT services. I am
starting to think that asynchronous processing may not be possible given my
requirements. Instead I may have to stick with synchronous request/reply
processing. In this case, the caller will block until the reply or timeout
occurs. Blocking will cause form based applications to go un-responsive.
But, these applications could use Async method calls and/or
Control.BeginInvoke calls when using my library. Or, better yet, I could
provide this as an optional behavior of my class. (client passes reference
of form to me). Do you have any other ideas?

"Some Guy" wrote:
Will the applications that use the library have a form?

"TrtnJohn" <Tr******@discussions.microsoft.com> wrote in message
news:D2**********************************@microsof t.com...
Thanks a lot for the help. I am now making good progress on this issue.
But, I am not sure I can just use the Form.BeginInvoke because I am
developing a class library that will be used by multiple Window's
applications. So, I don't really have a form. So far, I can't seem to
find
anyway for a class library to do this. Maybe I should be providing a
UserControl instead? But, this can be a problem because I would also like
to
expose the same class library to COM because some applications I need to
support are COM based. The plot thickens :(
"Some Guy" wrote:
Use your forms BeginInvoke.
"TrtnJohn" <Tr******@discussions.microsoft.com> wrote in message
news:29**********************************@microsof t.com...
> Rulin,
>
> This looks like a good suggestion. But, does this mean my class must
> inherit from System.Windows.Forms.Control? You can't do this type of
> call
> otherwise?
>
> "Rulin Hong" wrote:
>
>> Hint: Control.Invoke Method (Delegate).
>> I think you can figure it out if you understand Control.Invoke Method
>> (Delegate).
>>
>> "TrtnJohn" wrote:
>>
>> > I have a class that writes data to the serial port and has a
>> > separate
>> > thread
>> > to read data from the same port. Typically the main thread writes
>> > requests
>> > to the serial port and then returns to the caller. The read thread
>> > waits for
>> > a response while the application continues running. When a response
>> > is
>> > received, I want to fire an event. But, I don't want the read
>> > thread
>> > to
>> > raise the event because the event could spawn a whole set of new
>> > processing
>> > by the application. I'd like the read thread to buffer the data and
>> > then go
>> > back to reading. How can I signal the main thread to raise the
>> > event
>> > once
>> > the data is buffered?


Nov 21 '05 #10
You can use a Delegate to call BeginInvoke. This is what you are looking
for.

http://msdn.microsoft.com/library/de...mingsample.asp
"TrtnJohn" <Tr******@discussions.microsoft.com> wrote in message
news:61**********************************@microsof t.com...
Sometimes they will be. But, I cannot assume that they always will. In
some
cases, they may just be window-less processes or even NT services. I am
starting to think that asynchronous processing may not be possible given
my
requirements. Instead I may have to stick with synchronous request/reply
processing. In this case, the caller will block until the reply or
timeout
occurs. Blocking will cause form based applications to go un-responsive.
But, these applications could use Async method calls and/or
Control.BeginInvoke calls when using my library. Or, better yet, I could
provide this as an optional behavior of my class. (client passes
reference
of form to me). Do you have any other ideas?

"Some Guy" wrote:
Will the applications that use the library have a form?

"TrtnJohn" <Tr******@discussions.microsoft.com> wrote in message
news:D2**********************************@microsof t.com...
> Thanks a lot for the help. I am now making good progress on this
> issue.
> But, I am not sure I can just use the Form.BeginInvoke because I am
> developing a class library that will be used by multiple Window's
> applications. So, I don't really have a form. So far, I can't seem to
> find
> anyway for a class library to do this. Maybe I should be providing a
> UserControl instead? But, this can be a problem because I would also
> like
> to
> expose the same class library to COM because some applications I need
> to
> support are COM based. The plot thickens :(
>
>
> "Some Guy" wrote:
>
>> Use your forms BeginInvoke.
>>
>>
>> "TrtnJohn" <Tr******@discussions.microsoft.com> wrote in message
>> news:29**********************************@microsof t.com...
>> > Rulin,
>> >
>> > This looks like a good suggestion. But, does this mean my class
>> > must
>> > inherit from System.Windows.Forms.Control? You can't do this type
>> > of
>> > call
>> > otherwise?
>> >
>> > "Rulin Hong" wrote:
>> >
>> >> Hint: Control.Invoke Method (Delegate).
>> >> I think you can figure it out if you understand Control.Invoke
>> >> Method
>> >> (Delegate).
>> >>
>> >> "TrtnJohn" wrote:
>> >>
>> >> > I have a class that writes data to the serial port and has a
>> >> > separate
>> >> > thread
>> >> > to read data from the same port. Typically the main thread
>> >> > writes
>> >> > requests
>> >> > to the serial port and then returns to the caller. The read
>> >> > thread
>> >> > waits for
>> >> > a response while the application continues running. When a
>> >> > response
>> >> > is
>> >> > received, I want to fire an event. But, I don't want the read
>> >> > thread
>> >> > to
>> >> > raise the event because the event could spawn a whole set of new
>> >> > processing
>> >> > by the application. I'd like the read thread to buffer the data
>> >> > and
>> >> > then go
>> >> > back to reading. How can I signal the main thread to raise the
>> >> > event
>> >> > once
>> >> > the data is buffered?
>>
>>
>>


Nov 21 '05 #11
I got excited when I first saw that too. But, Delegate.BeginInvoke is not
the same as Control.BeginInvoke. There is no thread marshalling. The call
will not occur on the primary UI thread. I found 2 really good articles in
MSDN magazine that describe this problem in detail:

http://msdn.microsoft.com/msdnmag/is...s/default.aspx

and here:

http://msdn.microsoft.com/msdnmag/is...s/default.aspx



"Some Guy" wrote:
You can use a Delegate to call BeginInvoke. This is what you are looking
for.

http://msdn.microsoft.com/library/de...mingsample.asp
"TrtnJohn" <Tr******@discussions.microsoft.com> wrote in message
news:61**********************************@microsof t.com...
Sometimes they will be. But, I cannot assume that they always will. In
some
cases, they may just be window-less processes or even NT services. I am
starting to think that asynchronous processing may not be possible given
my
requirements. Instead I may have to stick with synchronous request/reply
processing. In this case, the caller will block until the reply or
timeout
occurs. Blocking will cause form based applications to go un-responsive.
But, these applications could use Async method calls and/or
Control.BeginInvoke calls when using my library. Or, better yet, I could
provide this as an optional behavior of my class. (client passes
reference
of form to me). Do you have any other ideas?

"Some Guy" wrote:
Will the applications that use the library have a form?

"TrtnJohn" <Tr******@discussions.microsoft.com> wrote in message
news:D2**********************************@microsof t.com...
> Thanks a lot for the help. I am now making good progress on this
> issue.
> But, I am not sure I can just use the Form.BeginInvoke because I am
> developing a class library that will be used by multiple Window's
> applications. So, I don't really have a form. So far, I can't seem to
> find
> anyway for a class library to do this. Maybe I should be providing a
> UserControl instead? But, this can be a problem because I would also
> like
> to
> expose the same class library to COM because some applications I need
> to
> support are COM based. The plot thickens :(
>
>
> "Some Guy" wrote:
>
>> Use your forms BeginInvoke.
>>
>>
>> "TrtnJohn" <Tr******@discussions.microsoft.com> wrote in message
>> news:29**********************************@microsof t.com...
>> > Rulin,
>> >
>> > This looks like a good suggestion. But, does this mean my class
>> > must
>> > inherit from System.Windows.Forms.Control? You can't do this type
>> > of
>> > call
>> > otherwise?
>> >
>> > "Rulin Hong" wrote:
>> >
>> >> Hint: Control.Invoke Method (Delegate).
>> >> I think you can figure it out if you understand Control.Invoke
>> >> Method
>> >> (Delegate).
>> >>
>> >> "TrtnJohn" wrote:
>> >>
>> >> > I have a class that writes data to the serial port and has a
>> >> > separate
>> >> > thread
>> >> > to read data from the same port. Typically the main thread
>> >> > writes
>> >> > requests
>> >> > to the serial port and then returns to the caller. The read
>> >> > thread
>> >> > waits for
>> >> > a response while the application continues running. When a
>> >> > response
>> >> > is
>> >> > received, I want to fire an event. But, I don't want the read
>> >> > thread
>> >> > to
>> >> > raise the event because the event could spawn a whole set of new
>> >> > processing
>> >> > by the application. I'd like the read thread to buffer the data
>> >> > and
>> >> > then go
>> >> > back to reading. How can I signal the main thread to raise the
>> >> > event
>> >> > once
>> >> > the data is buffered?
>>
>>
>>


Nov 21 '05 #12

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

Similar topics

3
by: Bob Lazarchik | last post by:
Hello: I need to fire events between two threads. Any suggestions on the best way to accomplish this. Scenerio: Thread A is main thread. Thread B is a Com port listener that fires an event...
5
by: bryanp10 | last post by:
I have a page that is almost entirely dynamically created. Textboxes and checkbox are working fine, firing events, and persistent their state. DropDownList is giving me a major headache. All...
2
by: Tumurbaatar S. | last post by:
ASP.NET QuickStart Tutorial says that: .... ASP.NET maintains a pool of HttpApplication instances over the course of a Web application's lifetime. ASP.NET automatically assigns one of these...
4
by: | last post by:
I'm looking to understand the way events work across multiple threads. I have an object that needs to process data as it comes in. When a certain threshold is hit, it needs to tell the host...
2
by: PAzevedo | last post by:
I have this Hashtable of Hashtables, and I'm accessing this object from multiple threads, now the Hashtable object is thread safe for reading, but not for writing, so I lock the object every time I...
2
by: mswlogo | last post by:
I looked high and low for code to do this and finally found some VB code that did it right. This is a C# flavor of it. public event EventHandler<EventArgsMyEventToBeFired; public void...
35
by: keerthyragavendran | last post by:
hi i'm downloading a single file using multiple threads... how can i specify a particular range of bytes alone from a single large file... for example say if i need only bytes ranging from...
14
by: Gotch | last post by:
Hi all, I've recently digged into C# and the whole .Net stuff. Particularly I found the idea of adding Events and Delegates to the C# language very interesting and I'm trying to use them in...
5
by: =?Utf-8?B?VmFubmk=?= | last post by:
Hi, I have a component where I need to have thread-safe access to a list. Operations on the list are done in critical sections (lock (lockObject) { ... } ) in the usual way, to make sure that no...
16
by: WATYF | last post by:
Hi there... I have a huge text file that needs to be processed. At the moment, I'm loading it into memory in small chunks (x amount of lines) and processing it that way. I'd like the process to be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.