Re: Thread A notifying Thread B of an Event

From: CJ Taylor (nospam_at_blowgoats.com)
Date: 02/20/04


Date: Fri, 20 Feb 2004 13:51:56 -0600


"Bob Day" <BobDay@TouchTalk.net> wrote in message
news:ubnnMT%239DHA.1504@TK2MSFTNGP12.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
> "horizontially" by the other thread).
>

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

Dim ThreadA as new System.Threading.thread(AddressOf myClass.MyFunctionA)
Dim ThreadB as new System.Threading.Thread(AddressOf myClass.MyFunctionB)

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(AddressOf 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
>
>



Relevant Pages

  • RE: Reuse code on multiple pages of project
    ... Thats a good practical start. ... ' Make the sub public so you can call the sub if you want to call it from the outside ... When you say that a method is shared, like you do with SetFocus, you make it so that the user of that function doesnt have to instantiate the class to an object to use the function. ... Put your class in a namespace and make all its functions shared. ...
    (microsoft.public.dotnet.framework.aspnet)
  • Thread A notifying Thread B of an Event
    ... There are two threads, A & B, that continously run and are started by Sub ... an event that a handler in thread B handles. ... thread A has added a rowto the common datasource that I ... may jus stick with the timer method. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Debug Q
    ... thats why I tended to copy, then paste / special with the target sheet. ... > End Sub ... >>> Don Guillett ... >>> SalesAid Software ...
    (microsoft.public.excel)
  • Re: register css style sheet
    ... but you can dynamically allocate them via attributes if thats ... Sub Page_Load(Sender As Object, E As EventArgs) ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Many Tried It Byt Can U...
    ... I did as u recommended but now i have a scenario, ... from A1 and The result in B1 shows No thats fine but i want to do the ... ) Is Nothing Then Exit Sub ... Student, working on a project ...
    (microsoft.public.excel.programming)