8

I've written a simple async TCP-Server - it works well. But now I want to make an output of the received Data on the Console-Testprogram. And the problem is, this isn't working! If I connect the MainThread to the EventHandler the program does nothing. Debug is showing that the sc05Server_DataAvailable was called but then nothing happened. The program is still responsive.

Here The Code:

private void ReadCallback(IAsyncResult asyncResult)
{
    Sc05BdClient sc05BdClient = asyncResult.AsyncState as Sc05BdClient;
    if (sc05BdClient == null) return;
    NetworkStream networkStream = sc05BdClient.NetworkStream;
    int read = networkStream.EndRead(asyncResult);

    if (read == 0)
    {
        lock (clients)
        {
            clients.Remove(sc05BdClient);
            return;
        }
    }

    string data = Encoding.GetString(sc05BdClient.Buffer, 0, read);
    System.Diagnostics.Debug.Print(data);
    OnDataAvailable(this, new DataAvailableEventArgs(data));  <---- here Handler is called
    networkStream.BeginRead(sc05BdClient.Buffer, 0, sc05BdClient.Buffer.Length, ReadCallback, sc05BdClient);
}


public event EventHandler<DataAvailableEventArgs> DataAvailable;

protected virtual void OnDataAvailable(object sender, DataAvailableEventArgs e)
{
    EventHandler<DataAvailableEventArgs> handler = DataAvailable;
    if (handler != null)
        handler(sender, e);
}


public class DataAvailableEventArgs : EventArgs
{
    public string Data;

    public DataAvailableEventArgs(string data)
    {
        Data = data;
    }
}

The Main program:

class Program
{
    static void Main()
    {
        Sc05BdServer sc05BdServer = new Sc05BdServer(IPAddress.Any, 2006);
        sc05BdServer.DataAvailable += sc05BdServer_DataAvailable;
        sc05BdServer.Start();

        Console.ReadKey();
        sc05BdServer.Stop();
    }

    static void sc05BdServer_DataAvailable(object sender, DataAvailableEventArgs e)
    {
       Console.WriteLine(e.Data);  <--- this is called once
    }
}

I think it has something to do with Threading - but I have no idea how to work with them.

p.s.w.g
  • 146,324
  • 30
  • 291
  • 331
GreenEyedAndy
  • 1,485
  • 1
  • 14
  • 31
  • can you post the type of your Event? The event that is being hooked up inside Sc05BdServer. – d.moncada Mar 05 '13 at 19:19
  • Are you sure that it's not entering the `if` condition and returning? Have you placed a breakpoint in the handler to see if the number of bytes read is 0? – Jim Mischel Mar 05 '13 at 19:31
  • @JimMischel yes I'm sure the System.Diagnostics.Debug.Print is doing it's Output. – GreenEyedAndy Mar 05 '13 at 19:40
  • @moncadad I edited my question and add the Information you wanted. – GreenEyedAndy Mar 05 '13 at 19:50
  • 2
    I'm not sure but check this question posted few days ago, maybe you are experiencing the similar issue:http://stackoverflow.com/questions/15143931/strange-behaviour-of-console-readkey-with-multithreading#comment21320353_15143931 – Nikola Davidovic Mar 05 '13 at 20:11
  • @NikolaDavidovic I follow the link and you're right. That's the Problem. The call on Console.Readkey ist blocking the Console.Writeline. That's too easy to think of. – GreenEyedAndy Mar 05 '13 at 22:04
  • @GreenEyedAndy: `Console.Readline()` should work though – leppie Mar 06 '13 at 04:28
  • 1
    @NikolaDavidovic: You should post an answer (even though this question is kind of duplicated). :) – leppie Mar 06 '13 at 04:29

1 Answers1

3

You are probably experiencing some kind of race issue although Console should be immune to that. Check this question but note that I couldn't reproduce the problem: Strange behaviour of Console.ReadKey() with multithreading

Community
  • 1
  • 1
Nikola Davidovic
  • 8,556
  • 1
  • 27
  • 33