如何避免从外部库调用每个事件?

发布于 2024-08-07 18:58:49 字数 691 浏览 1 评论 0原文

我编写了一个库,它可以使用我们的定制嵌入式硬件处理所有 TCP/IP 通信。它与我们的大多数内部软件一起使用,并且将来可能会单独出售。

最烦人的事情是,每次我处理库中的事件时,我都必须有一个单独的函数来调用。我只能想象有一种我不知道的更简单的方法来做到这一点......

有什么想法吗?

    public Setup(DiscoveryReader reader)
    {
        download = new DownloadFilesIndividual(Reader.ip, DateTime.Today);
        download.OnDownloadStatus += new DownloadStatusHandler(download_OnDownloadStatus);
    }

    void download_OnDownloadStatus(DownloadStatus status)
    {
        Invoke(new DownloadStatusHandler(this.safe_download_OnDownloadStatus), new object[] { status });
    }

    void safe_download_OnDownloadStatus(DownloadStatus status)
    {
        // Do UI stuff here
    }

I've written a library which handles all the TCP/IP comms with our custom embedded hardware. It is used with most of our in house software and in the future could possibly be sold separately.

The most annoying thing this is that every time I handle events from the library, I have to have a seperate function to invoke through. I can only imagine that there is an easier way to do this that I just dont know...

Any ideas?

    public Setup(DiscoveryReader reader)
    {
        download = new DownloadFilesIndividual(Reader.ip, DateTime.Today);
        download.OnDownloadStatus += new DownloadStatusHandler(download_OnDownloadStatus);
    }

    void download_OnDownloadStatus(DownloadStatus status)
    {
        Invoke(new DownloadStatusHandler(this.safe_download_OnDownloadStatus), new object[] { status });
    }

    void safe_download_OnDownloadStatus(DownloadStatus status)
    {
        // Do UI stuff here
    }

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

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

发布评论

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

评论(1

围归者 2024-08-14 18:58:49

句法糖

public Setup(DiscoveryReader reader)
{
    download = new DownloadFilesIndividual(Reader.ip, DateTime.Today);
    download.OnDownloadStatus += new DownloadStatusHandler(download_OnDownloadStatus);
}

void download_OnDownloadStatus(DownloadStatus status)
{
   if(InvokeRequired)
   {
    Invoke(new Action<DownloadStatus>(download_OnDownloadStatus),status);
   } else {
   // Do UI stuff here
   }
}

syntactical sugar

public Setup(DiscoveryReader reader)
{
    download = new DownloadFilesIndividual(Reader.ip, DateTime.Today);
    download.OnDownloadStatus += new DownloadStatusHandler(download_OnDownloadStatus);
}

void download_OnDownloadStatus(DownloadStatus status)
{
   if(InvokeRequired)
   {
    Invoke(new Action<DownloadStatus>(download_OnDownloadStatus),status);
   } else {
   // Do UI stuff here
   }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文