Simple prob with events, pls help

D

Daniel

I want to do something really simple.

I have a form and a class which handles my network layer

The form has an instance of the network class as a member and initialises
the network layer.

Inside the network class it then starts dealing with connections and
listening (server).

When a connection is made i have a method in the network class that is
called.

On the form i have a listbox. I want the method in the network class to add
an item to the listbox on the form.

I thought the best way to do this was delgates but it seems delegates are
only good for raising events within the classitself and not out onto forms
tha contain it.

Is there anyway to attach an event from within my network class to the
listbox on the form so that whenever that event is fired i can tell the
listbox to catch it and update itself?

Thanks
 
D

Daniel

Thanks, i got it working but now my problem is that i am using two different
threads.

i wanted thread1 to trigger the event, and then thread2 to handle it.

I keep getting a error saying this cant be done? Can it be done?
 
B

Bruce Wood

Is there anyway to attach an event from within my network class to the
listbox on the form so that whenever that event is fired i can tell the
listbox to catch it and update itself?

You wouldn't have the ListBox react to the event. Instead, you would
have the Form listen for the event and then take some action as a
result, which in your case would be add an item to the ListBox.

Assuming that your network connection class makes only one connection
(so the event itself doesn't need to carry with it information about
_which_ connection was made), your code might look something like this:

In your network connection class:

public class NetworkConnection
{
...
public event System.EventHandler ConnectionEstablished;
...
private EstablishConnection(...)
{
... do some stuff to establish the connection ...
if (this.ConnectionEstablished != null)
{
this.ConnectionEstablished(this, System.EventArgs.Empty);
}
}
}

Then, inside your Form class:

public class MyForm : Form
{
private NetworkConnection _connection;

public MyForm()
{
InitializeComponents();
this._connection = new NetworkConnection(...);
this._connection.ConnectionEstablished += new
System.EventHandler(HandleNewConnection);
}
...
private void HandleNewConnection(object sender, System.EventArgs e)
{
NetworkConnection conn = (NetworkConnection)sender;
... now add an item to the ListBox because of the new
connection established by NetworkConnection "conn" ...
}
}
 
G

Guest

In Windows Forms, UI must be updated on the main thread, handle your event in
the form code as Bruce has suggested.
 
B

Bruce Wood

Yes, it can be done. You need to look at Invoke / BeginInvoke. See Jon
Skeet's article on threading in Windows Forms:

http://www.yoda.arachsys.com/csharp/threads/winforms.shtml

I believe that the upshot is that in your Form's event handler, you
need to invoke another form method to do the actual update on the
ListBox:

private void HandleNewConnection(object sender, System.EventArgs e)

{
if (this._listBox1.InvokeRequired)
{
this._listBox1.Invoke(new
NetworkConnectionEstablishedHandler(UpdateListBox, new object[] {
sender }));
}
}

private delegate void
NetworkConnectionEstablishedHandler(NetworkConnection conn);

private void UpdateListBox(NetworkConnection conn)
{
... now add an item to the ListBox because of the new
connection established by NetworkConnection "conn" ...
}

.... I think ... :)

This assumes that the HandleNewConnection event handler is only ever
called as a result of an event raised by a NetworkConnection object.

I don't have much experience in this area, so if anyone else knows a
better way to do these, please chime in. (Jon?)
 

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

Similar Threads


Top