【问题标题】:main application catching event from a dll主应用程序从 dll 中捕获事件
【发布时间】:2014-01-27 21:47:24
【问题描述】:

这是我想用 C# 和 WPF 做的,但对如何设计和实现的想法有限:

  • 系统由一个dll/程序集(monitor.dll)和一个主应用程序(main.exe)组成。

  • monitor.dll 正在监视一个 txt 文件,该文件在其他宏运行时得到更新。当 txt 文件完成更新/完成写入后,monitor.dll 应该释放一个可以被 main.exe 检测到的信号。

  • 整个过程不必频繁。 monitor.dll可以每1/4秒检查一次txt文件,main.exe检查monitor.dll是否发出“done”信号的频率可以在同一顺序。

我想知道我应该如何设计系统?我应该使用什么技术?我应该在程序集中使用 BackGroundWorker 吗?

现在我在 monotor.dll 和 main.exe 中都使用了计时器,但似乎 monitor.dll 可以捕捉到事件,但 main.exe 总是无法捕捉到发送的信号由大会。谢谢。

【问题讨论】:

  • 在 DLL 中创建一个公共类,该类提供一个事件(每当文件更改时都会引发该事件)。在主应用程序中实例化这个类并订阅这个事件。考虑使用FileSystemWatcher 观看特定文件或文件夹。
  • 你必须使用那个 DLL 吗?也许您的主应用程序可以使用以下方法自行完成:msdn.microsoft.com/en-us/library/…
  • @FrancisDucharme 是的,我必须使用那个 dll,否则我想象它会容易得多。
  • @NicoSchertler 如何在 dll 中引发事件,以及如何在主应用程序中检测事件?一些示例代码?
  • @NicoSchertler 我的意思是,我想我可以在 dll 中捕获该事件,因为如果我在 dll 中放置一个断点,它将在 txt 文件完成写入时被命中。但是,如果我不在 dll 中设置断点,则主应用程序不会发生任何事情。

标签: c# .net wpf multithreading visual-studio


【解决方案1】:

在程序集中,我使用了一个计时器来定期检查 .txt 文件的状态,如下所示:

System.Timers.Timer _logFileCheckTimer;

    public FijiLauncherControl()

    {        // timer set up
                    _logFileCheckTimer = new System.Timers.Timer(250);  
                    _logFileCheckTimer.Enabled = true;
                    _logFileCheckTimer.Elapsed += new System.Timers.ElapsedEventHandler(_logFileCheckTimer_Elapsed);
                   _logFileCheckTimer.Start();  // start the timer

     }    

     void _logFileCheckTimer_Elapsed(object sender, EventArgs e)
        {
            if (_processOn && IsLogOn)
            {
                try
                {
                    _processFinished = CheckStatuts();  // checking file status

                    if (_processFinished) // fire event if checking status returns true
                    {
                        OnIjmFinished(EventArgs.Empty);
                        _processOn = false;
                        _logFileCheckTimer.Stop();
                    }
                }
                catch (Exception ex)
                {

                }
            }
        }

在大会上。在 dll 中设置一个事件,如果_processFinished = CheckStatuts(); 返回true,将触发该事件:

// 事件委托 公共委托无效 IjmFinishedEventHandler(对象发送者,EventArgs e); 公共事件 IjmFinishedEventHandler IjmFinished; // 事件处理程序

   protected virtual void OnIjmFinished(EventArgs e)
   {
      if (IjmFinished != null)   // trigger event
                IjmFinished(this, e);          
   }

在主应用程序中,应该有事件接收器,如下所示:

private FijiLauncherControl _fl; // the object of your assembly

  _fl.IjmFinished += new IjmFinishedEventHandler(_fl_IjmFinished); // event from the assembly should trigger an event handler

   void _fl_IjmFinished(object sender, EventArgs e)   // event handler
        {
            _vm.IjmFinished = true;   // a boolean var in viewmodel in main app set to be true once the event from the assembly is triggered

            //throw new NotImplementedException();
        }

这应该可以完成工作。

【讨论】:

  • 我试图编辑这个,但这真的很难编辑。我无法使用菜单栏上的代码按钮使它看起来像代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-04-21
  • 2013-11-12
  • 2012-05-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多