C#:同时从文本文件写入/读取+ WPF [英] C#: Writing/Reading From Text File At Same Time + WPF
问题描述
您好,专家,
我正在使用WPF(Windows Presentation Foundation).新手:)
问题是我想从另一个程序同时写入的日志文件中连续读取并更新表单中的文本框.因此,一个程序正在连续写入,而另一个程序正在从同一文件连续读取.
我需要一些建议来实施它的可能解决方案.
我能想到的一种解决方案是启动另一个线程,该线程以只读模式打开文件.
是否可以将文本框绑定到文本文件?因此,它会自动更新.
任何建议都会很棒!
谢谢
BR
Hi experts,
I am working with WPF (Windows Presentation Foundation). Newbie :)
The problem is I want to CONTINUOUSLY read from a log file which is being written by an other program at same time and update the textbox in the form. So, one program is continuously writing and other is continuously reading from same file.
I need suggestions what are the possible solutions to implement it.
One solution I can think of is to start another thread which opens the file in readonly mode.
Is it possible to bind text box to text file? So, that it is updated automatically.
Any suggestions will be great!
Thanks
BR
推荐答案
让正在写入文件的程序以写互斥方式打开它,并让另一个程序在文件上运行FileSystemWatcher.当它检测到文件已被写入时,便可以从文件中读取该文件.
是的,将需要使用线程,以便您不会在阅读程序上锁定UI.
Have the program that''s writing to the file open it with write exclusive, and have the other program run a FileSystemWatcher on the file. When it detects that the file has been written to, it can then read from the file.
Yes, threading will be required so that you don''t lock up the UI on the reading program.
您可能需要看一下 ^ ],但是如果另一个应用程序对该文件具有排他锁,则可能会遇到困难.
You may want to look at this ReaderWriterLock[^] however the difficulty may come if the the other application has an exclusive lock on the file.
这篇关于C#:同时从文本文件写入/读取+ WPF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!