Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在另一个线程运行WPF时更新textbox_C#_Wpf_Thread Safety - Fatal编程技术网

C# 在另一个线程运行WPF时更新textbox

C# 在另一个线程运行WPF时更新textbox,c#,wpf,thread-safety,C#,Wpf,Thread Safety,下面是我的应用程序的架构。我有一个Perl脚本,在单击按钮时使用下面的方法调用它 ProcessStartInfo psStartInfo = new ProcessStartInfo("perl.exe"); psStartInfo.Arguments = paramStr; psStartInfo.UseShellExecute = false; psStartInfo.RedirectStandardOutput = true

下面是我的应用程序的架构。我有一个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();
        }

    }
问题是:


对文本框的读写操作已成功完成,但我的应用程序占用了大量内存。如果禁用日志记录,内存使用将达到最佳状态。

生成的文件的大小是多少?这(将文件加载到内存中,与文本框中相同)可能会导致内存消耗更高。为什么要将原始日志文件复制到临时日志文件中,您可以打开原始日志文件进行只读,并通过仅从原始日志文件读取来更新文本框?可能您还可以使用FileSystemWatcher组件查看原始日志文件的更新并不断更新文本框…您可以尝试使用
file.ReadLines
(.NET 4)读取文件,然后用foreach循环替换
while
循环,若要将每一行添加到
StringBuilder
。请尝试使用(…){}语句将
TextReaderReadLogs=…
括在
中,以确保调用
IDisposable.Dispose()
,也可以使用TextBox.AppendText()而不是在Text属性上使用+=。这样,您就不会每次都将整个字符串写入文本框。您也不需要ScrollToEnd()命令。