Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

What actually "label.visble = true" did?

Posted on 2004-10-13
3
Medium Priority
?
196 Views
Last Modified: 2010-04-15
The code below is extracted from one of my project, it will hang when there a client is connected. When I make some changes in the showConnected() function, the program work well. I would like to know why!!


//The code below is at the main page

private void btOpenPort_Click(object sender, System.EventArgs e)
{
      Connector = new TcpServer(Int32.Parse(tbPort.Text), this);
      Thread runSrv = new Thread(new ThreadStart(Connector.OpenPort));
      runSrv.Start();
}

public void showConnected()
{
                //lbConnected is a label
      lbConnected.visible = true;
}

//These the code run is another thread

public TcpServer(int port, Server theBoss)
{
      PortNo = port;
      boss = theBoss;
}

public void OpenPort()
{
      Listener = new TcpListener(PortNo);;
      Listener.Start();
                  
                  
      Socket ConnectionSocket;
      NetworkStream socketStream;
      BinaryReader reader;
      BinaryWriter writer;

      while(!programClosed)
      {
            ConnectionSocket = Listener.AcceptSocket();
                        
            boss.showConnected();
                        
            socketStream = new NetworkStream(ConnectionSocket);

            writer = new BinaryWriter(socketStream);
            reader = new BinaryReader(socketStream);
            ClientReader = reader;
      }
}


When I initially set the visible of lbConnected.visible = false and lbConnected.Text = "" and modified the showConnected function as below, the program work well, why?

public void showConnected()
{
      lbConnected.Text = "Client Connected !!";
}
0
Comment
Question by:Kahhoe
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 1

Accepted Solution

by:
CrazyIvan007 earned 500 total points
ID: 12306787
You are accessing the UI Thread from another Thread. That can makes problem. You should ever use Delegates to access the UI Thread from within Threads.


public delegate void DelegateSetLabelVisible(string strControlName, bool boolVisible);

public class YourClass
{
     public DelegateSetLabelVisible m_DelegateSetLabelVisible;
     ...

     public YourClass()
     {
           m_DelegateSetLabelVisible = new DelegateSetLabelVisible(this.DelegateSetLabelVisible);
     }


     private void DelegateSetLabelVisible(string strControlName, bool boolVisible)
     {
      Control ctrl = GetControlAtForm(this, strControlName);

      if (ctrl != null)
      {
            Object objCtrl = ctrl;
            if (objCtrl.GetType().ToString() == "System.Windows.Forms.Label")
            {
                  Label _label = (Label)objCtrl;
                  _label.Visible = boolVisible;
            }
      }
     }

    private Control GetControlAtForm(Control ctrl, string strControlName)
   {
      Control ListViewExCtrl = null;

      foreach (Control _ctrl in ctrl.Controls)
      {
            if (_ctrl.Name == strControlName)
            {
                  return(_ctrl);
            }
            if (_ctrl.HasChildren)
            {
                  Control ctrl_Temp = GetControlAtForm(_ctrl, strControlName);
                  if (ctrl_Temp != null)      { ListViewExCtrl = ctrl_Temp; break; }
            }
      }

                return (ListViewExCtrl);
     }

}




then use

BeginInvoke(m_DelegateSetLabelVisible, new object[] { "your_label", true });

to Invoke the LabelChange from the Worker Thread to the UI Thread
0
 
LVL 1

Author Comment

by:Kahhoe
ID: 12315557
Thank you for this very important concept... The clue is the BeginInvoke method!! This also helped me in solving the problem while I need to add a control in another thread. For example need to add a new row to a grid..
0
 
LVL 1

Expert Comment

by:CrazyIvan007
ID: 12317042
As i said you should use Delegates with Invoke/BeginInvoke if you access the UI Thread from other Threads. That means every Control you wan't to modify/change/add should be Invoked. Without that it can run, but it can throw Exceptions. So its safer to use it.
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question