Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hi All,

 

I have been writing a little application that watches my documents and nay files/folders that are changed/created/deleted/renamed are copied to the main server. As a back up process it seems to work great. I have subclassed the FileSystemWatcher and put all of the logic in there and in my form all I need is:

fileSystemWatcher fsw = new fileSystemWatcher(true, true, true, true, @"\\source\", @"\\destination", true);
fsw.ShowProcess += new fileSystemWatcher.ShowProcessEventHandler(ShowProcess);
fsw.SynchronizingObject = this; 

 

The code is working great with one exception. If I am doing a big process, let's say copying 1GB of music the UI is frozen as the UI thread is busy.

 

Is it possible to put this process to a separate thread possibly using a background worker? If so can anyone give a brief example of how to do this?

 

Thanks, Dave.

Posted

If you need to work in a different thread, something like should work.

 

You may need to use a delagate to get the updateUI event to work correctly.

 

System.Threading.Thread t;

protected void button_click(...)
{
   t = new System.Threading.Thread(runSync);
   t.Start();
}

void runSync()
{
   fileSystemWatcher fsw = new fileSystemWatcher(true, true, true, true, @"\\source\", @"\\destination", true);
   fsw.ShowProcess += new fileSystemWatcher.ShowProcessEventHandler(ShowProcess);
   fsw.SynchronizingObject = this;
}

~Nate�

___________________________________________

Please use the [vb]/[cs] tags on posted code.

Please post solutions you find somewhere else.

Follow me on Twitter here.

  • 4 weeks later...
Posted

Hi Nate,

 

Thanks for your reply. I have not been working on this code for a while. I got a bit stuck with to be honest.

 

Apart from the issue you have posted for the app was working well enough. Until I decided to create 2 instances of the file system watch class I have written. The first watches a folder on my local machines and copies/changes/deletes/updates all files in the directory to a directory on the server. The 2nd instance watches the directory on the server and performs the same operations as the first but this time to a networked hard drive.

 

The events went nuts and the app just locked. Would people say that using the FileSystemWatcher class as I have here is abusing this class or am I trying to do something that is within its capabilities?

Posted

I don't know, but my guess is that you created a "race" condition, update made to client was replicated to server, and the server was replicated to the network drive. I think that the issue may have been doing both at once.

 

What size file did you test with? I'd try a 1kb text file since even a race condition there would only last a few seconds.

 

You may want to schedule events to do these file transfers.

~Nate�

___________________________________________

Please use the [vb]/[cs] tags on posted code.

Please post solutions you find somewhere else.

Follow me on Twitter here.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...