1

我有以下代码:

namespace SSS.RemoteTruckService
{
  public partial class Startup : Form
  {
    private Timer _gpsTimer;
    private Timer _ppsTimer;
    private Timer _creditCardTimer;
    private Timer _iniTimer;

    public string Message
    {
      get { return richTextBox_Message.Text; }
      set
      {
        richTextBox_Message.Invoke((MethodInvoker)(() 
          => richTextBox_Message.Text = DateTime.Now + " " + 
          value + Environment.NewLine + richTextBox_Message.Text));
      }
    }

    public Startup()
    {
      InitializeComponent();
    }

    private void ButtonStartClick(object sender, EventArgs e)
    {
      StartRemoteTruck();
    }

    private void ButtonPauseClick(object sender, EventArgs e)
    {
      if (_gpsTimer.Enabled) _gpsTimer.Enabled = false;
      if (_ppsTimer.Enabled) _ppsTimer.Enabled = false;
      if (_creditCardTimer.Enabled) _creditCardTimer.Enabled = false;
      if (_iniTimer.Enabled) _iniTimer.Enabled = false;
      ProcessIniFile.StopProcess();
    }

    public void StartRemoteTruck()
    {
      Message = "RemoteTruck started.";

      if (Settings.GlobalSettings == null)
      {
        Message = "GlobalSettings was null or not loaded. Cannot continue.";
        Logging.Log("GlobalSettings was null or not loaded. Cannot continue.", "RemoteTruck", Apps.RemoteTruckService);
        Environment.Exit(0);
      }

      if (Settings.GlobalSettings.IniFileWatcherEnabled)
      {
        ProcessIniFile.StartProcess();        
      }

      CreateTimers();      
    }

ProcessIniFile.StartProcess()我有代码:

namespace SSS.RemoteTruckService.inifile
{
  public static class ProcessIniFile
  {
    private static DateTime _iniLastWriteTime;
    private static readonly string Inifile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows), "sss.ini");
    private static FileSystemWatcher _watcher;

    public static void StartProcess()
    {  
      ReadIniFile();
      SaveCurrentIniReadings();
      CreateIniFileWatcher();
    }

    public static void StopProcess()
    {
      if (_watcher != null)
      {
        _watcher.EnableRaisingEvents = false;
        _watcher = null;
      }
    }

    private static void CreateIniFileWatcher()
    {
       _watcher = new FileSystemWatcher
        {
          Path = Environment.GetFolderPath(Environment.SpecialFolder.Windows),
          NotifyFilter = NotifyFilters.LastWrite,
          Filter = "sss.ini"
        };
      _watcher.Changed += SssIniWatcherChanged;
      _watcher.EnableRaisingEvents = true;
    }

我想将文件观察程序的读取状态传回调用表单。

也许我想太多了,但是如果我想添加到Message主窗体上,我该怎么做呢?

4

2 回答 2

0

您可以为此使用事件。您的流程可以发送事件,您的表单可以处理它们。更多信息:http: //msdn.microsoft.com/en-us/library/awbftdfh.aspx

于 2013-07-02T22:34:01.013 回答
0

我喜欢使用的简单但不漂亮的方法是将表单的那部分也设为静态。例如,创建一个静态变量WriteMessage,并在您的 Form Load 或Startup()中,您可以设置它:

WriteMessage = (s) => Message = s;

当然这有一些问题,但这是完成它的快速方法。Dispatcher.invoke其中一个问题是,如果您不在 UI 线程上,则可能需要使用。

于 2013-07-03T03:02:23.960 回答