-3

I have winform c# application running on task scheduler (it runs every 2 minutes).

I am using application.exit on form_load event after reading a file and insert in database.

It works fine but somehow after 2 days of running a scenario occurs where .exe remain running on task manager but program exited successfully since I am logging in text file.

static void Main()
{
  Application.SetHighDpiMode(HighDpiMode.SystemAware);
  Application.EnableVisualStyles();
  Application.SetCompatibleTextRenderingDefault(false);
  Application.Run(new Form1());
  Form1 formObj = new Form1();
  formObj.log(string.Empty, string.Empty, "Application exited successfully");
}

I have this log method in form1.cs

It gives Launch request ignored, instance already running Event Id:322

LarsTech
  • 80,625
  • 14
  • 153
  • 225
  • 4
    Why are you using a WinForms app in the first place? Why not just use a Console app? – John Sep 15 '22 at 04:58
  • Actually I was showing the status of insert on the form before – user3557716 Sep 15 '22 at 04:59
  • Does this answer your question? [Why would Application.Exit fail to work?](https://stackoverflow.com/questions/554408/why-would-application-exit-fail-to-work) – styx Sep 15 '22 at 05:04
  • No, I checked this thread already. – user3557716 Sep 15 '22 at 05:07
  • I am using debug complied version on task scheduler instead of release , I Hope that won't be an issue – user3557716 Sep 15 '22 at 05:09
  • Handle the `Form1.FormClosed` event to `log`. Don't create anything after `Application.Run(...)`. I'd consider the first comment. – dr.null Sep 15 '22 at 05:18
  • You need to do some more research and explicitly exit the app, there are known issues, eg: https://stackoverflow.com/q/18036863/495455 https://stackoverflow.com/q/32536145/495455 – Jeremy Thompson Sep 15 '22 at 05:28
  • A possible problem could be some thread that is keeping your process alive. Ensure that all started tasks and threads are properly shutdown, and attach a debugger to see all the threads that might be doing something. Or use Environment.Exit to just kill all threads. – JonasH Sep 15 '22 at 07:56

1 Answers1

0

A complete exit from the application can be done by using Environment.Exit(0);