Made a simple console application. In the properties of the project, I changed the type of the application to the Windows application - this way the application is opened hidden.

However, when you start the application, the mouse cursor in Windows changes to busy :

Cursor

There is nothing special about the Main method. In the DoWork method, methods for cleaning unnecessary files work.

static void Main() { var processes = Process.GetProcessesByName("mpCleaner"); if (processes.Length > 1) Environment.Exit(0); DoWork(); } private static void DoWork() { try { Thread.Sleep(5000); RemoveOldFunctions(); RemoveUnsupportedAutocadFunctions(); RemoveDllsFromTopDirectory(); RemovePendingOverwriteFiles(); RemoveOldDlls(); RemoveAutocadFunctionsIfHasItInSubdirectory(); } catch { // } finally { Environment.Exit(0); } } 

Is it possible to remove the cursor change? There is no need to offer a service

  • 3
    Do PeekMessage and GetMessage that the Windows could see that the process "hears" it, otherwise it thinks that it hung (this is the main core of ProcessMessages). UDP Oh, yes on c # this is most likely at stackoverflow.com/questions/34613362/… - nick_n_a
  • @nick_n_a, it says there that the equivalent on .NET is Application.DoEvents (). But I have already tried it - it does not help - Alexander Pekshev

1 answer 1

I checked - it seems to work, the wheel goes almost immediately.

 using System.Threading; using System.Windows.Forms; public class Demo { static void Main() { bool Working = true; new Thread(() => { Thread.Sleep(20000); // Вписать код вашей програмы Working = false; }).Start(); while (Working) { Application.DoEvents(); Thread.Sleep(500); } } } 

PS There is not enough try-catch.

It is necessary that the call to PeekMessage (without waiting) and GetMessage (with message cleaning) occur regularly, at least once a second, in order for the Windows to see that the process is “heard”, otherwise it considers that the GUI has hung (this is the main core of ProcessMessages in c # is the equivalent of Application.DoEvents(). ). And, yes, if your application is not a console, then .... it is a GUI. (Console, GUI, Service, Driver options - I think everything listed).

  • Yes, it became so much better. However, the "hang" icon still appears when the application is launched. Only now for one second. Apparently this can not be avoided - Alexander Pekshev
  • There is also a prikolol c # from "microsoft" - the time between the launch of exe, and the transfer of control from Мain can be from 1 to 10-20 seconds. (I used to think that it starts right away). I have the 1st launch - about 10 seconds, the “watch” hangs, and then, the 2.3rd launch starts - 1-2 seconds each. If to rewrite on with ++ - it will be faster;) - nick_n_a