473,756 Members | 3,663 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Thread A notifying Thread B of an Event

Using VS 2003, VB, MSDE...

There are two threads, A & B, that continously run and are started by Sub
Main. They instantiationsl of identical code. Thread A handles call
activity on telephone line 1 and Thread B handles call activity on telephone
line 2. They use a common SQL datasource, but all DataSets are unique to
each thread.

Is there a way for thread A to occasionally communication to thread B that
something has happened? The ideal situation would be for thread A to raise
an event that a handler in thread B handles. But, I don't see how to do
that (the raised event would be handled "up the call stack" in Sub Main, not
"horizontia lly" by the other thread).

Specifically, thread A has added a row(s) to the common datasource that I
need thread B to know about. I am currently doing it with a timer in thread
B that check the common datasource for changes every 15 seconds, which works
OK, but am looking for a simpler solution. I have looked at SQL triggers,
but don't see how that would alert thread B. I have looked at RaseEvents,
but don't see any help there either.

Any ideas? Do System.Timers. exert a heavy resource usage toll? If not I
may jus stick with the timer method.

Thansk!

Bob
Nov 20 '05 #1
20 2415

"Bob Day" <Bo****@TouchTa lk.net> wrote in message
news:ub******** ********@TK2MSF TNGP12.phx.gbl. ..
Using VS 2003, VB, MSDE...

There are two threads, A & B, that continously run and are started by Sub
Main. They instantiationsl of identical code. Thread A handles call
activity on telephone line 1 and Thread B handles call activity on telephone line 2. They use a common SQL datasource, but all DataSets are unique to
each thread.

Is there a way for thread A to occasionally communication to thread B that
something has happened? The ideal situation would be for thread A to raise an event that a handler in thread B handles. But, I don't see how to do
that (the raised event would be handled "up the call stack" in Sub Main, not "horizontia lly" by the other thread).

You can use addhandler... If you remember from your thread creation you
have something like

Dim ThreadA as new System.Threadin g.thread(Addres sOf myClass.MyFunct ionA)
Dim ThreadB as new System.Threadin g.Thread(Addres sOf myClass.MyFunct ionB)

