Object exclusion
Ok guys, this unexpected problem occurs in my code that didn't show up before.
public void StartUdpListener(Object state)
{
/* sock1 = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
sock1.Bind(receiveEndPoint);
EndPoint ep = (EndPoint)receiveEndPoint;*/
recv = sock1.ReceiveFrom(receivedNotification, ref ep);
notificationReceived = Encoding.ASCII.GetString(receivedNotification, 0, recv);
//sock1.Close();
if (listBox1.InvokeRequired)
{
this.Invoke((MethodInvoker)delegate { listBox = new StringBuilder(this.listBox1.Text); });
}
listBox.AppendLine(notificationReceived);
if (listBox1.InvokeRequired)
{
pos = listBox1.FindString(notificationReceived);
if (pos >= 0)
{
}
else
{
this.Invoke((MethodInvoker)delegate { this.listBox1.Items.Add(listBox.ToString()); });
}
}
}
I am getting ObjectDisposedException saying the line:
this.Invoke((MethodInvoker)delegate { listBox = new StringBuilder(this.listBox1.Text); });
cannot be executed because listBox1 has been removed. How is this possible and is there something to be done?
a source to share
I am making the following assumptions:
- This code is a method in the form (System.Windows.Forms.Form).
- The variable 'listBox1' is a ListBox control on a form.
- You receive an ObjectDisposedException when the form is closed.
- You are using this method in a separate thread (not shown in the code, but implied by the question).
I would assume your code is blocking the receiveFrom () call on the socket when the form is closed. The next message coming from the network calls returnFrom to return, after which you post the message to a list that no longer exists. The first time you access this list, it is the line of code "this.listBox1.Text" when you create the StringBuilder, which is the string that throws an ObjectDisposeException. The ListBox is an object that is probably located, although it could also be a Form at this stage based on how quickly the messages appear.
It seems like a lot is needed, but I'm not sure what the correct advice is. I would first confirm my assumptions 1-4 above, and then consider refactoring your application so that it doesn't use multiple threads. I am making this suggestion because I have to assume that this is not the only threading problem your application might have. I could definitely be wrong in this assumption, in which case you can ignore the answer.
If I restrict the “what to do” part of the question to a more limited area, then I would suggest that you properly turn off your UDP receiver before allowing the window to close, again assuming my assumptions are correct.
a source to share
Comment on this block:
if (listBox1.InvokeRequired)
{
this.Invoke((MethodInvoker)delegate { listBox = new
StringBuilder(this.listBox1.Text); });
}
listBox.AppendLine(notificationReceived);
StringBuilder (listbox) can be null at the point you do .AppendLine. This is because you are creating the list on another thread where you are using it. A new StringBuilder object is also created if this code runs on a non-interface thread (that is, that listBox1.InvokeRequired) checks.
a source to share