Calling a public method in window form after application.run

I have a window form that usually starts as a scheduled task, so I thought I would pass the arguments to the command in the task so that it starts automatically. That way, I could start it locally with no arguments, to manually start it if necessary. But I'm not quite sure how to get it to call a new form method after calling Application.Run when it starts as a task. Right now, it just shows the form and goes there, and then goes to the i.RunImport () line. Any ideas? Here is my code. Thank.

static void Main(string[] args)
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    if (args.Length > 0)
    {
        if (args.Any(x => x == "run=1"))
        {
            var i = new Importer();
            Application.Run(i);
            i.RunImport();
        }
    }
    else
    {
        Application.Run(new Importer());
    }
}
+5
source share
1 answer

Write an event handler for the event Form.Load:

static void Main(string[] args)
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    if (args.Length > 0)
    {
        if (args.Any(x => x == "run=1"))
        {
            var i = new Importer();
            // modify here
            i.Load += ImporterLoaded;
            Application.Run(i);

            // unsubscribe
            i.Load -= ImporterLoaded;
      }
    }
    else
    {
        Application.Run(new Importer());
    }
}

static void ImporterLoaded(object sender, EventArgs){
   (sender as Importer).RunImport();
}
+7
source

All Articles