0

I have an application uses C://root folder. This folder contains images. These images are readed by Pictureboxes.

  1. Images are coming from a linux machine by FTP. A linux machine uses ftp to open C://root which is in the windows. Save the IMAGE_1.jpg.
  2. IMAGE_1.jpg is readed by C# windows form application to show in the picturebox.

Images are coming every 10 second from linux machine. Windows shows these images in a thread.

Sometimes, linux or windows send exception. Because while one of them trying to read image to show(windows), the other is trying to save the image(linux)

Therefore, I have to understand that, if Image_1.jpg is used by linux machine, do not try to show the image in win form.

But how?

tshepang
  • 12,111
  • 21
  • 91
  • 136
Birol Capa
  • 349
  • 2
  • 6
  • 18
  • Can you modify the application/script on the linux machine? – azhrei Mar 21 '13 at 09:54
  • No, it is not possible, linux machine is a client, windows is the server, in the application, this must be hold on the windows – Birol Capa Mar 21 '13 at 09:55
  • What do you mean by "every 10 seconds"? Does the linux machine ftp one file at a time, and after it finishes uploading a file, does it wait at least 10 seconds, before it starts uploading a new file? – azhrei Mar 21 '13 at 10:05
  • windows machine send a signal every 10 seconds to get the image file. then linux machine sends the images by ftp to c://root – Birol Capa Mar 21 '13 at 10:07
  • Have you seen this similar question? (http://stackoverflow.com/questions/699538/file-access-error-with-filesystemwatcher-when-multiple-files-are-added-to-a-dire) – Maxime Vernier Mar 21 '13 at 10:08
  • I see it, problem is there is no exception at the winform but linux machine gives exception, it must be like that, in every situation linux can wwrite the file to the c://root, if it is busy, windows must wait, after linux machine done its job, then windows tries to show the images. – Birol Capa Mar 21 '13 at 10:17
  • I just checked out Maxime's comment after posting my answer. Maxime's point (I think) is to try sharing the file, ie so that your Windows box doesn't have problems writing to image1.jpg as new chunks are uploaded from linux. That should stop the exceptions you're seeing on linux. – azhrei Mar 21 '13 at 11:31

1 Answers1

1

In your Win Forms app, open the file for read, and share the file with other processes so they can keep reading/writing to the file.

Use the File.Open Method (String, FileMode, FileAccess, FileShare) to do this.

If you just use File.Open Method (String, FileMode) or File.Open Method (String, FileMode, FileAccess) then the file will be unshared.

By sharing, you should be keeping the ftp side of things happy.

If you get an exception while trying to open the file in Windows - that's fine. Just catch the exception and try again soon.

When you successfully open the file, check if the last two bytes are FF D9. In this case your JPG has finished being uploaded.

Here's some pseudo code.


    success = false
    using (FileStream fs = File.Open(path,  // eg your Image1.jpg
                                     FileMode.Open,
                                     FileAccess.Read, // we just need to read
                                     FileShare.ReadWrite)) // important to share!
    {
       // if last two bytes are FF D9 then
       //    success = true... can display image now
    }
    if (!success)
    {
       // file is being uploaded, or some other problem occurred
       // try again later
    }

azhrei
  • 2,303
  • 16
  • 18
  • Dear azhrei, I am just doing it before I ask my question: using (FileStream ImgFileStream = new FileStream(Path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { RefImg = Image.FromStream(ImgFileStream); } however, I still get error from FTP side, some times – Birol Capa Mar 21 '13 at 11:54
  • ok - sounds to me you might have a few problems - why don't you explain what errors you are seeing on FTP side? (edit your original question) – azhrei Mar 21 '13 at 12:09
  • Dear azhrei and Maxime Vernier, I changed my algorithm, changing the thread and also use your suggestions about fileaccess and fileshare, this combination solved my problem. thanks a lot. – Birol Capa Mar 21 '13 at 14:15