FileSystemWatcher与Windows FORM的通信 [英] FileSystemWatcher communication to Windows FORM

查看:54
本文介绍了FileSystemWatcher与Windows FORM的通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我正在使用FileSystemWatcher来监控目录。我希望FSW

弹出我已经实例化但不可见的形式。


然而,我遇到了一些问题。


1)在FSW.Changed FileSystemEventhandler中,我在我的表单上调用了Show方法

''frmMain.Show()'',表单出现但不完全

画了它似乎挂着沙漏。


private void OnCreated()

{

frmMain.Show();


//更新数据源,附加到DataTable


}

2)一旦我的表格完全涂漆而不是悬挂,我希望FSW

继续与现在可见的表格进行通信

有关传入的文件。请参阅表格,我有一个

DataGridView,它的数据源是一个数据表,在上面的OnChanged事件中由FSW附加




我有2个主要类:FORM和MASTER。 FORM类是self

解释。 MASTER类继承自ApplicationContext

类。这是FSW所在的位置。我这样做只在FSW检测到新文件时显示表格



任何人有任何想法或能够对这些有所了解

问题?


非常感谢任何帮助。


public static void Main()

{

Application.Run(新MASTER());

}


公共类MASTER:ApplicationContext

{

私人System.IO.FileSystemWatcher;

...

}

公共类FORM:表格

{

public System.Data.DataTable dt;

...

}

Hi all,

I''m using a FileSystemWatcher to monitor a directory. I want the FSW
to pop up my already instantiated but invisible form.

However, I''m running into some problems with this.

1) In the FSW.Changed FileSystemEventhandler, I called the Show method
''frmMain.Show( )'' on my form, the form appears but not completely
painted and it appears to be hanging with the hourglass.

private void OnCreated( )
{
frmMain.Show( );

//update the datasource, append to DataTable

}

2) Once I have my form fully painted and not hanging, I want the FSW
to continue communicating with the now visible form relaying
information about the incoming files. See, on the form I have a
DataGridView that has as its datasource a Datatable that is appended
to by the FSW in the OnChanged event above.

I have 2 main classes: FORM and MASTER. the FORM class is self
explanatory. The MASTER class inherits from the ApplicationContext
class. This is where the FSW is located. I do this to show the form
only when new files are detected by the FSW.
Anyone have any ideas or am able to shed some light on these
questions?

Any help is much appreciated.


public static void Main( )
{
Application.Run(new MASTER( ) );
}

public class MASTER : ApplicationContext
{
private System.IO.FileSystemWatcher;
...
}

public class FORM : Form
{
public System.Data.DataTable dt;
...
}

推荐答案

2007年6月2日星期六00:08:42 -0700,Angelo< an ** *****@gmail.com写道:
On Sat, 02 Jun 2007 00:08:42 -0700, Angelo <an*******@gmail.comwrote:

[...]

1)在FSW.Changed FileSystemEventhandler中,我调用了在我的表单上显示方法

''frmMain.Show()'',表单出现但不完全

paint ed和它似乎挂着沙漏。


[...]

2)一旦我的表格完全涂漆而不挂,我希望FSW

继续与现在可见的表格进行通信

有关传入文件的信息。请参阅表格,我有一个

DataGridView,它的数据源是一个数据表,在上面的OnChanged事件中由FSW附加




[...]

任何人有任何想法或能够阐明这些

问题吗?
[...]
1) In the FSW.Changed FileSystemEventhandler, I called the Show method
''frmMain.Show( )'' on my form, the form appears but not completely
painted and it appears to be hanging with the hourglass.

[...]
2) Once I have my form fully painted and not hanging, I want the FSW
to continue communicating with the now visible form relaying
information about the incoming files. See, on the form I have a
DataGridView that has as its datasource a Datatable that is appended
to by the FSW in the OnChanged event above.

[...]
Anyone have any ideas or am able to shed some light on these
questions?



在我看来,主要问题是当引发FileSystemWatcher

事件时,事件处理程序在另一个线程上执行
来自主UI线程的
。你应该能够通过

来解决这个问题,使用Form.Invoke()在主UI线程上执行Show()方法

而不是直接调用Show() 。


同样,我不认为有这个事件会有任何问题

处理程序更新表单上的DataGridView,而不是事实

你也需要确保使用Form.Invoke()来执行任何

代码实际更新DataGridView。


Pete

It seems to me that the main issue is that when the FileSystemWatcher
events are raised, the event handler is executed on a different thread
from your main UI thread. You should be able to address this problem by
using Form.Invoke() to execute the Show() method on the main UI thread
rather than calling Show() directly.

Likewise, I don''t think there should be any problem having the event
handler update the DataGridView on the form, other than the fact that
there too you need to make sure you use Form.Invoke() to execute whatever
code will actually update the DataGridView.

Pete




如果你想要FSW与目录进行通信。你必须打开

形成不同的线程..你可以使用backgroundworker组件来完成这个任务,或者你可以对system.threading进行一些搜索

命名空间..

你必须做多线程应用程序,你要注意控件的

访问修饰符。例如,你必须写另一种形式''控制'的文本。这个控件'的访问修饰符必须是

是公共的...我希望这有助于你。


if you want FSW to communicate with the directory.you have to open the
form in different thread.. you can use backgroundworker component for
this task or you can make some search about system.threading
namespace..
you must do multi-thread application and u make attention to the
access modifiers of the controls. for example you have to write
another form''s control''s text.this control''s access modifier have to
be public..i hope this helps you.


2007年6月2日星期六03:13:34 -0700,Hakan Fatih YILDIRIM

< hf * ********@gmail.com写道:
On Sat, 02 Jun 2007 03:13:34 -0700, Hakan Fatih YILDIRIM
<hf*********@gmail.comwrote:

如果你想要FSW与目录进行通信。你必须打开

形式在不同的线程..你可以使用backgroundworker组件来完成这个任务或者你可以搜索一下system.threading

命名空间..
if you want FSW to communicate with the directory.you have to open the
form in different thread.. you can use backgroundworker component for
this task or you can make some search about system.threading
namespace..



我不知道这将如何解决任何问题。问题是FSW

在一个不同于表格'的消息泵

的线程上引发事件。 FSW不知道表格应该在哪个线程上执行,

所以你把表格放在哪个线程上并不重要,FSW

无法在表单的主题上引发事件。

I don''t see how that will solve anything. The issue is that the FSW
raises the event on a thread different from that the form''s message pump
is on. The FSW doesn''t know which thread the form should be executing on,
and so it really doesn''t matter which thread you put the form on, the FSW
isn''t going to be able to raise the event on the form''s thread.


你必须做多线程应用程序并且你要注意

访问控件的修饰符。
you must do multi-thread application and u make attention to the
access modifiers of the controls.



我也看不出哪些访问修饰符与多线程

代码有关。这是我的断言,无论如何都没有必要创建一个新的线程

,但即使你这样做,表单'''''的控件上的访问修饰符也是一个

完全独立的问题。

I also don''t see what access modifiers have to do with multi-threaded
code. It''s my assertion that there''s no need to create a new thread
anyway, but even if you did, access modifiers on the form''s controls are a
completely separate issue.


例如你必须写另一种形式''控制'的文本
这个控件'的访问修饰符必须是公开的...... b希望这会对你有所帮助。
for example you have to write
another form''s control''s text.this control''s access modifier have to
be public..i hope this helps you.



这是真的,如果你有一个特定表格之外的代码,你想要直接访问那个形式''s control ,你必须更改该控件的访问权限

修饰符,以便它是公开的。但是你必须这样做

代码是在与表单相同的线程上执行还是

不同的代码。


此外,我建议最好不要在一般情况下在

a表格中公开控件,而是提供特定的公共方法

在表格中提供封装访问控件。这样,只有

表单本身可以访问控件,并且不必担心某些外部代码以不合需要的方式搞乱控件。


Pete

It''s true, if you have code outside a particular form that you want to
access that form''s control directly, you have to change the access
modifier for that control so that it''s public. But you have to do that
whether the code is executing on the same thread as the form or a
different one.

Furthermore, I would suggest that it is better to not expose a control on
a form in the general case, and rather to provide specific public methods
in the form to provide encapsulated access to the control. That way, only
the form itself has access to the control and doesn''t have to worry about
some external code messing with the control in an undesirable way.

Pete


这篇关于FileSystemWatcher与Windows FORM的通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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