Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Console.Writeline from EventHandler

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.

like image 823
GreenEyedAndy Avatar asked Mar 05 '13 19:03

GreenEyedAndy


People also ask

What is C# EventHandler?

An event handler, in C#, is a method that contains the code that gets executed in response to a specific event that occurs in an application. Event handlers are used in graphical user interface (GUI) applications to handle events such as button clicks and menu selections, raised by controls in the user interface.

How do you raise an event in C#?

Typically, to raise an event, you add a method that is marked as protected and virtual (in C#) or Protected and Overridable (in Visual Basic). Name this method On EventName; for example, OnDataReceived .

Is an event handler a delegate?

The EventHandler delegate is a predefined delegate that specifically represents an event handler method for an event that does not generate data. If your event does generate data, you must use the generic EventHandler<TEventArgs> delegate class.

What is an advantage of having the main application class extend EventHandler?

The advantage of using EventHandler<TEventArgs> is that you do not need to code your own custom delegate if your event generates event data. You simply provide the type of the event data object as the generic parameter.


1 Answers

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

like image 97
Nikola Davidovic Avatar answered Oct 04 '22 15:10

Nikola Davidovic