Pedro Dusso
Pedro Dusso

Reputation: 2140

Using backgroundworker to handle data Streaming with Events in WinForms

I having some trouble in C#. I'm very new to C# events, so it's being difficult to me visualize the global solution to my problem. I'm supposed to do a WinForm to collect data from a LabJackDriver and graph it. I have a graph component ready, so my problem is how to do the basics.

I was searching were about some solutions here and I found this. I'm trying some solution in this way.

Can you give some steps in how to start it? I must implement the fallowing interface:

public interface IStream {
    event StreamingDataArriveEventDelegate StreamingDataArrive;
    event StreamingStateChangeEventDelegate StreamingStateChange;

    void addAnalogPort(int portId);
    ISensorSource getSensorSource(int portId);

    void Start();
    void Stop();
    void Close();
}

Thanks in advance,

Pedro D

Upvotes: 0

Views: 486

Answers (1)

Albin Sunnanbo
Albin Sunnanbo

Reputation: 47038

Well you start by creating a class that implements the interface, like this

public class MyClass : IStream
{
    public event StreamingDataArriveEventDelegate StreamingDataArrive;

    public event StreamingStateChangeEventDelegate StreamingStateChange;

    public void addAnalogPort(int portId)
    {
        throw new NotImplementedException();
    }

    public ISensorSource getSensorSource(int portId)
    {
        throw new NotImplementedException();
    }

    public void Start()
    {
        throw new NotImplementedException();
    }

    public void Stop()
    {
        throw new NotImplementedException();
    }

    public void Close()
    {
        throw new NotImplementedException();
    }
}

Replace all throw new NotImplementedException(); with relevant code.

This has still not much to do with events. I suppose you also need to know how to fire an event.

Withing your MyClass when you need to notify the user that data has arrived you fire the SteamingDataArrive event like this:

if (StreamingDataArrive != null)
    StreamingDataArrive(this, new StreamingDataEventArgs("data"));

or whatever your delegate signature is.

To listen to the same event you subscribe the event to a handler

MyClass myClass = new MyClass();
myClass.StreamingDataArrive += new StreamingDataArriveEventDelegate(myClass_StreamingDataArrive);

and create a handler

void myClass_StreamingDataArrive(object Sender, StreamingDataEventArgs eventArgs)
{
    throw new NotImplementedException();
}

you might need to change StreamingDataEventArgs to match your delegate signature.

Upvotes: 1

Related Questions