473,804 Members | 2,008 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

FileSystemWatch er communication to Windows FORM

Hi all,

I'm using a FileSystemWatch er 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 FileSystemEvent handler, 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 ApplicationCont ext
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 : ApplicationCont ext
{
private System.IO.FileS ystemWatcher;
...
}

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

Jun 2 '07 #1
15 4776
On Sat, 02 Jun 2007 00:08:42 -0700, Angelo <an*******@gmai l.comwrote:
[...]
1) In the FSW.Changed FileSystemEvent handler, 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?
It seems to me that the main issue is that when the FileSystemWatch er
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
Jun 2 '07 #2
hi,
if you want FSW to communicate with the directory.you have to open the
form in different thread.. you can use backgroundworke r component for
this task or you can make some search about system.threadin g
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.

Jun 2 '07 #3
On Sat, 02 Jun 2007 03:13:34 -0700, Hakan Fatih YILDIRIM
<hf*********@gm ail.comwrote:
if you want FSW to communicate with the directory.you have to open the
form in different thread.. you can use backgroundworke r component for
this task or you can make some search about system.threadin g
namespace..
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.
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.
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
Jun 2 '07 #4
Hi,

You have to synchronize the event calling thread with the form's
thread (message pump thread). You can use the form's Invoke member
method to do so.

for instance:

....
void OnEvent()
{
if( frm.InvokeReuir ed )
frm.Invoke(new MethodInvoker(O nEvent));
else
frm.Text = "Event called...";
}
....

Hope this helps.
Moty

Jun 2 '07 #5
On Jun 2, 1:40 pm, Moty Michaely <Moty...@gmail. comwrote:
Hi,

You have to synchronize the event calling thread with the form's
thread (message pump thread). You can use the form's Invoke member
method to do so.

for instance:

...
void OnEvent()
{
if( frm.InvokeReuir ed )
frm.Invoke(new MethodInvoker(O nEvent));
else
frm.Text = "Event called...";}

...

Hope this helps.
Moty
Oops,

Just saw that Pete answered. Sorry.

Moty.

Jun 2 '07 #6
Guys,

Thanks for all your responses so far.

I've tried several things unsuccessfully.

1) Peter and Moty, I've tried form.Invoke( ) but I get an exception
that

"{"Invoke or BeginInvoke cannot be called on a control until the
window handle has been created."}"

The form has already been instantiated FORM frm = new FORM( ); and
in the constructor I call the ForceCreate( ) method.

Seems like a catch 22, with the form having to be already visible to
invoke the visible method.

Any ideas what's going on here?

2) I've also tried using a delegate to call the show method but still
I get the Exception above.

<CODE>
public delegate void delVoid( );

public class FORM
{
private System.IO.FileS ystemWatcher FSW;

public FORM ( )
{
FSW = new FileSystemWatch er( ) ;

//FSW definition here
...
FSW.Created += new FileSystemEvent Handler
( FSW_Created ) ;
FSW.EnableRaisi ngEvents = true;
}

private void FSW_Created( )
{
delVoid deleg = new delVoid( this.ShowForm ) ;
deleg.Invoke( ) ;
//have also tried deleg.BeginInvo ke( null, null ) ;
}

private void ShowForm( )
{
this.Show( ) ;
}
}
</CODE>

On Jun 2, 2:08 am, Angelo <angelo...@gmai l.comwrote:
Hi all,

I'm using a FileSystemWatch er 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 FileSystemEvent handler, 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 ApplicationCont ext
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 : ApplicationCont ext
{
private System.IO.FileS ystemWatcher;
...

}

public class FORM : Form
{
public System.Data.Dat aTable dt;
...

}- Hide quoted text -

- Show quoted text -

Jun 2 '07 #7
***** CORRECTION TO THE MY LAST POST *****

I used the frm.CreateContr ol( ) method, not frm.ForceCreate ( ) that I
mentioned.

<CODE>

private void FSW_Created( )
{
this.Invoke( new MethodInvoker( this.ShowForm ) );
}

private void ShowForm( )
{
this.Show( ) ;
}

</CODE>
Jun 2 '07 #8
On Sat, 02 Jun 2007 10:12:12 -0700, Angelo <an*******@gmai l.comwrote:
1) Peter and Moty, I've tried form.Invoke( ) but I get an exception
that

"{"Invoke or BeginInvoke cannot be called on a control until the
window handle has been created."}"

The form has already been instantiated FORM frm = new FORM( ); and
in the constructor I call the ForceCreate( ) method.

Seems like a catch 22, with the form having to be already visible to
invoke the visible method.

Any ideas what's going on here?
No. Well, yes, but I admit I don't really understand why the form's
underlying window handle hasn't been created yet. I can't find any
mention of a ForceCreate() method, so I can't comment on what you are
doing there. It does seem like there should be a way to force the form to
be created fully upon instantiation, but "should be" and "is" are
sometimes a long way apart. :)

You may find that you have to use Invoke() on the main form instead of the
status form. The main form would then optionally call Show() on the
status form, as well as displaying whatever data you want shown. I
suppose one might consider this a better design anyway, since it shifts
management of the status form to the main form where the management of
your UI should be anyway.

