#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    1
    Rep Power
    0

    display message to label


    I am working on a small TCP server project, I would like to display a progress message in a label on a form. The message displays too fast, and it doesn’t hold the last text message. I would like it to hold the last assigned message.

    Thanks

    public static void Main ()
    {
    Application.Run(new TCPHelloWorldServer());

    Label label1 = new Label();

    label1.Text = "initializing server...";
    TcpListener listener = new TcpListener ( 8080 );
    listener.Start ();

    label1.Text = "server initialized, waiting for incoming connections...";
    Socket s = listener.AcceptSocket ();

    label1.Text = ( "client connected, waiting for requests..." );
    // create a NetworkStream for easier access
    NetworkStream ns = new NetworkStream ( s );

    // use a stream reader because of ReadLine()-method
    StreamReader r = new StreamReader ( ns );

    bool loop = true;
    while ( loop )
    {
    // read a line until CRLF
    string command = r.ReadLine ();
    string result = null;


    label1.Text = ( "executing remote command: " + command );
    switch ( command )
    {
    case "GET":
    result = "Hello World !";
    break;

    // finish communication
    case "EXIT":
    result = "BYE";
    loop = false;
    break;

    // invalid command
    default:
    result = "ERROR";
    break;
    }

    if ( result != null )
    {

    label1.Text = ( "sending result: " + result );
    // add a CRLF to the result
    result += "\r\n";

    // convert data string to byte array
    Byte[] res = System.Text.Encoding.ASCII.GetBytes ( result.ToCharArray () );

    // send result to the client
    s.Send ( res, res.Length, 0 );
    }
    }


    label1.Text = ( "clearing up server..." );
    s.Close ();


    label1.Text = ( "press return to exit" );


    }
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    109
    Rep Power
    11
    I'm not to sure what you want to do, but assuming you want to pause after displaying the message for half a second you could try:

    System.Threading.Thread.Sleep(500);

    Right after you change the label, you may have to refresh the label too.

IMN logo majestic logo threadwatch logo seochat tools logo