代码之家  ›  专栏  ›  技术社区  ›  Tim

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

  •  0
  • Tim  · 技术社区  · 15 年前

    我已经编写了一个库,用我们定制的嵌入式硬件处理所有的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
        }
    
    1 回复  |  直到 15 年前
        1
  •  1
  •   Andrew Keith    15 年前

    语法糖

    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
       }
    }