detect if application is opened on start-up or via user?
-
Hi, I've got a program which runs on computer start-up. Is there some way to detect how the program has been opened? For example; If the user opens the program manually (clicking on the program .exe file), it creates a messagebox that says 'foo'. Whereas if the program is opened by the computer at start-up it outputs a messagebox that says 'bar'. something like:
if(openedby == user){
MessageBox.Show("foo");
}else{
MessageBox.Show("bar");
}Just a simple example with, hopefully, a simple answer! Thanks
-
Hi, I've got a program which runs on computer start-up. Is there some way to detect how the program has been opened? For example; If the user opens the program manually (clicking on the program .exe file), it creates a messagebox that says 'foo'. Whereas if the program is opened by the computer at start-up it outputs a messagebox that says 'bar'. something like:
if(openedby == user){
MessageBox.Show("foo");
}else{
MessageBox.Show("bar");
}Just a simple example with, hopefully, a simple answer! Thanks
You can send a command-line argument on start up to signal whether a user has ran it or not. ie: if i run a file like "C:\myfile.exe /startup" you would check it like this:
bool isUser = false;
string[] args = Environment.GetCommandlineArgs();
if (args[1] == "/startup")
isUser = false;
else
isUser = true;Don't be overcome by evil, but overcome evil with good
-
You can send a command-line argument on start up to signal whether a user has ran it or not. ie: if i run a file like "C:\myfile.exe /startup" you would check it like this:
bool isUser = false;
string[] args = Environment.GetCommandlineArgs();
if (args[1] == "/startup")
isUser = false;
else
isUser = true;Don't be overcome by evil, but overcome evil with good
Thanks! That sounds brilliant. I'm setting the application in the startup registry key like this:
rkStartup.SetValue("app name", Application.ExecutablePath.ToString());
can I just change it to this:
rkStartup.SetValue("app name", Application.ExecutablePath.ToString()+" /startup");
My computers old and I'm not particularly keen on restarting it to check as it will take forever ;) Thanks!
-
Thanks! That sounds brilliant. I'm setting the application in the startup registry key like this:
rkStartup.SetValue("app name", Application.ExecutablePath.ToString());
can I just change it to this:
rkStartup.SetValue("app name", Application.ExecutablePath.ToString()+" /startup");
My computers old and I'm not particularly keen on restarting it to check as it will take forever ;) Thanks!
Okay, I've pretty much got it working (I've booted up my laptop to test it on). I realised that I should surround the app name in brackets:
rkStartup.SetValue("app name", "\""+Application.ExecutablePath.ToString()+"\""+" /startup");
and it does run on start-up and does what it's supposed to do, but then it crashes :^) It's just a 'program has stopped responding message'.. So I'm looking into that now.
-
Okay, I've pretty much got it working (I've booted up my laptop to test it on). I realised that I should surround the app name in brackets:
rkStartup.SetValue("app name", "\""+Application.ExecutablePath.ToString()+"\""+" /startup");
and it does run on start-up and does what it's supposed to do, but then it crashes :^) It's just a 'program has stopped responding message'.. So I'm looking into that now.
DragenGimp wrote:
and it does run on start-up and does what it's supposed to do, but then it crashes It's just a 'program has stopped responding message'.. So I'm looking into that now.
To debug, go to your project properties and click the debug tab. In the middle you see a box saying "Command line arguments:" add /startup in that box. What that does is, when you start your debugger (f5) it will start your application with that argument. Now you'll be able to see why its crashing.
Don't be overcome by evil, but overcome evil with good
-
DragenGimp wrote:
and it does run on start-up and does what it's supposed to do, but then it crashes It's just a 'program has stopped responding message'.. So I'm looking into that now.
To debug, go to your project properties and click the debug tab. In the middle you see a box saying "Command line arguments:" add /startup in that box. What that does is, when you start your debugger (f5) it will start your application with that argument. Now you'll be able to see why its crashing.
Don't be overcome by evil, but overcome evil with good
Thanks. What happens is it just stops working and it brings up an unhandled error: object was in a zombie state now I've figured out that it doesn't crash until it reaches this line:
this.WindowState = FormWindowState.Minimized;
now I've got a check on resizing:
void MainFormResize(object sender, EventArgs e) { if(this.WindowState == FormWindowState.Minimized){ this.ShowInTaskbar = false; notifyIcon1.Visible = true; }else{ this.ShowInTaskbar = true; notifyIcon1.Visible = false; fetch\_iniData("data.ini", true); } }
For some reason it is the:
this.ShowInTaskbar = false;
That is causing it to crash... I can't see why though. The strange thing is that it's calling up the same function that is called on a button press, which works fine.. so why it works then and not when the application loads is beyond me:confused:
-
Thanks. What happens is it just stops working and it brings up an unhandled error: object was in a zombie state now I've figured out that it doesn't crash until it reaches this line:
this.WindowState = FormWindowState.Minimized;
now I've got a check on resizing:
void MainFormResize(object sender, EventArgs e) { if(this.WindowState == FormWindowState.Minimized){ this.ShowInTaskbar = false; notifyIcon1.Visible = true; }else{ this.ShowInTaskbar = true; notifyIcon1.Visible = false; fetch\_iniData("data.ini", true); } }
For some reason it is the:
this.ShowInTaskbar = false;
That is causing it to crash... I can't see why though. The strange thing is that it's calling up the same function that is called on a button press, which works fine.. so why it works then and not when the application loads is beyond me:confused:
DragenGimp wrote:
now I've figured out that it doesn't crash until it reaches this line: this.WindowState = FormWindowState.Minimized;
That is real weird. I tried duplicating this issue but i couldn't break it :sigh: I searched on google and didn't find anything useful. Hopefully, with the resize event everything is working now.
Don't be overcome by evil, but overcome evil with good
-
DragenGimp wrote:
now I've figured out that it doesn't crash until it reaches this line: this.WindowState = FormWindowState.Minimized;
That is real weird. I tried duplicating this issue but i couldn't break it :sigh: I searched on google and didn't find anything useful. Hopefully, with the resize event everything is working now.
Don't be overcome by evil, but overcome evil with good
Hi, I've been absent from my computer for a couple of days, but I seem to have fixed the problem. I still have no idea why it was crashing, but I simply added this code:
this.ShowInTaskbar = false;
before I called up the minimise function and it seems to stop it from crashing. It's a fix, but I'd still like to know why it was crashing in the first place if anyone figures it out! Thanks