Thread and "callbacks"

  • Thread starter Thread starter GTi
  • Start date Start date
G

GTi

Easy question for experts:

1)
I have a main application.
This application use a class in a library (MyLib)
ListenClass lc = new ListenClass();
I also have a "callback" class named
class GetListenDataClass(string data)


2)
I have a library (MyLib) that have a class ( ListenClass ) that again
creates a thread.
This thread will listen to a port and when data comes I want this
thread to send the data back to my main application :
GetListenDataClass(string data).

How can I do this?
Is this called delegates?
[Please use teaspoon when explaining it to me - I don't understand it]
 
GTi,

Either that, or you pushed send 6 times. Note that when you send something
it usually takes a few minutes for the post to show up.

Kind regards,
 
Gti,

GTi said:
Easy question for experts:

1)
I have a main application.
This application use a class in a library (MyLib)
ListenClass lc = new ListenClass();
I also have a "callback" class named
class GetListenDataClass(string data)


2)
I have a library (MyLib) that have a class ( ListenClass ) that again
creates a thread.
This thread will listen to a port and when data comes I want this
thread to send the data back to my main application :
GetListenDataClass(string data).

How can I do this?
Is this called delegates?
[Please use teaspoon when explaining it to me - I don't understand it]

The class which listens to the port should expose an event to which
subscribers that are interested in what is sent to the port can register.
I'll sketch this:

// listener (producer)
public delegate void IncomingDataHandler( string data );
public class IncomingDataListener
{
public event IncomingDataHandler IncomingData;
// ...
private void FireIncomingData( string data )
{
IncomingDataHandler l_IncomingData = IncomingData;
if( l_IncomingData != null )
l_IncomingData( data );
}
private void MonitorIncomingData( )
{
while( true )
FireIncomingData( GetIncomingData( ) );
}
private string GetIncomingData( )
{
// your port listener, blocks until data is available
// and returns it as string
}
}

// subscriber (consumer)
public class DataConsumer : IDisposable
{
private IncomingDataListener idl;
public DataConsumer( IncomingDataListener idl )
{
this.idl = idl;
idl.IncomingData += new IncomingDataHandler (
ConsumeIncomingData
);
}
public void Dispose( )
{
idl.IncomingData -= new IncomingDataHandler (
ConsumeIncomingData
);
}
private void ConsumeIncomingData( string data )
{
// consume data, don't forget to synchronize!
}
}

Kind regards,
 
TT said:
GTi,

Either that, or you pushed send 6 times. Note that when you send something
it usually takes a few minutes for the post to show up.

Kind regards,

Yes and No...
The [Post message] button returned a service down error page. So I get
back waited and pressed the button again. Then after a few attempts it
worked OK again. But it posted the messages even that it gived med a
error.
I'm sorry about that.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top