0

A part of this code will print the 'New Ip client found', I added a code(the commented part) where it will get the hostname. I want that hostname to be printed in a separate listbox. How should I do it? I tried to append it but it wont show in the listbox I provided in the [Design].

public void performConnect()
{
    while (true)
    {
        if (myList.Pending())
        {
            thrd = thrd + 1;
            tcpClient = myList.AcceptTcpClient();

            IPEndPoint ipEndPoint = (IPEndPoint)tcpClient.Client.RemoteEndPoint;
            string clientIP = ipEndPoint.Address.ToString();
            nStream[thrd] = tcpClient.GetStream();

            // IPHostEntry ipEntry = Dns.Resolve(clientIP);
            // string sClientHost = ipEntry.HostName;

            currentMsg = "\n New IP client found :" + clientIP;
            recieve[thrd].Start();

            //StringBuilder sb = new StringBuilder();                        
            // sb.Append(listBox1.Text);                        
            // sb.Append(sClientHost + " is online.");                       
            //listBox1.Text = sb.ToString();

            this.Invoke(new rcvData(addNotification));
            try
            {
                addToIPList(clientIP);

            }
            catch (InvalidOperationException exp)
            {
                Console.Error.WriteLine(exp.Message);
            }
            Thread.Sleep(1000);
        }
    }
}

Thank you.

2
  • What is the framework you are using? Is this WinForms, WPF, etc? Sep 1, 2015 at 16:28
  • windows form application and .Net Sep 1, 2015 at 16:30

3 Answers 3

1

Since you are referencing main thread from another thread, you can't just do it directly. Cross thread operation is not allowed to prevent deadlocks. Do as following to access your list which in the main thread from another thread.

lstYourListBox.BeginInvoke(new MethodInvoker(() => lstYourListView.items.Add(clientIP));
2
  • what is 'lstYourListBox' and 'lstYourListView' shall i put this in the current function or in the listbox1 function? Sep 1, 2015 at 17:42
  • 1
    lstYourListBox is the id of your list box.Oops typo detected, it should have been lstYourListBox not lstYourListView. Effect of working in ASP.NET haha. Put this code inside your addToIPList(clientIP) method where you must have added items to your listbox.
    – NZeta520
    Sep 1, 2015 at 18:00
0

I haven't used Windows Forms in a long time so take this for what it's worth. Try doing the following after you add the text to the listbox:

listBox1.Invalidate();
listBox1.Update();
5
  • so there is no problem in the append part? Sep 1, 2015 at 16:36
  • @user3406220 No, I don't think so. That looks OK to me. Sep 1, 2015 at 16:39
  • @user3406220 BTW, using StringBuilder here is not doing much for you since you just have one string to add to listBox1.Text. You could probably just do this: Sep 1, 2015 at 16:40
  • @user3406220 Oops, hit return. Try this instead: listBox1.Text += sClientHost; Sep 1, 2015 at 16:41
  • sb.Append(listBox1.Text); returns an error of 'Cross-thread operation not valid: Control 'listBox1' accessed from a thread other than the thread it was created on.' Sep 1, 2015 at 16:45
0

This is the code you need to add to the listbox, just replace the listbox1 with the name \ id of your listbox

Action action = new Action(() => { 
                listBox1.Items.Add(sClientHost); 
            });
this.Dispatcher.Invoke(action, DispatcherPriority.ApplicationIdle);
13
  • it won't recognize the clientIp (does not exist in current context) if i put it in the listbox1 ..and i need to get the hostname not the Ip Sep 1, 2015 at 16:50
  • yes, if i make it currentMsg = "\nHostname found :" + sClientHost; Sep 1, 2015 at 16:57
  • so you can get the hostname, and you want to put this into the listbox? am i understanding that correctly? Sep 1, 2015 at 16:58
  • If i have understood you correctly i have amended my answer to reflect your need with hostname Sep 1, 2015 at 16:59
  • yes i need that hostname to be place in another place which is supposed to be the listbox1. Sep 1, 2015 at 17:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.