now these act like any other class... Which means you can raisevents and
hook between threads (a lot of us design this way, how do you think we make
data intensive UI's so damn quick.=))

So, all those events in A are availible in B through your class
declaration... say classA has eventA and classB has eventB...

So.. lets try this.

sub main()

dim classA as new myClassA
dim classB as new myClassB

AddHandler classA.eventA, New EventHandler(Ad dressOf classB.onEventA )
...thread starting...

end sub

ClassB.onEventA is a matching eventhandler to the class A event (I just
used an argumentless event handler delegate (thats what EventHandler is...)

Delegates ensure communication across threads... (one of there many useful
purposes)

And thats about it...

HTH,
CJ
Specifically, thread A has added a row(s) to the common datasource that I
need thread B to know about. I am currently doing it with a timer in thread B that check the common datasource for changes every 15 seconds, which works OK, but am looking for a simpler solution. I have looked at SQL triggers, but don't see how that would alert thread B. I have looked at RaseEvents,
but don't see any help there either.

Any ideas? Do System.Timers. exert a heavy resource usage toll? If not I
may jus stick with the timer method.

Thansk!

Bob

Nov 20 '05 #2
Thanks for your help, but I am afraid I am going to need far more
explaination. I have played with your comments for some time now, and
cannot get them to work in any meaningful way.

I have no problem with Raising an event that is caught higher up in the call
stack. But raising an event in one thread caught by a 2nd thread handler
simply isn't working as expected.

Question: If thead A raises an event caught by thread B, I would expect the
delgate to be running on thread B . But debug inidcates that thread A is
running the delagate on thread B. Is this correct? This will present a
host of problems.

Can you reference a URL or more detailed areticles on this issue? I have
searched MSDN, and have not come up with sample code or a good explaination.
Any type of sample code the works would be appreciated.

Thanks!

Bob
"Bob Day" <Bo****@TouchTa lk.net> wrote in message
news:ub******** ********@TK2MSF TNGP12.phx.gbl. ..
Using VS 2003, VB, MSDE...

There are two threads, A & B, that continously run and are started by Sub
Main. They instantiationsl of identical code. Thread A handles call
activity on telephone line 1 and Thread B handles call activity on telephone line 2. They use a common SQL datasource, but all DataSets are unique to
each thread.

Is there a way for thread A to occasionally communication to thread B that
something has happened? The ideal situation would be for thread A to raise an event that a handler in thread B handles. But, I don't see how to do
that (the raised event would be handled "up the call stack" in Sub Main, not "horizontia lly" by the other thread).

Specifically, thread A has added a row(s) to the common datasource that I
need thread B to know about. I am currently doing it with a timer in thread B that check the common datasource for changes every 15 seconds, which works OK, but am looking for a simpler solution. I have looked at SQL triggers, but don't see how that would alert thread B. I have looked at RaseEvents,
but don't see any help there either.

Any ideas? Do System.Timers. exert a heavy resource usage toll? If not I
may jus stick with the timer method.

Thansk!

Bob

Nov 20 '05 #3
Hi Bob,

A queue is the preferred way to send data across free-thread boundaries.
There is an online chat that has discussion of this on:
http://msdn.microsoft.com/chats/vstu...dio_041602.asp

Alan Dennis book, .NET Multithreading does quite a good job of explaining
this, and provides example code. You can get information from the publisher
at www.manning.com -- the books is available in both print and eBook
versions.

Dick
--
Richard Grier (Microsoft Visual Basic MVP)

See www.hardandsoftware.net for contact information.

Author of Visual Basic Programmer's Guide to Serial Communications, 3rd
Edition ISBN 1-890422-27-4 (391 pages) published February 2002.
Nov 20 '05 #4
Hi Bob,

Thanks for posting in the community.

First of all, I would like to confirm my understanding of your issue.
From your description, I understand that you have two threads, and you hope
thread A will tell thread B that something has been done.
Have I fully understood you? If there is anything I misunderstood, please
feel free to let me know.

Based on my experience, if the event handler is on thread A, it will be
fired on Thread A and will not on thread B. To achieve your aim, the Timer
will be a good solution, also you may try to use the .net framework buildin
synchronized object.
e.g. Mutex.
You can wait for the Mutex in Thread B, and after the Thread A has done
something, thread A will realease the mutex and then thread B which is
waiting for the object return and the Thread B will run continuely.

Synchronizing Data for Multithreading
http://msdn.microsoft.com/library/de...us/cpguide/htm
l/cpconmanagedthr eadingsupport.a sp

Mutex
http://msdn.microsoft.com/library/de...us/cpguide/htm
l/cpconmutex.asp
Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 20 '05 #5
Based on my experience, if the event handler is on thread A, it will be
fired on Thread A and will not on thread B. To achieve your aim, the Timer
will be a good solution,
A Timer? How on earth did you get a job at MS Peter? I swear your
"solutions" are the most ill thought out ideas on the planet. Again, you
have completly missed the point on this dicussion and have offered nothing
valuable to add to it.
also you may try to use the .net framework buildin
synchronized object.
e.g. Mutex.
I don't believe synchronization was what he was asking....
You can wait for the Mutex in Thread B, and after the Thread A has done
something, thread A will realease the mutex and then thread B which is
waiting for the object return and the Thread B will run continuely.

Synchronizing Data for Multithreading
http://msdn.microsoft.com/library/de...us/cpguide/htm l/cpconmanagedthr eadingsupport.a sp

Mutex
http://msdn.microsoft.com/library/de...us/cpguide/htm l/cpconmutex.asp
Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 20 '05 #6
Cor
Hi CJ,

I have read it 10 times (the problem) I did that because it is for Peter
difficult to defend himself. I think that a timer is in this case one of the
solutions (and in my eyes even the best).

The main difficult is the problem description, in my opinion it starts with
bringing us probably on the wrong leg. It is telling about a common
datasource, probably meant with that the database while we are using the
word common datasource for something else (not that it is used wrong, but
just because we are using it mostly in another meaning)...

Than it is for the rest one solution which uses in every thread its own
datatables completely separated as well in the database as in the datasets
(it are 2 lines, but if it where hundred it was the same in my idea)

Then there is a horizontal event necessary, and that is weird, in this
situation, there is no need for line 1 to see activity on line 2 and visa
versa (think that it would be 100lines).

The main program can be used to keep track on the activities time by time by
information given to it from the threads. (Not told that it should, this is
just guessing).

For that is a timer very good usable in my idea.

And I do not think that Peter's sollution are that bad, he is given them
mostly on not answered threads and therefore he has probably to guess a lot.

Cor
Nov 20 '05 #7
Cor,
Hi CJ,

I have read it 10 times (the problem) I did that because it is for Peter
difficult to defend himself. I think that a timer is in this case one of the solutions (and in my eyes even the best).

Timers just seem to be wasted time on the CPU. Not the time they take to
iterate (because I know they sleep their thread until its time to raise the
tick). But to constnatly parse through your data tables to notice if there
is a change seems HIGHLY inefficient.

As for horizontal events, I don't see this as being a big problem. Now,
maybe I didn't explain myself well enough, I don't know. But look at it
this way.

In order to work horizontally, ThreadA needs to know the existance of
ThreadB and ThreadB needs to know the existance of threadA. So you can do
this with simple object references. Obviously, some thread has to start
both A and B (they just can't start out of no where) so that can place a
property in A and a property in B to be references back to the opposite
class that is running the method on a separate thread.

Now, since we have both references within the object (even though they are
running on different threads) we can still hook the events to that
particular instance, then use a delegate to bring it over the thread boundry
to update the dataset on the opposing thread.

So, what are we supposed to hook? Well, internally in each class you could
tell it to hook the rowchanged/rowchanging event on a datatable, then define
your own event to hook between the threads (to send additional data if so
desired).

Then on the actual methods that process the changes, do whatever you want
within that separated thread.

Now, does this work on 100 lines? Sorta, it requires a little more work to
make it work properly, but that wasn't the question at hand now was it.
Same concept though, except you could use the main thread to do the
delegating...

But a timer? come on Cor...

The main difficult is the problem description, in my opinion it starts with bringing us probably on the wrong leg. It is telling about a common
datasource, probably meant with that the database while we are using the
word common datasource for something else (not that it is used wrong, but
just because we are using it mostly in another meaning)...

Than it is for the rest one solution which uses in every thread its own
datatables completely separated as well in the database as in the datasets
(it are 2 lines, but if it where hundred it was the same in my idea)

Then there is a horizontal event necessary, and that is weird, in this
situation, there is no need for line 1 to see activity on line 2 and visa
versa (think that it would be 100lines).

The main program can be used to keep track on the activities time by time by information given to it from the threads. (Not told that it should, this is just guessing).

For that is a timer very good usable in my idea.

And I do not think that Peter's sollution are that bad, he is given them
mostly on not answered threads and therefore he has probably to guess a lot.
Guesses a lot? I'm going to use that one next time a company asks me why
something doesn't work. I'm just going to say "Yeah, I guessed on how it
should work... sorry it cost you so much money... but such is life."
Cor

Nov 20 '05 #8
Cor
Hi CJ,

I think it is not an horizontaly approach and that is the problem.
I think the current solution should be seen as something like this.

Sub main
- thread B (which uses a timer to control)
- thread A.

While it should be in my opinion
Sub main (Which uses a system.threadin g.timer to do checking time by time,
or a better method if it is needed that if it has to be in real time)
- thread A
- thread B
- thread Xn.

Than could all the threads be from the same class.

But as I said, the problem is not really clear for me.

But again just my thought.

And what about Peter, your message did look to me a little bit if he did
everything wrong.
But I think that makes mistakes like you and me ("lessons") but not that
much as I got the idea from your message.

:-))

Cor

Nov 20 '05 #9
Cor
Hi CJ,

I did not know how to write this, now I know, I was mixing you up with
someone totally different sometimes active in this newsgroup.

Stuppid me, I would have written it in a different way normaly to you.

(much less lines)

But mistakes are lessons

:-))

