当另一个线程运行 WPF 时更新文本框

发布于 2024-12-11 07:25:59 字数 1964 浏览 0 评论 0原文

这是我的应用程序的架构。我有一个 Perl 脚本,通过单击按钮使用下面的方法进行调用。

        ProcessStartInfo psStartInfo = new ProcessStartInfo("perl.exe");
        psStartInfo.Arguments = paramStr;
        psStartInfo.UseShellExecute = false;
        psStartInfo.RedirectStandardOutput = true;
        psStartInfo.RedirectStandardError = true;
        psStartInfo.CreateNoWindow = false;


        ps.StartInfo = psStartInfo;
        ps.Start();
        string os = ps.StandardOutput.ReadToEnd();

上面的代码执行成功。有一个文本文件是使用我在上述代码中触发的 Perl 脚本生成的。我必须读取该文件并在 WPF 文本框中显示其中的所有内容。该 Perl 文件每 5 到 10 秒更新一次。我使用下面提到的代码来读取文件并将其显示在我的文本框中。

        dispatcherTimer = new DispatcherTimer();
        dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
        dispatcherTimer.Interval = new TimeSpan(0, 0, 30);
        dispatcherTimer.Start();

     private void dispatcherTimer_Tick(object sender, EventArgs e)
    {
        txtExecLog.Text = "";

        if (File.Exists("C:\\FlashAuto\\Execution_Logs\\log.txt"))
        {
            File.Copy("C:\\FlashAuto\\Execution_Logs\\log.txt", "C:\\FlashAuto\\Temp\\log.txt", true);
            TextReader readLogs = new StreamReader("C:\\FlashAuto\\Temp\\log.txt");
            string line = readLogs.ReadLine();
            while (line != null)
            {
                txtExecLog.Text += "\n" + line;
                line = readLogs.ReadLine();
                txtExecLog.ScrollToEnd();
            }

            CountLines = txtExecLog.LineCount - 1;
            readLogs.Close();
            // Forcing the CommandManager to raise the RequerySuggested event
            txtExecLog.ScrollToEnd();
            CommandManager.InvalidateRequerySuggested();
            readLogs.Dispose();
        }
        else
        {
            txtExecLog.Text += "log file not found at: "+DateTime.Now.ToString();
        }

    }

问题是:

对文本框的读取和写入已成功完成,但我的应用程序占用了大量内存。如果我禁用日志记录,内存使用率是最佳的。

Here is the architecture of my app. I have a Perl script that is called using the method below on click of a button.

        ProcessStartInfo psStartInfo = new ProcessStartInfo("perl.exe");
        psStartInfo.Arguments = paramStr;
        psStartInfo.UseShellExecute = false;
        psStartInfo.RedirectStandardOutput = true;
        psStartInfo.RedirectStandardError = true;
        psStartInfo.CreateNoWindow = false;


        ps.StartInfo = psStartInfo;
        ps.Start();
        string os = ps.StandardOutput.ReadToEnd();

The above code executes successfully. There is a text file which is generated using the Perl script I fired in the above mentioned code. I have to read that file and show everything in that in my WPF textbox. That Perl file is updated after every 5 to 10 secs. I use the below mentioned code to read the file and show it in my textbox.

        dispatcherTimer = new DispatcherTimer();
        dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
        dispatcherTimer.Interval = new TimeSpan(0, 0, 30);
        dispatcherTimer.Start();

     private void dispatcherTimer_Tick(object sender, EventArgs e)
    {
        txtExecLog.Text = "";

        if (File.Exists("C:\\FlashAuto\\Execution_Logs\\log.txt"))
        {
            File.Copy("C:\\FlashAuto\\Execution_Logs\\log.txt", "C:\\FlashAuto\\Temp\\log.txt", true);
            TextReader readLogs = new StreamReader("C:\\FlashAuto\\Temp\\log.txt");
            string line = readLogs.ReadLine();
            while (line != null)
            {
                txtExecLog.Text += "\n" + line;
                line = readLogs.ReadLine();
                txtExecLog.ScrollToEnd();
            }

            CountLines = txtExecLog.LineCount - 1;
            readLogs.Close();
            // Forcing the CommandManager to raise the RequerySuggested event
            txtExecLog.ScrollToEnd();
            CommandManager.InvalidateRequerySuggested();
            readLogs.Dispose();
        }
        else
        {
            txtExecLog.Text += "log file not found at: "+DateTime.Now.ToString();
        }

    }

Here is the problem:

Reading and writing to the textbox is successfully done but there is a huge chunk of memory that is eaten up by my app. If I disable the logging, memory usage is optimal.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文