The process can not access the file because it is being used by another process.

I have some issues related to WebBrowser Control in .net cf.

And in its URL Property I am assigning one .htm file. In that .htm file we are setting the backgroung using one image file

"#BKGROUND#", "\"" + FilePaths.stStrImagesFolderPath + @"\backgroundImage.jpg" + "\""

But sometimes, specially while doing fast navigation , Its throwing exception "The process can not access the file because it is being used by another process." Sometimes its coming for .htm file and sometimes it comes for backgroundImage.jpg file.

How to avoid this.

Kindly help.



Answer this question

The process can not access the file because it is being used by another process.

  • Argenta

    I have 5 Tabs, and pressing on each tab I am showing different .html files in WebBrowser control.The Html Files are different for all the tabs, but the background Image is same for all the files. So While Moving fast from tab to tab, sometimes its throwing exception "The process can not access the file because it is being used by another process."
  • Ken Cooper

    Subhasmita,

    I've got a couple more questions:

  • Which platform (PPC2003, WM5, SmartPhone) are you targeting This is very important because the actual underlying native control may be IE, Pocket IE, or an HTML reader control depending on the platform.
  • Which version of the compact framework are you using
  • Does this repro on the emulator If it does, it will be easier for me to repro here. You should be able to download the correct emulator image from the SDKs on the .NET Compact Framework Downloads page.
  • Does each tab page have its own instance of the WebBrowser or are you moving a single WebBrowser instance from page to page If the former, are the contents of the webbrowser instances pre-loaded during Form.Load or does each navigate when its page is activated
  • Does your application ever write to the html files If so, is your app locking the html file
  • Would you post code for a minimal repro
  • In the meantime, I'll try to find out if the native controls lock any files during navigation.

    Thanks
    Dan


  • Moridi

    A few questions:

    • Is it possible that some other program really does have the file locked Try doing a soft reset to release all file handles, then run your app immediately after the device boots.
    • What do you mean by "fast navigation"
    • Does the problem occur when navigating to your page or away from your page or both
    • Are you deploying from Visual Studio or manually copying files to the device
    • Which version of the compact framework are you using
    • Which platform (PPC2003, WM5) are you targeting
    • Does this repro on the emulator

    Thanks
    Dan


  • poker_paul

    Hey Dan,

    1. The target platform is WM5

    2. We r using .Net cf 2.0.

    3. No its not reproducable on Emulator.

    4. Yeah we are using the same Web-Browser control on each tab. Just Changing the WeBrowser.Url propery as the tab changes.

    5. Yeah OnClicking the Tab for the first time, we are creating and saving the corresponding .html file and setting it as the Url of the WebBrower.

    Note : html file used is different for all the tabs, but the backgroundImage file for all the .html file are tha same.

    And one more thing at some point of time we are deleting the .html files also. So at that point of time also this exception can come right.

    Thanks for your response buddy. Can you suggest me any logic to avoid this kind of exception Sowhere I have read about trying to open the file with fileShare 'None', I was planning to put some delay if exception occurs...But What I know how long it will take to release the file......

    And one more thing can we forcibly close any file which is being used by some other process


  • lord_8

    I would suggest a couple of things:

    • Both before and after you navigate to a page, attempt to open and read the file with System.IO.FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read). This will determine whether or not the WebBrowser has the file locked.
      
      System.IO.FileStream stream = null;
      try
      {
        System.IO.FileStream stream = new System.IO.FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read);
        if (null != stream)
        {
          byte[] bytes = new byte[stream.Length];
          int bytesRead = stream.Read(bytes, 0, stream.Length);
        }
      }
      finally
      {
        if (null != stream)
        {
          stream.Close();
        }
      }
      
      
    • When navigating to a new tab, don't set WebBrowser.Url until you are sure that the current tab has finished it's file operations.
    • Monitor the Navigating, Navigated, and DocumentComplete events to see at which point the exception is raised. I'm not sure how helpful this will be, but it might point to whether it is your file or a cached file that is locked.
    • Put the call that is throwing the exception in a try/catch block in loop and retry until the navigation succeeds
      bool keepTrying = true;
      while (keepTrying)
      {
      try
      {
      webBrowser1.Url = new Uri("myfile");
      }
      catch (Exception x)
      {
      keepTrying = DetermineWhetherToKeepTrying();
      }
      }
    Let me know what you find out.
    Dan

  • Sniper167

    Hey Thank you so much Dan. I am implementing this and I will let you know, the results.

    Regards,

    Subhasmita.


  • The process can not access the file because it is being used by another process.