在WinForms中使用backgroundworker处理带有事件的数据流

发布于 2024-10-18 03:56:08 字数 645 浏览 3 评论 0原文

我在 C# 中遇到一些麻烦。我对 C# 事件非常陌生,因此很难想象我的问题的全局解决方案。我应该做一个 WinForm 从 LabJackDriver 收集数据并绘制图表。我已经准备好了一个图形组件,所以我的问题是如何做基础知识。

我在这里搜索一些解决方案,发现这个。我正在尝试以这种方式解决一些问题。

您能给出一些如何启动的步骤吗?我必须实现以下接口:

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

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

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

提前致谢,

Pedro D

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦回梦里 2024-10-25 03:56:08

首先,您首先创建一个实现该接口的类,如下所示

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();
    }
}

将所有 throw new NotImplementedException(); 替换为相关代码。

这仍然与事件没有太大关系。我想您还需要知道如何触发事件。

使用 MyClass 时,当您需要通知用户数据已到达时,您可以像这样触发 SteamingDataArrive 事件:

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

或者无论您的委托签名是什么。

要侦听同一事件,您可以将事件订阅到处理程序

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

并创建处理程序,

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

您可能需要更改 StreamingDataEventArgs 以匹配您的委托签名。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文