Click here to Skip to main content
15,897,891 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using a FileSystemWatcher to raise the event when an image file is edited on Paint and update the preview image control using it. But setting the file as a source second time, it throws an error because the file is still in use by another process. So I discovered this is because of FileSystemWatcher.

What I have tried:

C#
private void btnEdit_Click(object sender, RoutedEventArgs e)
        {
            if (!File.Exists(lastImage)) return;
            FileSystemWatcher izleyici = new FileSystemWatcher(System.IO.Path.GetDirectoryName( lastImage),
                System.IO.Path.GetFileName(lastImage));
            izleyici.Changed += izleyici_Changed;
            izleyici.NotifyFilter = NotifyFilters.LastWrite;
            izleyici.EnableRaisingEvents = true;
            ProcessStartInfo info = new ProcessStartInfo();
            info.FileName = lastImage;
            info.Verb = "edit";
            Process.Start(info);
        }

        void izleyici_Changed(object sender, FileSystemEventArgs e)
        {
           //I want to add code here to release the file. Dispose() not worked for me

           setImageSource(lastImage);
        }

        void setImageSource(string file)
        {
            var bitmap = new BitmapImage();

            using (var stream = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                bitmap.BeginInit();
                bitmap.CacheOption = BitmapCacheOption.OnLoad;
                bitmap.StreamSource = stream;
                bitmap.EndInit();
            }

            ssreview.Source = bitmap;
        }


In this code I want to release the file before updating the `Image`. I tried Dispose but it was not worked. How can I do that?
Posted
Updated 28-Aug-16 15:19pm
v2
Comments
Philippe Mori 28-Aug-16 17:07pm    
I also think it does not make much sense to blame FileSystemWatcher for your problems.

However, from your code, it look like you never detach events handler which is clearly wrong. In almost any case you attach an handler to an event, you must also detach it at some point. Thus it seems that your code is poorly designed.
Amt-Coder 28-Aug-16 21:13pm    
I was right about one point that the problem was FileSystemWatcher itself, but I was wrong at FSW locks file. I found the solution by asking it in stackoverflow. The problem is that FSW raises the events not in UI. So it needs to be invoked. Please see here

1 solution

Considering the FileSystemWatcher has nothing to do with files themselves and cannot lock them I'm wondering how you determined that it's the FileSystemWatcher that's causing the problem?

The FileSystemWatcher does NOT tell you when the file is closed by the process writing it. It only tells you when the "Last File Write" time changes. This can change during the write of the file several times. It's up to you to TRY to open the file for exclusive read in an attempt to read the content. If this attempt fails, then you wait a bit and try again.
 
Share this answer
 
Comments
Amt-Coder 28-Aug-16 21:16pm    
I was thinking the problem is FSW because I deleted the code part for FSW and so there was no problem at that time. That is the reason. But I was wrong about it locks file. The solution is here if you want to take a look.
Dave Kreskowiak 29-Aug-16 1:15am    
That's nice. You didn't mention anything about threading in your application at all. Had you, it would have made the answer obvious.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900