Multithreading

This is a discussion on Multithreading within the C# Programming forums, part of the General Programming Boards category; I'm trying to do a simple server with the TcpListener class, and I'm using multithreading to handle the accept()'ing. Of ...

  1. #1
    Just one more wrong move. -KEN-'s Avatar
    Join Date
    Aug 2001
    Posts
    3,230

    Multithreading

    I'm trying to do a simple server with the TcpListener class, and I'm using multithreading to handle the accept()'ing. Of course, that's sort of a hangover from C++ Winsock habits (I never could get a handle on asychronous sockets).

    Anyway, my code looks like this:

    Code:
    void startClick(object sender, System.EventArgs e)
    {
    	server.Start();
    			
    	Thread thread = new Thread(new ThreadStart(Listen));
    
    }
    
    public void Listen()
    {
    	Socket accept;
    	while(true)
    	{	accept = server.AcceptSocket();
    				
    	         listBox.Items.Add(accept.RemoteEndPoint);
    	 }
    }
    where startClick() is generated when the user clicks a button on the form to begin the server.

    For some reason the thread is never generated, and the Listen() function is never called.


    Also, is working with asynchronous sockets any easier in C# if so, maybe I should just do that...

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    Your program is fine, I'm having the exact same lines in some of mine... you are just missing one line:

    Code:
    void startClick(object sender, System.EventArgs e)
    {
    	server.Start();
    			
    	Thread thread = new Thread(new ThreadStart(Listen));
    
                    thread.Start(); // !!! ;-)
    }
    
    public void Listen()
    {
    	Socket accept;
    	while(true)
    	{
    	accept = server.AcceptSocket();
    				
    	         listBox.Items.Add(accept.RemoteEndPoint);
    	 }
    }
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  3. #3
    Just one more wrong move. -KEN-'s Avatar
    Join Date
    Aug 2001
    Posts
    3,230
    Talk about a "duh" moment. Nvoigt...don't take this the wrong way...but I think I love you .

  4. #4
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    ::Joke so easy that it's omitted::

  5. #5
    Registered User CompiledMonkey's Avatar
    Join Date
    Feb 2002
    Location
    Richmond, VA
    Posts
    438
    Hey man, everybody has those moments. Only god knows how many times I've done stupid $$$$ like that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multithreading (flag stopping a thread, ring buffer) volatile
    By ShwangShwing in forum C Programming
    Replies: 3
    Last Post: 05-19-2009, 08:27 AM
  2. multithreading in C++
    By manzoor in forum C++ Programming
    Replies: 19
    Last Post: 11-28-2008, 12:20 PM
  3. Question on Multithreading
    By ronan_40060 in forum C Programming
    Replies: 1
    Last Post: 08-23-2006, 08:58 AM
  4. Client/Server and Multithreading
    By osal in forum Windows Programming
    Replies: 2
    Last Post: 07-17-2004, 04:53 AM
  5. Multithreading
    By JaWiB in forum Game Programming
    Replies: 7
    Last Post: 08-24-2003, 10:28 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21