473,756 Members | 9,576 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 4597
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
13082
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
6387
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
2785
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
2184
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
2190
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
4015
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
4742
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
16147
by: SeC | last post by:
Hi. Is there any way to detect if application is being killed by 'End Process' via Task Manager ?
4
20781
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
9482
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10062
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9901
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9878
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
9728
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
5167
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5322
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3827
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
3
2694
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.