For example (warning: uncompiled, untested code...for illustration
purposes only :) ):

class MainForm
{
private StatusForm _status;
private FileSystemWatch er _fsw;

void InitFSW()
{
_fsw = new FileSystemWatch er();

_fsw.Created += FSW_CreatedInvo ke;
}

void FSW_CreatedInvo ke(object sender, FileSystemEvent Args fsea)
{
Invoke(new FileSystemEvent Handler(FSW_Cre ated), new object[]
{sender, fsea});
}

void FSW_Created(obj ect sender, FileSystemEvent Args fsea)
{
if (_status == null)
{
_status = new StatusForm();
}

if (!_status.Visib le)
{
_status.Show();
}

_status.FSW_Cre ated(sender, fsea);
}
}

class StatusForm
{
void FSW_Created(obj ect sender, FileSystemEvent Args fsea)
{
// do your actual status updating here
}
}
2) I've also tried using a delegate to call the show method but still
I get the Exception above.
No surprise there. A delegate is just a way to call a method. It's sort
of like a function pointer. It doesn't affect whether you are in a state
in which Invoke() is allowed or not.

Pete
Jun 2 '07 #9
On Sat, 02 Jun 2007 10:26:10 -0700, Angelo <an*******@gmai l.comwrote:
***** CORRECTION TO THE MY LAST POST *****

I used the frm.CreateContr ol( ) method, not frm.ForceCreate ( ) that I
mentioned.
Ah. That. Well, you'll note in the docs:

CreateControl does not create a control handle
if the control's Visible property is false

So, before calling CreateControl() , you need to make sure you set the
Visible property to true.

I think it's possible that is sufficient to solve your problem, rather
than all the extra code I posted. :)

Pete
Jun 2 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
7842
by: Craig Thompson | last post by:
I've attempted to write a windows service that creates one FileSystemWatcher for each entry in a XML config file. Everything start perfrectly and runs as I expect for about 5 minutes and then after that the FileSystemWatchers seem to stop monitoring... My theory is that for some reason the Garbage Collector is trashing my FileSystemWatchers. Another clue to my situation is that I just took my service and made it into a windows form...
2
2176
by: Sacha Korell | last post by:
I need to set up a FileSystemWatcher in my web application (to automatically process uploaded files) and I'm trying to decide where to set it up. I would like to keep it within the web app, but it needs to watch a certain folder even when nobody is making a request. I could instatiate the FileSystemWatcher in the Application_Start event, but what happens when somebody restarts the server? I guess in that case the FileSystemWatcher wouldn't...
20
4522
by: J-T | last post by:
We are working on an asp.net application which is a 3-tier application.I was aksed to create a component which monitors a folder and gets the file and pass them to a class library in our business logic layer(so far so good and easy).I initialize my class which is using a FileSystemWatcher in my Global.asax and everything works fine.I have found FileSystemWatcher class not very reliable and sometimes it behavies unexpectedly.I'm afriad that...
21
1947
by: dast | last post by:
Hi, I'm having trouble letting my background thread tell my main thread what to do. I'm trying to tell my main thread to open a form, but when my background thread ends, the form that I thought my main thread had opened disappears. Obviously there's something that I don't understand here. The background thread is run in another class on another form. How do I,
1
2759
by: Frederic H | last post by:
Hi, I have a problem when I try to open a new form with the Changed event (FileSystemWatcher) : the form hangs just after the paint. But I use the same function (OpenFormPerform) to open a new form when I click on the Button1 and it works fine. I think that it's a problem with thread or applicationContext. I can use Application.Run(new Form) to put the new form in the message loop but the application is suspended after the...
12
7463
by: ljh | last post by:
Has anyone else noticed that the FileSystemWatcher raises the changed event twice when a file is changed? Do you have any idea why this is the case?
4
4386
by: hooksie2 | last post by:
Hi, I am trying to use FileSystemWatcher to watch a log file which is written to via a 3rd party app and display the log in listbox on a form. The FileSystemWatch seems to work okay but I get the following error when I try to write to the listbox: "A first chance exception of type 'System.ArgumentNullException' occurred in System.Windows.Forms.dll"
17
465
by: Mufasa | last post by:
I have need to be polling a directory for files ( I am going to be using files to have programs communication between each other - don't ask! ) Anyway - the question I have is what happens if in the middle of processing something another files comes in. Does the event get interrupted because the new file is now ready? If so - is there a way to stop it? Essentially make the event a critical region that can't be interrupted. TIA - Jeff.
1
3162
by: Lila Godel | last post by:
My VB.NET 2008 application is setup with a Sub Main and no forms. At run time a NotifyIcon is created with one context menu choice (Close which terminates app). I have no trouble running the application and when I select Close from the icon's right click menu the system tray icon goes away and the application is removed from the listing on the processes tab of task manager. My problem is that the FileSystemWatcher event does not work....
0
9711
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10595
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10343
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10088
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9169
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7633
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5529
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4306
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3831
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.