Should i use BackgroundWorker?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • watashi
    New Member
    • Jul 2007
    • 12

    #1

    Should i use BackgroundWorker?

    Hello ,
    I m writing a code realted to TCP/IP application. When the " client tries to connect to server and the server is not listening " condition, i m using recurcive function just to keep on trying till the server starts listening.
    The problem is, the form or screen become dead. Even though I wrote the statement to disable some textbox and buttons before starting the above mentioned recursive function, disable action is not occuring.

    private void btnActivate_Cli ck(object sender, EventArgs e)
    {

    //disable the textbox ,buttons etc...

    StartConnecting ( );

    }

    private void StartConnecting ( )
    {

    try
    {
    _clientSocket.C onnect(ipEndPoi nt);

    ............
    ..............
    ..............
    }
    catch
    {
    StartConnecting ( );
    }

    }


    what might be the problem.
    should i use backgroundworke r of threadpool .

    thanks in advance
  • Shashi Sadasivan
    Recognized Expert Top Contributor
    • Aug 2007
    • 1435

    #2
    Originally posted by watashi
    Hello ,
    I m writing a code realted to TCP/IP application. When the " client tries to connect to server and the server is not listening " condition, i m using recurcive function just to keep on trying till the server starts listening.
    The problem is, the form or screen become dead. Even though I wrote the statement to disable some textbox and buttons before starting the above mentioned recursive function, disable action is not occuring.




    what might be the problem.
    should i use backgroundworke r of threadpool .

    thanks in advance
    To start with put your code in between the [CODE] Tag

    Code:
     private void btnActivate_Click(object sender, EventArgs e)
     {
             
    //disable the textbox ,buttons etc...
    
    StartConnecting( );
    
    }
    
    private void StartConnecting( )
    {
    
       try
        {
          _clientSocket.Connect(ipEndPoint);
         
         ............
         ..............
    ..............
        }
       catch
       {
        StartConnecting( );
        }
    
    }
    When your button click occours it calls the StartConnecting () method
    on failure it calls it again
    putting it into an infinite loop (worst case scenario)

    what do you thick would happen if you included only the line below in your event handler
    Code:
    private void btnActivate_Click(object sender, EventArgs e)
     {
             
    //disable the textbox ,buttons etc...
    
    while(true){}
    
    }
    cheers

    Comment

    Working...