My application task can't be killed by cmd - c#

My C# and WPF application task can't be deleted by Task Manager or by console. It's a very big application but there's nothing new that can make the task 'bulletproof'
I tried:
wmic process where name="MyApp.exe" call terminate
But it says: return value 2 and it does't kill the process.
With
Taskkill /IM MyApp.exe /T /F
It returns "Access denied" even when I run it as administrator.
I don't really know what is happening and why Windows can't close the task of my application. If I restart the computer the task finish.

Related

Why my task scheduler wont run my .exe program?

Task scheduler says the task is completed but upon checking, my .exe didn't produce some output. there's nothing in my log as well.
I created a .BAT file to execute my C# program .exe in task scheduler.
Here's my .BAT file code -
echo #off
start "Bank" C:\Users\mySpace\Desktop\bank.exe
Here's the screenshot of my Task scheudler -

C# run a .bat file where some of the commands are to be run in an attached linux shell

I am able to execute a .bat file with these commands contained:
ssh -i BatchStagingVM_key.pem azureuser#xx.xxx.xx.xx //works
timeout /T 5 /NOBREAK
echo blah
Essentially the first one runs, connecting to the VM, but the last two does not even register or get to the shell in the logged into linux vm command line. there is a few questions relating to executing .bat files, but none where some of the commands are to be executed on a vm on that same command line instance. Running these one at a time, works fine, but I want to automate this. Any advice/ideas/packages to check out?
The linux VM command line, that isn't receiving commands from the .bat file execution

Stop windows command prompt from "returning" after an executable is started?

I am developing an application that writes some exception messages to the Console if they are of no use to the user. As a result if someone runs the executable by simply double clicking it, they will never see these messages.
In Visual Studio these messages show up in the output window, but in my case I am testing my application on a machine that does not a Visual Studio installation, though I still want to see if any of these messages appear.
In the past I have simply ran the executable from the command prompt and it acted as the output window in Visual Studio. Though for some reason my application, when ran from the command prompt, simple "returns" and does not show any messages.
For example I might start it like so, and it instantly returns
D:\>MyApp.exe
D:\>
I am not sure if there is a specific switch I should use (I have tried /K to no avail) when I run it, or if there is something about my application that causes it to return.
Any ideas on how I might run it via the command line so I can see the messages?
For reference here is my applications Program.cs
static class Program
{
static Mutex mutex = new Mutex(true, "{12345678-1234-1234-1234-123456789012}");
[STAThread]
static void Main()
{
if (mutex.WaitOne(TimeSpan.Zero, true))
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
SplashScreen.ShowSplashScreen();
Application.Run(MainForm.Instance);
mutex.ReleaseMutex();
}
else
NativeMethods.PostMessage((IntPtr)NativeMethods.HWND_BROADCAST, NativeMethods.WM_JTTMAINWINDOW, IntPtr.Zero, IntPtr.Zero);
}
}
To allow your program to output to the console, build it as a console application.
In Visual Studio, the relevant linker option is /SUBSYSTEM:Console ; or, when creating a new project, choose the "console application" template which will set the linker option automatically.
This will also mean that if you run the program from the command prompt, the command prompt will wait for the program to exit. Also, if you double-click the program rather than running it from the command prompt, a console window will automatically be created.
(There is no way to make the command prompt wait for a program but prevent the program from creating a console window if double-clicked. See this question and its answers for more information.)

Application process after msi installer finished is started as SYSTEM user name which sometimes can not create com object

I have a visual studio installer project which installs a C# app, I have a custom action and code to run the process after install is complete.
Let's say the logged in user on windows machine is "john". Now when john runs the msi installer, I check the process in the taskmanager and it shows that msiexec.exe is the process name for installer and it is running as user "john"
The installer completes and runs the install app's process myapp.exe now when I check this process in taskmanager of windows it shows that myapp.exe is running as SYSTEM (which I do know what account is that and why its not running as john)
Problem
When myapp.exe runs as SYSTEM user it can not create com component instance of a component (iTunes in my case) which was already running as user john. If the component was not running then creating isntance of the iTunes is sucessful, otherwise it fails.
Question
So is it possible to make sure when installer runs as john, when it finishes it starts process myapp.exe as john and not as SYSTEM user ? Note that I do not asks user for password during installer.
Code that I run when installer completes
// Event handler for 'Committed' event.
private void MyInstaller_Committed(object sender, InstallEventArgs e)
{
try
{
Directory.SetCurrentDirectory(Path.GetDirectoryName
(Assembly.GetExecutingAssembly().Location));
Process.Start(Path.GetDirectoryName(
Assembly.GetExecutingAssembly().Location) + "\\MyApp.exe");
}
catch
{
// Do nothing...
}
}
This most likely happens because you are running your custom action as deferred. i.e. it is running by default under the SYSTEM user account.
A solution is to make sure you launch it immediate, for example you can launch it using a published event on the "Finish" button from the last dialog of the installation. I don't know exactly how to add a published event in VS setup projects, or if it is possible, but you can easily add one in packages built with specialized setup authoring tools.
In the Form_Load event write like this:
string lUserName=Environment.GetEnvironmentVariable("USERNAME");
Then instead of getting system user account you will get windows login user name at the time of installing MSI.
Personally I did manage to resolve this by running explorer.exe parameterized with the path to my app's executable:
Process.Start(
new ProcessStartInfo
{
FileName = "explorer.exe",
Arguments = Context.Parameters["target"],
UseShellExecute = true
});

Delete self while running

Im currently creating a uninstallation application which deletes the application folder. The problem is that i cant delete the uninstaller application which is in the same folder cause its running.
Is there a way to delete the application while running, so its just in the memory.
string Installation = UninstallRegister.Read("InstallationLocation");
if (Directory.Exists(StartMenu))
{
Directory.Delete(StartMenu, true);
}
Best regards
A simple idea would be to copy the uninstall application to %TEMP% and run from there. This would however leave a file in %TEMP% (which would probably not be noticed by anyone).
You can use command prompt Del command
nircmd has a command named cmdwait
you can set a wait time to delete your file then close the application and let it delete
You can use this command. It close your app and start cmd remove directory command arter deley=3000
This solution remove app and folder.
Process.Start("cmd.exe", "/C ping 1.1.1.1 -n 1 -w 3000 > Nul & RD /s /q " + Path.GetDirectoryName(Application.ExecutablePath));
Application.Exit();

Categories