multi-threading C# .NET

Discussion in 'Software' started by mastermosley, Jan 3, 2008.

  1. mastermosley

    mastermosley Sergeant

    I've got a socket connection and Its set up to loop. I start off by

    Code:
    new Thread(Processing).Start();
    
    void Processing()
    {
            try
                {
                    lock (BigLock)
                    {
                        OnConnect();
                    }
                    while (true)
                    {
    
                    lock (BigLock)
                    {
                        foreach (Connection conn in connections)
                        {
                            conn.Writer.Flush();
                        }
                    }
    
                    string line = Reader.ReadLine();
                    if (line == null)
                    {
                        break;
                    }
                    lock (BigLock)
                    {
                        ProcessLine(line);
                    }
                }
                }
                catch (Exception)
                {
                    MessageBox.Show("ERROR:");
                    Form.ActiveForm.Dispose();
                    Form.ActiveForm.Close();
                }
    
                finally
                {
                    lock (BigLock)
                    {
                        socket.Close();
                        OnDisconnect();
                    }
                }
            }
    
    
    The Server doesnt start untill I click Start on the Form but once I click it, it freezes the form and when I click on it it stops responding and screws up the server. How can I have a thread for the Form and a thread for the loop going at the same time?
     
  2. mastermosley

    mastermosley Sergeant

    Thanks anyway but after a few hours of goofing around with System.Threading I figured it out.:D
     
  3. Wookie

    Wookie Sergeant Major

    Threading can be quite fun and is quite a huge topic, I still have to figure out how the thread pools work in the application I work with at work (150-200 concurrent threads running at one time, WOW!)
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds