0

I have a windows form application which i added in windows startup program using installer package (inno setup) which is working fine and my application launches on the start up too. Now i want to trigger a function which execute another application on the startup of main/base application. I mean not every time when a form loads but only the first time when the application launch(on start up) is it possible to do so?

Harish Kumar
  • 927
  • 3
  • 20
  • 46

1 Answers1

0

EDIT: based on the OP comments and understanding, here's a simple example of how to get the last system boot up time and then storing it in a text file at the root of the app executable, next time the app is launched the DateTime value stored in the file will be used to verify if there's a need to run the method:

private void Form1_Load(object sender, EventArgs e)
{
    var lastAppStartup = GetLastAppStartup();
    var lastBootUpTime = GetLastBootUpTime();

    // If last computer boot up time is greater than last app start up time
    // Store last boot up time for next launch as app startup time
    if (lastBootUpTime.CompareTo(lastAppStartup) > 0)
    {
        AppMethodRunOnceOnStartup();
        File.WriteAllText("lastbootuptime.txt", lastBootUpTime.ToString("yyyy-MM-dd hh:mm:ss"));
    }
}

private DateTime GetLastBootUpTime()
{
    var query = new SelectQuery(@"SELECT LastBootUpTime FROM Win32_OperatingSystem");
    var searcher = new ManagementObjectSearcher(query);
    var result = DateTime.Now;

    foreach (ManagementObject mo in searcher.Get())
    {
        result = ManagementDateTimeConverter
                    .ToDateTime(mo.Properties["LastBootUpTime"].Value.ToString());
    }

    return result;
}

private DateTime GetLastAppStartup()
{
    var lastAppStartup = File.ReadAllText("lastbootuptime.txt");
    return DateTime
            .ParseExact(lastAppStartup, "yyyy-MM-dd hh:mm:ss", CultureInfo.InvariantCulture);
}

// Run this method only once when computer boot up
private void AppMethodRunOnceOnStartup()
{
    // This method runs only once based on the last system boot up time
}

Either use the [Form.Load][1] event, or from Program Main before Application.Run to launch a process (probably Program Main is what you're looking for):

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        var process = Process.Start("notepad");
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }
}
[1]: https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.form.load?view=netcore-3.1
kshkarin
  • 574
  • 4
  • 9
  • no main() is the starting initialization of the application. I want a way to detect that it runs for the first time after computer started @kshkarin – Harish Kumar Oct 08 '20 at 18:18
  • @HarishKumar do you mean you want your WinForms app to only run once? So that users can't launch multiple instances of it? – kshkarin Oct 08 '20 at 18:21
  • Like mentioned here? https://stackoverflow.com/questions/12340043/how-to-run-one-instance-of-a-c-sharp-winform-application – kshkarin Oct 08 '20 at 18:24
  • no not multiple instances, sorry if you not understand the question. in simple words i want to detect that application runs for the first time. on the restart of the machine. it could be many times a user can launch it from the start button for that i have _Load() event. but i dont want that my another function triggers every time the application starts – Harish Kumar Oct 08 '20 at 18:30
  • @HarishKumar log to Windows event log when the app is launched, now the event log will include the last boot up time and when the app started. Next step is to query the last boot up time each when your app starts and compare it to the last event log timestamp that the app logged. to log an event from the app - https://stackoverflow.com/a/27640623/4000335 to get the last boot up time - https://stackoverflow.com/a/7407346/4000335 – kshkarin Oct 08 '20 at 18:37
  • @HarishKumar please check the edit, hopefully that's what you meant from my understanding of your comments. – kshkarin Oct 08 '20 at 19:37