473,695 Members | 2,782 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_
Nov 21 '05 #1
5 4595
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( )
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*****@hotmai l.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.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( )
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**@noplaceli kehome.com> wrote in message
news:34******** *************** *********@4ax.c om...
On Fri, 23 Sep 2005 15:09:55 -0400, "Mike Labosh"
<ml*****@hotmai l.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.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( )
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*****@hotmai l.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*****@hotmai l.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
13071
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 finished running, I want to do more stuff *condition* on the fact that the shell script has finished running, inside the same python script. The only way I can think of is to fork a process and then call the
3
6385
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 to trace pid 124 of user A (uid = 100) and then I noticed that he doing something wrong so, I get process id now process id is 156 (supposed) then I want to kill all inherrit processes of user A (now I know only this process is 156 and uid is 100)...
1
2777
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 windows by themselves, and force them to use the Main app. If I use Windows Services, I'm not sure can I create one dynamically, and then kill easily. But, if using console application, my following coding is working now. using System;
6
2177
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 instance individually, but this does not appear possible using the Process object. When I run the example below the process object "p" can be viewed using Quick Watch however process object p2 is displayed as undefined, with the added affect of not...
0
2182
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 command button in the application. So the code goes something like this: psiBat = New ProcessStartInfo(arrRun.Item(0)(1)) psiBat.WindowStyle = ProcessWindowStyle.Hidden
3
4011
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(), I just want a quick way to see if the process I started is finished. I could popen("ps -p %d" % pid) and see whether it's there anymore...but since pids get reused, there's the chance (however remote) that I'd get a false positive, plus I don't...
8
4729
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 kill the process someone.. does anyone know the code for this? thank you so much in advance!!!!
9
16121
by: SeC | last post by:
Hi. Is there any way to detect if application is being killed by 'End Process' via Task Manager ?
4
20775
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 subprocess.Popen call, and afterwards, (using a web request) the python code uses the PID of the previously created process(stored in a db) and kills it with an os.kill call using the SIGKILL signal. The creation of the process is ok, apache calls the...
0
8568
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8845
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8825
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7660
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5839
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4579
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3003
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2272
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1976
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.