FileSystemWatcher在WinForms中起作用,但在WPF中不起作用 [英] FileSystemWatcher Works In WinForms but not in WPF

查看:148
本文介绍了FileSystemWatcher在WinForms中起作用,但在WPF中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨.

在阅读了许多文章(这里是好人的指导::)之后,我得以碰到FileSystemWatcher类,该类使我能够监视计算机中各个位置的更改.首先,我尝试使用Windows Forms,并且一切正常.假定FileSystemWatcher在默认情况下在与主线程不同的线程上运行,则Microsoft的工作人员会执行以下操作:

Hi.

After reading a number of articles (directed to by the good guys here :-)), i was able to come across FileSystemWatcher class that enabled me to monitor changes on various locations in my computer. To begin with, i tried on Windows Forms and it all worked all fine. Given that FileSystemWatcher, by default, runs on a separate thread from the Main Thread, the folks at Microsoft made this line:

watcher.SynchronizingObject = this;



允许FileSystemWatcher与主线程一起工作,从而解决了非法的跨线程错误.


在WPF中转换逻辑,同一行实际上是一个编译错误,内容为:



which allows the FileSystemWatcher to work alongside the Main Thread, thus solving the illegal cross thread error.


Tranfering the logic in WPF, the same line is actually a compile error saying:

Cannot implicitly convert type 'MyProject.MainWindow' to 'System.ComponentModel.ISynchronizeInvoke'. An explicit conversion exists (are you missing a cast?)



为什么会是这种情况,怎么办呢?

谢谢:-)



Why could this be the case and what could be the solution?

Thanks :-)

推荐答案

我建​​议您不要尝试将FileSystemWatcher直接绑定到UI.相反,当您需要基于FileSystemWatcher的事件更新UI时,可以使用 ^ ]方法来解决线程是否正确以更新UI.
I would suggest that you don''t try to tie the FileSystemWatcher to the UI directly. Instead when you need to update the UI based on the events of the FileSystemWatcher you can use Dispatcher.CheckAccess[^] method to resolve if the thread is correct to update the UI.


这篇关于FileSystemWatcher在WinForms中起作用,但在WPF中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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