473,324 Members | 2,248 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

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(file)
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_
Nov 21 '05 #1
5 4573
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.Add(filename)

End Function

Private Sub cleanupTempFiles()

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()
for each file in temp
if file is open then
process = get process using this file
kill(process)
end if

delete file
next


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
Nov 21 '05 #2
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"
<ml*****@hotmail.com> wrote:
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.Add(filename)

End Function

Private Sub cleanupTempFiles()

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()
for each file in temp
if file is open then
process = get process using this file
kill(process)
end if

delete file
next


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.


Nov 21 '05 #3
> 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?
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" <di**@noplacelikehome.com> wrote in message
news:34********************************@4ax.com...
On Fri, 23 Sep 2005 15:09:55 -0400, "Mike Labosh"
<ml*****@hotmail.com> wrote:
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.Add(filename)

End Function

Private Sub cleanupTempFiles()

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()
for each file in temp
if file is open then
process = get process using this file
kill(process)
end if

delete file
next


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.

Nov 21 '05 #4
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"
<ml*****@hotmail.com> wrote:
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?


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.


Nov 21 '05 #5
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"
<ml*****@hotmail.com> wrote:
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?


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.


Nov 21 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: John Lin | last post by:
Howdy, I want to know how to tell if a forked process is done. Actually, my real question is that I want to run a shell script inside of a python script, and after the shell script has...
3
by: pattanawadee | last post by:
Deall All, Could anybody suggestion me How to kill all inherrit processes (sibling child,previous and parent process) in the case I know only child process id and user id, For example I strart...
1
by: Mullin Yu | last post by:
But, I want is that I can have a Main app that will start a new process or kill one particular or all process. The process will open a console exe. But, I don't want the user to close the console...
6
by: gizmo | last post by:
I have a requirement to initiate more than one instance of an application using the filenames. (the example below will start two instances of MS Word). My problem is that I need to kill each...
0
by: WATYF | last post by:
This is my problem... I have some code that starts a Process and returns it to a variable... (prcBat) At any time while that process is running... I want to be able to Kill it by pressing a...
3
by: David Hirschfield | last post by:
I'm launching a process via an os.spawnvp(os.P_NOWAIT,...) call. So now I have the pid of the process, and I want a way to see if that process is complete. I don't want to block on os.waitpid(),...
8
by: Rain | last post by:
Does anyone know how i can kill a process using C#? im developing an asp.net web application using C# and the asp.net process doesnt die even if the application has already exited. So i need to...
9
by: SeC | last post by:
Hi. Is there any way to detect if application is being killed by 'End Process' via Task Manager ?
4
by: Richard Rossel | last post by:
Hi Fellows, I have a problem with process termination. I have a python code that apache runs through a django interface. The code is very simple, first, it creates a process with the...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.