Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. detect if application is opened on start-up or via user?

detect if application is opened on start-up or via user?

Scheduled Pinned Locked Moved C#
tutorialquestion
8 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    DragenGimp
    wrote on last edited by
    #1

    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

    T 1 Reply Last reply
    0
    • D DragenGimp

      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

      T Offline
      T Offline
      teejayem
      wrote on last edited by
      #2

      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

      D 1 Reply Last reply
      0
      • T teejayem

        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

        D Offline
        D Offline
        DragenGimp
        wrote on last edited by
        #3

        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!

        D 1 Reply Last reply
        0
        • D DragenGimp

          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!

          D Offline
          D Offline
          DragenGimp
          wrote on last edited by
          #4

          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.

          T 1 Reply Last reply
          0
          • D DragenGimp

            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.

            T Offline
            T Offline
            teejayem
            wrote on last edited by
            #5

            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

            D 1 Reply Last reply
            0
            • T teejayem

              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

              D Offline
              D Offline
              DragenGimp
              wrote on last edited by
              #6

              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:

              T 1 Reply Last reply
              0
              • D DragenGimp

                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:

                T Offline
                T Offline
                teejayem
                wrote on last edited by
                #7

                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

                D 1 Reply Last reply
                0
                • T teejayem

                  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

                  D Offline
                  D Offline
                  DragenGimp
                  wrote on last edited by
                  #8

                  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

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups