C# Win Forms GUI locks up with Asynch Socket Callback / WinForms.Delegate

D

Dave

I wrote a simple Asynchronous Socket client that receives OnConnect
notifications and attempts to report this back to the win forms app.
as the notification method is on another thread i'm using
winforms.Invoke or BeginInvoke but the GUI locks up - seemingly just
after entering ConnectResultInvoked().

i thought this was to be pretty straightforward, but i'm stumped so
far. does anyone know what could be the problem??

here's a skeleton of the code:

public partial class WLClientInfoForm : Form
{
public delegate void ConnectResultDelegate(string IPAddr, int
PortNum, string ConnectionResult);
public ConnectResultDelegate ConnResultDelegate;
AsynchClient _SockClient;

public WLClientInfoForm()
{
ConnResultDelegate = new
ConnectResultDelegate(ConnectResultInvoked);
}

public void ConnectResultInvoked(string IPAddr, int PortNum,
string ConnectionResult)
{
ListViewItem TSItem =
ConnectResultsListView.Items.Add(IPAddr);
TSItem.SubItems.Add(PortNum.ToString());
TSItem.SubItems.Add(ConnectionResult);
}

private void StartButton_Click(object sender, EventArgs e)
{
_SockClient = new AsynchClient(this);
_SockClient.AttemptConnection(IPAddressTextBox.Text,
PortNumberTextBox.Text);
}
.....
}

class AsynchClient
{
WLClientInfoForm _ParentFormObj;

public AsynchClient(WLClientInfoForm ParentFormObj)
{
_ParentFormObj = ParentFormObj;
}

public bool AttemptConnection(string IPAddr, string PortNum)
{
....
client.BeginConnect(remoteEP,
new
AsyncCallback(ConnectCallback), client);
.....
}

private void ConnectCallback(IAsyncResult ar)
{
...
client.EndConnect(ar);

_ParentFormObj.Invoke(_ParentFormObj.ConnResultDelegate,
new Object[] { _IPAddr _PortNum,
"Connected Successfully!" } );
OR

_ParentFormObj.BeginInvoke(_ParentFormObj.ConnResultDelegate,
new Object[] { _IPAddr,
_PortNum, "Connected Successfully!" } );

}
}

i omitted exception handling and other to include to be as brief as
possible.

thanks so much, dave
 
P

Peter Duniho

Dave said:
[...]
i omitted exception handling and other to include to be as brief as
possible.

You omitted so much code, it's impossible to say for sure what's going on.

However, your symptom describes a classic deadlock situation, so I
suspect that your AttemptConnection method or the StartButton_Click
method has some sort of "wait for completed connection" logic in it.
This prevents the main UI thread from ever being in a position to
service the invoked delegate.

Normally, using BeginInvoke would work around an issue like this, which
suggests you also have something waiting on the BeginInvoke() somehow.
Again, lacking complete code, it's hard to say.

Pete
 
D

Dave

Dave said:
[...]
i omitted exception handling and other to include to be as brief as
possible.

You omitted so much code, it's impossible to say for sure what's going on.

However, your symptom describes a classic deadlock situation, so I
suspect that your AttemptConnection method or the StartButton_Click
method has some sort of "wait for completed connection" logic in it.
This prevents the main UI thread from ever being in a position to
service the invoked delegate.

Normally, using BeginInvoke would work around an issue like this, which
suggests you also have something waiting on the BeginInvoke() somehow.
Again, lacking complete code, it's hard to say.

Pete

hmm you're right on both fronts. no nailed my issue and i did omit
too much code.

good job and thanks, dave
 
D

Dave

Dave said:
[...]
i omitted exception handling and other to include to be as brief as
possible.
You omitted so much code, it's impossible to say for sure what's going on.
However, your symptom describes a classic deadlock situation, so I
suspect that your AttemptConnection method or the StartButton_Click
method has some sort of "wait for completed connection" logic in it.
This prevents the main UI thread from ever being in a position to
service the invoked delegate.
Normally, using BeginInvoke would work around an issue like this, which
suggests you also have something waiting on the BeginInvoke() somehow.
Again, lacking complete code, it's hard to say.

hmm you're right on both fronts. no nailed my issue and i did omit
too much code.

good job and thanks, dave- Hide quoted text -

- Show quoted text -

good grief i typed to fast there. that should have been "you nailed
my issue and i did omit too much code".
 

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