Cor

Nov 20 '05 #10

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

Similar topics

26
2377
by: news.microsoft.com | last post by:
Hi, Currently I have a thread thats spinning and doing a Thread.Sleep(someTime). I was thinking of changing this to Thread.Sleep(Timeout.Infinite); then when I have actual data in a collection to process in this thread, I was going to do a _thread.Interrupt(); Would this be a better approach to spinning a thread on ?
1
4475
by: benmorganpowell | last post by:
I have a small windows service which connects to a POP3 server at defined intervals, scans the available messages, extracts the required information and inserts the data into a SQL database. I am assuming that this is not an uncommon piece of software. I want to get an architecture that conforms as closely as possible with the recommendations from Microsoft on developing Windows Services, but to be honest I have found difficultly in...
2
2067
by: Bob Day | last post by:
Using VS 2003, Vb, MSDE... Option 1 ------------------------------------------- Thread A and B are instantiations of 2 different classes. If thread A raises an event caught by thread B, the delagate in thread B that executes is 1) running under thread A based on the debug threading window and 2) does not seem to be aware of any instantiations of thread B in its declarations section. See **** for what fails. Class B (which is thread...
7
2695
by: Charles Law | last post by:
My first thought was to call WorkerThread.Suspend but the help cautions against this (for good reason) because the caller has no control over where the thread actually stops, and it might have a lock pending, for example. I want to be able to stop a thread temporarily, and then optionally resume it or stop it for good.
2
4148
by: Tim | last post by:
The are 2 threads - main thread and worker thread. The main thread creates the worker thread and then the worker thread runs in an infinite while loop till it is asked to exit by setting an event. The worker thread instantiates an object and calls methods of that object. If some method call fails, the main thread needs to be notified about the failure. What mechanisms exist for notifying the main thread about the worker thread's status -...
9
1116
by: Tim | last post by:
if I start a thread mythread.start and then I click a button that has a do loop with no doevents in the loop, do loop until myval=true
11
1954
by: mark | last post by:
Right now I have a thread that sleeps for sometime and check if an event has happened and go back to sleep. Now instead I want the thread to sleep until the event has occured process the event and go back to sleep. How to do this? thanks mark class eventhndler(threading.Thread): def __init__(self):
18
2537
by: J.K. Baltzersen | last post by:
To whomever it may concern: I am using MS Visual C++ 6.0. I have a process A which instantiates an object C. At a later point the process A creates the thread B. The thread B has access to the object C.
0
9273
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9872
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9711
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8712
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7244
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6534
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3805
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3358
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2666
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.