Events not always called depending on application type

P

Philipp Sumi

Hello newsgroup

I'm using a library to discover UPnP devices on the network via sockets.
Its use is pretty straightforward:

private void Discover()
DevDiscovery disco = new DevDiscovery ();
disco.OnAddedDevice += new DevDiscovery.DiscoveryHandler(OnAdded);

disco.Start();
}



This code works always, when placed within a main-method market with a
[STAThread]-attribute (console app or winform). I'm quite sure it works
always with this code within a windows form if I'm using Invoke():

protected override void OnHandleCreated(EventArgs e)
{
Invoke(new MethodInvoker(Discover));
}



But is does *not* work always, if I'm starting the discovery from within
a windows form control! Sometimes, the event handlers are being called,
sometimes not. I guess, it's about the threading model, but I have no
clue how to solve it...

Thanks for your advice

Philipp
 
N

Nicholas Paldino [.NET/C# MVP]

Philipp,

It is possible that the threading model has something to do with it,
depending on whether or not COM interop is involved. However, I see a
bigger problem. Basically, the Discover method is creating an instance of
DevDiscovery, and immediately letting go of the reference. This will
continue to fire events until there is a garbage collection, at which point
it is going to stop listening and stop firing events. You need to store
this reference somewhere so that you have an active object that can fire
events and receive the UPnP notifications.

Hope this helps.
 
P

Philipp Sumi

Hello Nicholas

Great observation - I just took over the automatically generated
discovery code without considering the environment in my application. I
could just make a short trial but could not reproduce the error with the
fixed code :)

Thank you very much!

Philipp
 

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

Top