Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. because it is being used by another process

because it is being used by another process

Scheduled Pinned Locked Moved Visual Basic
questioncsharphelp
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    KreativeKai
    wrote on last edited by
    #1

    In my program I create a file using: dim strFileName as string = "c:\test.txt" File.Create(My.Settings.strFileName) I'm just using the file as a basic reference that the program is running and while this file exists don't try and start another instance of the program. I don't use the file or add to it or anything. Later I try to delete the file using: File.Delete(My.Settings.strFileName) I've also tried Kill(My.Settings.strFileName) Unfortunately I get this error: The process cannot access the file 'c:\test.txt' because it is being used by another process. I understand the error, but how do I get the program to let go after I create it so I can delete it later? :confused:

    Lost in the vast sea of .NET

    K G 2 Replies Last reply
    0
    • K KreativeKai

      In my program I create a file using: dim strFileName as string = "c:\test.txt" File.Create(My.Settings.strFileName) I'm just using the file as a basic reference that the program is running and while this file exists don't try and start another instance of the program. I don't use the file or add to it or anything. Later I try to delete the file using: File.Delete(My.Settings.strFileName) I've also tried Kill(My.Settings.strFileName) Unfortunately I get this error: The process cannot access the file 'c:\test.txt' because it is being used by another process. I understand the error, but how do I get the program to let go after I create it so I can delete it later? :confused:

      Lost in the vast sea of .NET

      K Offline
      K Offline
      Kschuler
      wrote on last edited by
      #2

      You can accomplish the same goal by going to the Project settings, view the Application Tab, and make sure the checkbox that says Make single instance application is checked. Now only one instance of your application can run at a time. Also, if you want to, you can View the Application Events and add and event called the MyApplication_StartupNextInstance. You can add code here to perform a specific task if a user tries to run the application more than once. Hope this helps.

      K 1 Reply Last reply
      0
      • K KreativeKai

        In my program I create a file using: dim strFileName as string = "c:\test.txt" File.Create(My.Settings.strFileName) I'm just using the file as a basic reference that the program is running and while this file exists don't try and start another instance of the program. I don't use the file or add to it or anything. Later I try to delete the file using: File.Delete(My.Settings.strFileName) I've also tried Kill(My.Settings.strFileName) Unfortunately I get this error: The process cannot access the file 'c:\test.txt' because it is being used by another process. I understand the error, but how do I get the program to let go after I create it so I can delete it later? :confused:

        Lost in the vast sea of .NET

        G Offline
        G Offline
        Gregory Gadow
        wrote on last edited by
        #3

        File.Create does not just create a file, it also opens it and returns a reference to the created FileStream. Try this:

        Dim MyFile As FileStream = File.Create(My.Settings.strFileName)

        You can then use MyFile to add text. When you are done, call

        MyFile.Close
        File.Delete(My.Settings.strFileName)

        K 1 Reply Last reply
        0
        • G Gregory Gadow

          File.Create does not just create a file, it also opens it and returns a reference to the created FileStream. Try this:

          Dim MyFile As FileStream = File.Create(My.Settings.strFileName)

          You can then use MyFile to add text. When you are done, call

          MyFile.Close
          File.Delete(My.Settings.strFileName)

          K Offline
          K Offline
          KreativeKai
          wrote on last edited by
          #4

          Thanks!!! That worked perfectly!

          Lost in the vast sea of .NET

          D 1 Reply Last reply
          0
          • K Kschuler

            You can accomplish the same goal by going to the Project settings, view the Application Tab, and make sure the checkbox that says Make single instance application is checked. Now only one instance of your application can run at a time. Also, if you want to, you can View the Application Events and add and event called the MyApplication_StartupNextInstance. You can add code here to perform a specific task if a user tries to run the application more than once. Hope this helps.

            K Offline
            K Offline
            KreativeKai
            wrote on last edited by
            #5

            My app is being called by a service and isn't really Form based at all. I currently don't have the application framework enabled. This suggestion was great... I didn't know this feature existed, but I'll try it another day on a different app which is more based around forms. Thanks!! :)

            Lost in the vast sea of .NET

            1 Reply Last reply
            0
            • K KreativeKai

              Thanks!!! That worked perfectly!

              Lost in the vast sea of .NET

              D Offline
              D Offline
              Dave Kreskowiak
              wrote on last edited by
              #6

              If you're using a file to prevent a second instance from being opened, you have a problem. This won't work in all cases since you can have two instances of your app both checking for the existance of the file and both trying to create the file at the same time. A better solution is to create a Mutex instead. A file can be created by multiple processes at the same time. A Mutex cannot. If two processes try to create the same mutex as the same time, one of them is forced to wait until the other request completes. Documention on the Mutex class can be found here[^]. Just be sure to read it and to release the Mutex when your app is done with it.

              A guide to posting questions on CodeProject[^]
              Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                   2006, 2007, 2008
              But no longer in 2009...

              modified on Tuesday, April 6, 2010 6:44 PM

              K 1 Reply Last reply
              0
              • D Dave Kreskowiak

                If you're using a file to prevent a second instance from being opened, you have a problem. This won't work in all cases since you can have two instances of your app both checking for the existance of the file and both trying to create the file at the same time. A better solution is to create a Mutex instead. A file can be created by multiple processes at the same time. A Mutex cannot. If two processes try to create the same mutex as the same time, one of them is forced to wait until the other request completes. Documention on the Mutex class can be found here[^]. Just be sure to read it and to release the Mutex when your app is done with it.

                A guide to posting questions on CodeProject[^]
                Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                     2006, 2007, 2008
                But no longer in 2009...

                modified on Tuesday, April 6, 2010 6:44 PM

                K Offline
                K Offline
                KreativeKai
                wrote on last edited by
                #7

                Thanks! That does sound like a better solution! I'll look into it. :)

                Lost in the vast sea of .NET

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups