how to release resources of a process - c#

I am invoking an external process by using System.Diagonistics.Process and passing two filenames as parameters. Now some time this process terminates due to exceptions and it seems that files handle are not being released by the process. How can i release the resources occupied by the process.

What is making you think that file handles are not being released? Since you are starting a process which is external to your application you don't have much control over what the other process is going to do when it terminates abnormally.

This application is WMVAppend.exe (available with Microsoft Media SDK). Even when we restart the machines, it report the error dialogue and work fine for the next run. The issue with only media appending which is not done by our process. I also checked the source files and they seem perfect but they are not appended perfectly and files are not available to use.
Or It might be possible that process is not terminated at all, but i am using Process.WaitForExit() which should return only the process is terminated. The error dialog on restart suggests me this case could be possible.

How are you detecting that file handles are not being released by the dying process?
All resources should be released by a process that dies for any reason. If they aren't, then it is a bug that should be filed with Microsoft.

Related

mySolutionName.exe file deleted from /debug directory. How to restore or rebuild?

I'm new to c#. Antivirus deleted the mySolutionName.exe file out of the /debug directory and now I cannot execute my code. I'm concerned that anything I do may make the issue worse.
When I press F5 I get an error of:
CS2012 C# Cannot open for writing --> C:\Users\me\source\repos\MyApp\MyApp\obj\Debug\MyApp.exe''
Can someone please tell me how to rebuild the file so that I can continue developing?
after your edit:
Your program is probably still running outside of the debugger.
You need to use the task-manageer to kill all instances of MyApp.exe if this doesnt solve your issue a reboot should do the trick as well
So why is this happening?
Most liky your application is somewhere stuck on a blocking function or in a never ending loop. maybe there is even a seperate thread still operating that you forgot to close. We lack some information to tell yout that for sure. But to find out what is happening you can close your window while running in debugger mode and see if the application properly closes. if it doesnt you can hit pause and see where the program is stuck and resolve this issue by ending the task/loop/whatever in your OnClosing function of your window.
A good way to solve this issue is looking which process currently accesses the file.
A tool to do that is Microsoft SysInternals Process Explorer. It has a feature called "Find handle or DLL ..." which can be accessed by Ctrl+F.
The result will show the process which accesses the file. You can then judge whether it's Antivirus or something else that prevents you from writing to the file. If possible, you can then take an action in that program to release the file.
Example: a program is accessing my powerpoint presentation, which has the term "Schulungen" in its file name.
Process Explorer figures out: it's open in Powerpoint.exe, so I can simply close the file in Powerpoint - problem fixed.

Listing core windows processes and services

I'm in a bit of a pinch here and I'd like to get a list of processes running on a machine that associate with windows itself, or are required to be running all the time like a background process or antivirus.
I'm making this program to keep my skills up to scratch, so it will likely never be released on a large scale.
Basically, I'm making an app that scans the computer for programs and only allows those on the whitelist to run. I need to automatically have all windows processes, and those in the startup folder added to this list.
Programs that are not on the list are terminated after a 30 second warning.
I've already gone through several posts on stack, and a google search yielded minimal results.
Is there a way I can differentiate process as a windows process by scanning some metadata or getting the original file path of that process and scanning the file?
PS:
Is there a way to scan all installed programs like in control panel and list them?
Okay. I did it. Finally.
Thanks to all that helped.
I ended up taking a snapshot of all running processes at the time before the timer is enabled and new processes not on the list are blocked. Processes that cannot be closed are likely viruses, or system components. I've never seen an app or a game block process killing before, and parents must ensure task manager is closed before running the timer.
CausticLasagne.

How to tell if my application has been removed from the clipboard listener chain?

I am writing a small utility application to monitor my clipboard. This at current works quite well, but a friend has told me that it randomly will stop showing alerts upon clipboard change, and that restarting fixes it.
I couldn't reproduce, but eventually I ran into it myself - the application had stopped recieving WM_DRAWCLIPBOARD events.
How can I tell when my application has been removed from the listener chain?
This is one of the hazards of the clipboard listener chain: One bad application can damage the chain. Instead of using the SetClipboardViewer function, use AddClipboardFormatListener which does not have this problem.
If you are debugging an application that uses SetClipboardViewer and the application crashes or you stop the application in the debugger, or in some other way bypass the restoration of the clipboard chain, there is performance degradation in Windows, to the point that seemingly unrelated features of Windows, such as Alt-Tab, or restoring a minimized window, stop working. They come back if you restart Windows.
An application that inserts itself into the clipboard chain this way should be calling ChangeClipboardChain during Dispose of the main form, or at another appropriate time, to avoid this issue. Dispose isn't called if you stop the app in the debugger.
I have not tried AddClipboardFormatListener; first time I've heard of it; I'm going to try it to see if I can avoid the issue in my own app while I'm debugging.

In VB.NET check for changes in a file launched with process.start()

I'm developing a vbnet/c#.NET based application that opens files with different applications(excel, word, etc).
The application is launched using Dim app As Process = Process.Start(ProcessProperties)
Now, when I have to terminate the process I use app.Kill() but I need to check if the document has been modified before killing it.
How can I handle that? And if it's possible, how can I launch the application native prompt for save?
Thanks
For office applications use Office Interop Assemblies, not Process.Start to start and control them. Here is an example code for Excel (in VB.NET). You should add Microsoft.Office.Interop.Excel.dll to the project references in order for this to work.
oExcel = New Microsof.Office.Interop.Excel.Application
oBook = oExcel.Workbooks.Open(filepath)
'Do your stuff
oBook.Close 'This will trigger the application native prompt if the document was modified
oExcel.Quit()
For other programs it depends much on a program
You can achieve behaviour close to what you require by calling Process.CloseMainWindow rather than Process.Kill.
The behavior of CloseMainWindow is identical to that of a user closing an application's main window using the system menu. Therefore, the request to exit the process by closing the main window does not force the application to quit immediately.
Data edited by the process or resources allocated to the process can be lost if you call Kill. Kill causes an abnormal process termination, and should be used only when necessary. CloseMainWindow enables an orderly termination of the process and closes all windows, so it is preferable for applications with an interface.
In the case of Office applications with unsaved changes, CloseMainWindow would launch the Save dialog. You would need to handle scenarios where the users presses “Cancel”, since that may result in the WaitForExit call blocking indefinitely.
For example:
// Launch Word application.
Process wordProcess =
Process.Start(#"C:\Program Files (x86)\Microsoft Office\Office12\winword.exe");
// Give user some time to type in text.
Thread.Sleep(TimeSpan.FromSeconds(20));
// Request Word to close.
wordProcess.CloseMainWindow();
// Wait until user saves or discards changes.
// May block indefinitely if user cancels.
wordProcess.WaitForExit();
There are different approaches for this problem. You can calculate some sort of initial check-sum and see whether your document has any changes by redoing the check-sum and comparing against the original one.
This part is not very clear from your question, If the document gets saved, probably you can look at the date_modified value of the file to see whether there has been any modifications.

Application.Exit() or Environment.Exit(1) removes the exe

When I do throw exception I have added the exit, but after that the exe file is removed. is it a known problem? (in VS2010)
Neither Application.Exit nor Environment.Exit will remove the executable from disk.
It sounds like some other process is trying to delete the executable. As soon as the application exits, the file will no longer be in use, and the .exe could be removed.
Depending on what you're doing, this could, potentially, be from a virus scanner. I would check your Virus Scanner logs to see if there is an issue there. This should be very unlikely if this is a completely managed (C# only) application - but if you're using native code that is "questionable" according to the scanner, it may be removing the executable.
Otherwise, I would check for other processes that may be trying to remove the executable.
No this doesnt sound right. Do you mean the .Exe Application file is been removed from Disk? That cannot/Should not happen on Exiting. Can you send mroe details on what you are trying to do, What kind of application, etc. I tried with a simple C# Console application and it wouldnt seem to happen in VS 2010.

Categories