如何监控在一个文本框和文本文件持续输出内容 [英] How to monitor Textfile and continuously output content in a textbox

查看:151
本文介绍了如何监控在一个文本框和文本文件持续输出内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在控制游戏服务器的程序。其中一个我做的功能是一个活生生的服务器日志文件监测器。

I'm making a program that controls a game server. One of the functions I'm making is a live server logfile monitor.

有一个日志文件(一个简单的文本文件),获取服务器的运行更新。

There is a logfile (a simple textfile) that gets updated by the server as it runs.

我如何不断地检查日志文件和输出它在一个RichTextBox内容?

How do I continuously check the logfile and output it's content in a RichTextBox?

我做这个简单的功能只是尝试并获得在日志的内容。
这当然会只是按行输出它获取文本行我的文本框。
也将锁定程序,只要循环运行,所以我知道了也没用。

I did this simple function just try and get the content of the log. It will of course just get the text row by row and output it to my textbox. Also it will lock the program for as long as the loop runs, so I know it's useless.

public void ReadLog()
{
  using (StreamReader reader = new StreamReader("server.log"))
  {
    String line;

    // Read and display lines from the file until the end of the file is reached.
    while ((line = reader.ReadLine()) != null)
    {
      monitorTextBox.AppendText(line + "\n");
      CursorDown();
    }
  }
}



但是,你将如何去解决了实时监控尽可能简单?

But how would you go about solving the live monitoring as simple as possible?

*编辑*

我M使用Prescots解决方案。伟大的东西。

I'm using Prescots solution. great stuff.

目前我使用的是sstreamreader从文件中的文本放到我的文本框。我碰到的是,每当我试图访问我的事件处理程序的任何GUI控件的程序刚刚停止,没有错误或警告的问题。

At the moment I'm using a sstreamreader to put the text from the file to my textbox. I ran into the problem is that, whenever I tried to access any of the gui controls in my event handler the program just stopped with no error or warnings.

我发现它有做穿线。我解决了,像这样:

I found out that it has to do with threading. I solved that like this:

private void OnChanged(object source, FileSystemEventArgs e)
{
    if (monitorTextField.InvokeRequired)
    {
        monitorTextField.Invoke((MethodInvoker)delegate { OnChanged(source, e); });
    }
    else
    {
      StreamReader reader = new StreamReader("file.txt");

      monitorTextField.Text = "";
      monitorTextField.Text = reader.ReadToEnd();
      reader.Close();
      CursorDown();
    }
}

现在我唯一的问题是,file.txt的是所使用的服务器,所以我不能访问它,因为它的正由另一进程。我无法控制这个过程..所以也许我的运气。

Now my only problem is that the file.txt is used by the server so I can't access it, since it's "being used by another process". I can't control that process.. so maybe I'm out of luck.

不过,该文件可以在记事本中serevr运行时被打开,所以不知何故,必须是可能的。也许当它更新,我可以做文件的临时副本,并阅读该副本。不知道啊

But the file can be opened in notepad while the serevr is running, so somehow it must be possible. Perhaps I can do a temp copy of the file when it updates and read the copy. Dunno...

推荐答案

检查出的System.IO.FileSystemWatcher 类:

public static Watch() 
{
    var watch = new FileSystemWatcher();
    watch.Path = @"D:\tmp";
    watch.Filter = "file.txt";
    watch.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite; //more options
    watch.Changed += new FileSystemEventHandler(OnChanged);
    watch.EnableRaisingEvents = true;
}

/// Functions:
private static void OnChanged(object source, FileSystemEventArgs e)
{
    if(e.FullPath == @"D:\tmp\file.txt")
    {
        // do stuff
    }
}

编辑:如果你知道有关文件的一些细节,你可以处理最efficent方式得到最后一行。例如,也许当你阅读的文件,就可以消灭你读过什么,所以接下来它的更新的时候,你只要抓住什么是存在的输出。也许你知道的一行时添加,那么你的代码就可以立即跳转到该文件的最后一行。等等。

if you know some details about the file, you could handle the most efficent way to get the last line. For example, maybe when you read the file, you can wipe out what you've read, so next time it's updated, you just grab whatever is there and output. Perhaps you know one line is added at a time, then your code can immediately jump to the last line of the file. Etc.

这篇关于如何监控在一个文本框和文本文件持续输出内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