How kill a specific process - vb.net

I have a process that interacts with an Excel application in background, but sometimes the process has an error and the Excel Application doesn't finish correctly.
Always, when this process starts try to delete the Excel file from the hard drive.
But, when the process has an issue and I retry to run the process again, the following message shows me when I try to delete the Excel file:
"The process cannot access the file [Excel file path] because it is being used by another process."
I can not kill all processes of Excel.
Then, my question is:
Is there some way to know what is the process that manage this Excel?
Or, Is there some way to know the process using a Try ... Catch structure?

Related

Stop VBA code and void or cancel all it has already processed?

I understand that an "on error" code can be used if an error occurs. However, I wonder if it is possible to void all that had been processed by that macro? Perhaps recalling the last memory?
For example, I have a code that runs to transfer many different tasks. Is there a code that can be used to undo what it has done other than manually going to those sheets or files and delete them manually?
Thank you in advance.
Save a copy of the workbook to a backup as it starts the code, on error rename the current workbook to something else, restore the backup over the original then close the renamed one you are in.
Open the file again and it will be exactly as it was before you ran the macro.
NOTE, THIS WILL BE SAVING YOUR FILE WHEN IT RUNS, Make sure you have a backup somewhere else when doing your testing.

How to close/stop a .NET application and re-execute it?

My application updates(running a vba script) an excel shared workbook, and since it is shared, there shouldn't be problems when someone else is using the same file at the same time. But for some reason, sometimes it simply freezes, without any error message, just freezes.
Is there a way to programatically make the application stops/closes automatically when frozen or after some minutes(In normal conditions, this updating process shouldn't take more than 1 minute)?
And, if possible, re-launch the app again automatically after some minutes for at least 5 attempts?
This way would ensure process completes succesfully.
I have had to do this same thing before but because I had an application that would look for updates to it's self on the network and then update it locally. Problem is, you cannot update the exe that is running.
What I did to get around it is to create another program that would wait a second, update the exe, then run the exe again.
Because I did this with a few different apps, I made my "Updater" generic so I could send some command line parameters and it would use those to copy and run.
If you want to try something else, you might be able to accomplish this same thing by creating a BAT file and running it. I'm not real good on BAT files so I can't help you there. But, it is another way to handle it.

close tcp\udp connection by process name without killing the process Autoit script

I need help with this autoit script
It's close a tcp\udp connection of a process without killing the process
I found script in a forum but it's not working
let's say I want to close any connection established from "internet download manager"
so I put in process name
$processname = "IDMan.exe"
but it still not working !!
I guess some thing wrong in the code
Second : I want to but a WHILE loop to kill the process connection whenever it started
how can I do that
Here is the script without modifications
Link to script
sorry I tried to put the script source here but I failed
What you need is an Admin Rights.
Add this to the top of your script
#RequireAdmin
You can use CloseTCPConnectionByProcessName in a while loop, just add some sleep to keep it stable.

VB program works fine when stepping through but crashes when ran with the .exe

This is a weird persistant issue I am having with a small visual basic program.
Problem: The excutable of the vb program crashes when trying to run but when I step through the program inside of visual studio it runs successfully.
Details: The program performs a winscp.com transfer from a ftp server and then takes the downloaded file and extracts the data from it before sending it to a webpage. The program also decrypts a des3 encrypted file which holds the login details for the sftp server and the webpage.
My Thoughts: I was thinking this could be something to do with the excutable jumping ahead of the slower transfer and decrypt functions thus causing a "file not found" exception to be produced and the program to exit. Lending to this if I slowly (aka spend 10+ seconds stepping through the code) move through the code it works just fine.
Solution: I was thinking of including some kind of checks inside the program to make sure that the file exsists before moving on but I still wanted to get your guys opinion.
Thanks!
As requested
Crash Messages:
(I capture everything in exceptions and exit properly so no "crash" is reported but the error I get in my logs is as follows:)
1st run with standard test case:
Could not find file 'C:\Users\Administrator\Desktop..\ILC2INFOENC.txt'
2nd run standard case (the file above was not deleted by my cleanup function either as it could not be found)
Could not find file 'C:\Users\Administrator\Desktop..\ILCNETSL10663.csv'
Background:
The first file that is found missing is the encrypted login information file while the second file is the winscp.com downloaded csv file.
Thanks again for all the help and suggestions.
Solution: Applied checks to the existance of the files in question as to keep the vb program from jumping ahead of the slower moning openssl decrypt and the winscp file transfer. If I had the time to redo this portion of code I would have utilized the .net framework provided sftp function and the decrypter instead of winscp and openssl as to better control the flow of the program! If you need more options check below the original question for different takes of this issue.

How can I protect a process I start from within my vb.net program?

I've created a small application that basically reads and writes to a single Excel.exe process. It's basically a timer that records the time I use on projects and then store it in an Excel sheet. This works great, however, I've noticed that if I open Excel manually, work on some sheets and whatnot, save and exit etcetc, the process my software use gets broken or something. The same thing that happens if I manually close the excel.exe process and my software doesn't "know".
So I was wondering if it's possible to protect the excel.exe process somehow? To make sure it can't be closed or tampered with in the meantime?
Let me suggest an alternative approach that does not require you to have an Excel process running all the time (after all, this also consumes a lot of system resources):
Let your application record your information. Every now and then -- for example, after a work entry has been finished or a specific time has elapsed -- open the Excel sheet, write the data, and close it again (also closing the Excel process that you are automating). This save operation should not take more than a few seconds and it will (mostly) prevent the problem you are experiencing.
In fact, since Office automation is always a bit painful, an even better way would be to output your data without requiring an Excel process. To do this, you could use
one of the third-party Excel libraries available for .net,
a CSV or HTML file, which can be opened by Excel, or
open the Excel file as a database with ADO.NET.
You cannot protect a process, but you can check the process.HasExited property to find out whether the process has terminated and take action based on that.
Add an exception handler. Either call non-throw methods if possible.