How to determine which process to kill ???

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dino Buljubasic

    #1

    How to determine which process to kill ???

    My application can allow a user to open a file for viewing by fetching
    file data from database, creating the file in a temp directory and
    starting appropriate process (i.e. Adobe or any other depending of
    file type) to open the file.

    when I close my application, it is suppose to clean all files it
    created in temp directory.

    I do this by :
    for each file in temp
    File.Delete(fil e)
    next

    This works fine but if a file is open (say pdf file open by Adobe
    process), I can not delete the file with this because the file is used
    by Adobe.

    How can I determine which process is using the file and kill it in
    order to delete the file from temp directory.

    I need something like this

    for each file in temp
    if file is open then
    process = get process using this file
    kill(process)
    end if

    delete file
    next

    Thank you
    _dino_
  • Mike Labosh

    #2
    Re: How to determine which process to kill ???

    1. When you create your temp files in your application, you should hold onto
    the filenames that were used, like, in a string array or something:

    ' untested air code

    Private _tmpFileList As New ArrayList()

    Private Function createTempFile( ) As String

    Dim filename As String = _
    System.IO.Path. GetTempFileName ()

    _tmpFileList.Ad d(filename)

    End Function

    Private Sub cleanupTempFile s()

    For Each filename As String In _tmpFileLilst
    Try
    File.Delete filename
    ' Catch -- do something here for FileNotFound or File is open
    Finally
    System.IO.Path. Delete(filename )
    End Try
    Next

    End Sub

    You could also accumulate your FileStream's or StreamReader or Writer in a
    HashTable keyed off the filenames you get from createTempFile( )
    [color=blue]
    > for each file in temp
    > if file is open then
    > process = get process using this file
    > kill(process)
    > end if
    >
    > delete file
    > next[/color]

    2. That right there sounds dangerous to me. What if you accidentally kill
    some system related process or Windows Service or your AntiVirus agent(s).
    You could destabilize the system. As a rule, your application should only
    be deleting files that it created.

    --
    Peace & happy computing,

    Mike Labosh, MCSD

    "When you kill a man, you're a murderer.
    Kill many, and you're a conqueror.
    Kill them all and you're a god." -- Dave Mustane


    Comment

    • Dino Buljubasic

      #3
      Re: How to determine which process to kill ???

      OK, the question is "What to do when file is open (i.e. held by
      another process, for example by process used to open it)?"
      What I need to do is to close it and then delete it. How can I do
      that?


      On Fri, 23 Sep 2005 15:09:55 -0400, "Mike Labosh"
      <mlabosh@hotmai l.com> wrote:
      [color=blue]
      >1. When you create your temp files in your application, you should hold onto
      >the filenames that were used, like, in a string array or something:
      >
      >' untested air code
      >
      >Private _tmpFileList As New ArrayList()
      >
      >Private Function createTempFile( ) As String
      >
      > Dim filename As String = _
      > System.IO.Path. GetTempFileName ()
      >
      > _tmpFileList.Ad d(filename)
      >
      >End Function
      >
      >Private Sub cleanupTempFile s()
      >
      > For Each filename As String In _tmpFileLilst
      > Try
      > File.Delete filename
      > ' Catch -- do something here for FileNotFound or File is open
      > Finally
      > System.IO.Path. Delete(filename )
      > End Try
      > Next
      >
      >End Sub
      >
      >You could also accumulate your FileStream's or StreamReader or Writer in a
      >HashTable keyed off the filenames you get from createTempFile( )
      >[color=green]
      >> for each file in temp
      >> if file is open then
      >> process = get process using this file
      >> kill(process)
      >> end if
      >>
      >> delete file
      >> next[/color]
      >
      >2. That right there sounds dangerous to me. What if you accidentally kill
      >some system related process or Windows Service or your AntiVirus agent(s).
      >You could destabilize the system. As a rule, your application should only
      >be deleting files that it created.[/color]

      Comment

      • Mike Labosh

        #4
        Re: How to determine which process to kill ???

        > OK, the question is "What to do when file is open (i.e. held by[color=blue]
        > another process, for example by process used to open it)?"
        > What I need to do is to close it and then delete it. How can I do
        > that?[/color]

        I think you misunderstand. Your application should not go around killing
        other processes just so you can delete their files. That should be the job
        of the other process. If the other process that's not yours leaves files
        cluttering up %TEMP% then you should manually delete them after politely
        shutting down the other process. (Like File-> Exit or Restarting a Service
        or Rebooting)

        If the other process is one of your apps, change it's behavior and redeploy.

        --
        Peace & happy computing,

        Mike Labosh, MCSD

        "When you kill a man, you're a murderer.
        Kill many, and you're a conqueror.
        Kill them all and you're a god." -- Dave Mustane
        "Dino Buljubasic" <dino@noplaceli kehome.com> wrote in message
        news:34l8j197np fmvbmadvg4a6pjs t1vngvkam@4ax.c om...[color=blue]
        >
        > On Fri, 23 Sep 2005 15:09:55 -0400, "Mike Labosh"
        > <mlabosh@hotmai l.com> wrote:
        >[color=green]
        >>1. When you create your temp files in your application, you should hold
        >>onto
        >>the filenames that were used, like, in a string array or something:
        >>
        >>' untested air code
        >>
        >>Private _tmpFileList As New ArrayList()
        >>
        >>Private Function createTempFile( ) As String
        >>
        >> Dim filename As String = _
        >> System.IO.Path. GetTempFileName ()
        >>
        >> _tmpFileList.Ad d(filename)
        >>
        >>End Function
        >>
        >>Private Sub cleanupTempFile s()
        >>
        >> For Each filename As String In _tmpFileLilst
        >> Try
        >> File.Delete filename
        >> ' Catch -- do something here for FileNotFound or File is open
        >> Finally
        >> System.IO.Path. Delete(filename )
        >> End Try
        >> Next
        >>
        >>End Sub
        >>
        >>You could also accumulate your FileStream's or StreamReader or Writer in a
        >>HashTable keyed off the filenames you get from createTempFile( )
        >>[color=darkred]
        >>> for each file in temp
        >>> if file is open then
        >>> process = get process using this file
        >>> kill(process)
        >>> end if
        >>>
        >>> delete file
        >>> next[/color]
        >>
        >>2. That right there sounds dangerous to me. What if you accidentally kill
        >>some system related process or Windows Service or your AntiVirus agent(s).
        >>You could destabilize the system. As a rule, your application should only
        >>be deleting files that it created.[/color]
        >[/color]


        Comment

        • Dino Buljubasic

          #5
          Re: How to determine which process to kill ???

          So would the right way be to simply close my application leaving say
          pdf file open?

          Because in that case, next time my application runs, if the user tries
          to open the same pdf file and assuming that he/she has not closed it
          after terminating my application I am going to run into the same
          problem, this time trying to open a file that is already open.

          Sorry but it is little bit confusing to me.

          Thank you for your help

          On Fri, 23 Sep 2005 15:28:56 -0400, "Mike Labosh"
          <mlabosh@hotmai l.com> wrote:
          [color=blue][color=green]
          >> OK, the question is "What to do when file is open (i.e. held by
          >> another process, for example by process used to open it)?"
          >> What I need to do is to close it and then delete it. How can I do
          >> that?[/color]
          >
          >I think you misunderstand. Your application should not go around killing
          >other processes just so you can delete their files. That should be the job
          >of the other process. If the other process that's not yours leaves files
          >cluttering up %TEMP% then you should manually delete them after politely
          >shutting down the other process. (Like File-> Exit or Restarting a Service
          >or Rebooting)
          >
          >If the other process is one of your apps, change it's behavior and redeploy.[/color]

          Comment

          • Dino Buljubasic

            #6
            Re: How to determine which process to kill ???

            I guess i shoud simply then catch io exception when trying to open the
            file and not process it or?

            On Fri, 23 Sep 2005 15:28:56 -0400, "Mike Labosh"
            <mlabosh@hotmai l.com> wrote:
            [color=blue][color=green]
            >> OK, the question is "What to do when file is open (i.e. held by
            >> another process, for example by process used to open it)?"
            >> What I need to do is to close it and then delete it. How can I do
            >> that?[/color]
            >
            >I think you misunderstand. Your application should not go around killing
            >other processes just so you can delete their files. That should be the job
            >of the other process. If the other process that's not yours leaves files
            >cluttering up %TEMP% then you should manually delete them after politely
            >shutting down the other process. (Like File-> Exit or Restarting a Service
            >or Rebooting)
            >
            >If the other process is one of your apps, change it's behavior and redeploy.[/color]

            Comment

            Working...