Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 1 of 1
  1. #1
    New Coder
    Join Date
    Sep 2011
    Thanked 1 Time in 1 Post

    Threading paradox in C#

    Hey guys!
    I am working on a little program for handling TCP connections, and as you may or may not know, execution of later code is blocked while the program is waiting for a connection to accept (TcpClient.TcpClientAccept()).
    I tried to do a little work around, which I assume is also the best way to run listening servers, by starting a seperate thread... However Visual C# denies to compile the code, since listenThread is instantiated in a function that isn't necessarily called when listenThread.Abort() is called.
    Well, what the heck, here's my code. btnListen and btnStop are the start and stop buttons obviously.
    private void btnListen_Click (object sender, EventArgs e) {
                btnListen.Enabled = false;
                btnStop.Enabled = true;
                Thread listenThread = new Thread ( new ThreadStart(Server.winMain.Listen) );
            private void btnStop_Click ( object sender, EventArgs e ) {
                btnListen.Enabled = true;
                btnStop.Enabled = false;
                listenThread.Abort ();
                serverSocket.Stop ();
    Visual C# 2010 underlines listenThread.Abort(); with the error "listenThread does not exist in the current context, which makes sense to me. But how do I get around this?

    EDIT: Also, even though really offtopic, what does [somethingsomething] mean? Is it labels or what? I have no idea how to search for this...
    Last edited by Time Sheep; 09-01-2012 at 10:02 PM.
    Need help?
    I work with the following languages:
    HTML: Professional - PHP: Professional - Visual C#: Average - CSS: Average - ASP: Please don't hurt me! ;(
    Currently practicing: Visual C#


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts