Hide Console Windows

patrick

Well-known member
Joined
Dec 5, 2021
Messages
263
Programming Experience
1-3
Hello


It is a windows_task_scheduler.

1) This is a console app project. MainForm(Form) was added Here. (MainForm(Form) was added to the console project.)
2) after registering as a windows task scheduler,
3) the console_window should not appear when operating as a windows_task_scheduler.<====== When Not Environment.UserInteractive
4) If you double-click and run an exe other than the windows_task_scheduler, MainForm(Form) should appear. <====When Environment.UserInteractive


C#:
namespace Windows_Task_Scheduler
{
    class Program
    {
        static void Main(string[] args)
        {
            string mtxName = "Windows_Task_Scheduler";

            // Mutex Create
            bool createdNew;
            Mutex mutex = new Mutex(true, mtxName, out createdNew);

            if (createdNew)
            {
                if (Environment.UserInteractive)
                { // UserInteractive Windows User interface

                    // Show - Windows Forom
                    Application.EnableVisualStyles();
                    Application.SetCompatibleTextRenderingDefault(false);
                    Application.Run(new MainForm());
                }
                else
                { // IIS or Windows_Task_Scheduler



                     <==== Hide ===> Console Windows 
                     <==== Hide ===> MainForm
 

                    [B][I]<============ <==== Hide ===> Console Windows & MainForm How to do....[/I][/B]


                }
            }
            else
            {
                Console.WriteLine("Error: Program is already running");

                return;
            }

            // Mutex Release
            mutex.ReleaseMutex();
        }
    }
}


Please Help me...
 
Last edited:
C#:
namespace Windows_Task_Scheduler
{
    class Program
    {
        static void Main(string[] args)
        {
            string mtxName = "Windows_Task_Scheduler";

            // Mutex Create
            bool createdNew;
            Mutex mutex = new Mutex(true, mtxName, out createdNew);

            if (createdNew)
            {
                if (Environment.UserInteractive) <============ Environment.UserInteractive (Environment.UserInteractive is Environment.UserInteractive even if Windows_Task_Scheduler is executed.)
                { // UserInteractive Windows User interface
            // Here, User double-clicks the exe to run it
                    // Show - Windows Forom
                    Application.EnableVisualStyles();
                    Application.SetCompatibleTextRenderingDefault(false);
                    Application.Run(new MainForm());
                }
                else
                { // IIS or Windows_Task_Scheduler Run

                     <==== Hide  MainForm
 

                    [B][I]<============ <==== Hide ===>MainForm How to do....[/I][/B]


                }
            }
            else
            {
                Console.WriteLine("Error: Program is already running");

                return;
            }

            // Mutex Release
            mutex.ReleaseMutex();
        }
    }
}


it is a windows_task_scheduler.

1) This is a MainForm(Form) app project.
2) after registering as a windows_task_scheduler,
3) I want to not appear the MainForm(Form) app should not appear when operating as a windows_task_scheduler.<====== When Not Environment.UserInteractive
4) If you double-click and run an exe other than the windows_task_scheduler, MainForm(Form) should appear. <====When Environment.UserInteractive
 
When you launch via the task scheduler, it can be user interactive so your check for Environment.UserInteractive is not appropriate. You should instead pass in a command line parameter when launching via the task scheduler.

As for your console window issue, that is the default windows behavior if you register your app as a console app. If you register it as a Windows app, then it won't automatically create a console window for you.
 
When you launch via the task scheduler, it can be user interactive so your check for Environment.UserInteractive is not appropriate. You should instead pass in a command line parameter when launching via the task scheduler.

As for your console window issue, that is the default windows behavior if you register your app as a console app. If you register it as a Windows app, then it won't automatically create a console window for you.

Your Answer) When you launch via the task scheduler, it can be user interactive so your check for Environment.UserInteractive is not appropriate. You should instead pass in a command line parameter when launching via the task scheduler.
==> What is means?
 
What do you think the args is on this line of code?
C#:
static void Main(string[] args)
 
What do you think the args is on this line of code?
C#:
static void Main(string[] args)

Hello

What value should (static void Main(string[] args)) be?
Do you distinguish whether the user double-clicked the exe to run it or ran it with windows_Task_schduler?
 
Instead of asking us to explain the basics, do some reading on commandline arguments. If the user just runs an EXE directly then there are no arguments. If you write the commandline, as you can in a shortcut or Task Scheduler or Run, then you can specify whatever commandline arguments you want. It's up to you to write the application to look for the specific arguments you want and behave accordingly, then to send those arguments when you run the app.
 
Instead of asking us to explain the basics, do some reading on commandline arguments. If the user just runs an EXE directly then there are no arguments. If you write the commandline, as you can in a shortcut or Task Scheduler or Run, then you can specify whatever commandline arguments you want. It's up to you to write the application to look for the specific arguments you want and behave accordingly, then to send those arguments when you run the app.

hello

Below is the code to register in windows_task_scheduler.


C#:
class WindowsAutoStarter
{
    private const string kTASK_SCHEDULER_NAME = "My App";
    public static void SetAutoStart(bool enable)
    {
        TaskService taskService = new TaskService();
        TaskDefinition taskDefinition = taskService.NewTask();

        if (enable)
        {
            // trrigger
            taskDefinition.Triggers.Add(new LogonTrigger());
            //taskDefinition.Triggers.Add(new BootTrigger());

            // general
            taskDefinition.Principal.DisplayName = "ProgramName";
            taskDefinition.RegistrationInfo.Description = "blahblah";
            taskDefinition.Principal.UserId = string.Concat(Environment.UserDomainName, "\\", Environment.UserName);
            taskDefinition.Principal.LogonType = TaskLogonType.InteractiveToken;
            taskDefinition.Principal.RunLevel = TaskRunLevel.Highest;

            // condition
            taskDefinition.Settings.MultipleInstances = TaskInstancesPolicy.IgnoreNew;
            taskDefinition.Settings.DisallowStartIfOnBatteries = false;
            taskDefinition.Settings.StopIfGoingOnBatteries = false;
            taskDefinition.Settings.AllowHardTerminate = false;
            taskDefinition.Settings.StartWhenAvailable = false;
            taskDefinition.Settings.RunOnlyIfNetworkAvailable = false;
            taskDefinition.Settings.IdleSettings.StopOnIdleEnd = false;
            taskDefinition.Settings.IdleSettings.RestartOnIdle = false;

            // setting
            taskDefinition.Settings.AllowDemandStart = false;
            taskDefinition.Settings.Enabled = true;
            taskDefinition.Settings.Hidden = false;
            taskDefinition.Settings.RunOnlyIfIdle = false;
            taskDefinition.Settings.ExecutionTimeLimit = TimeSpan.Zero;
            taskDefinition.Settings.Priority = System.Diagnostics.ProcessPriorityClass.High;

            // operate
            taskDefinition.Actions.Add(new ExecAction(Application.ExecutablePath));

            // registration
            taskService.RootFolder.RegisterTaskDefinition(kTASK_SCHEDULER_NAME, taskDefinition);
        }
        else
        {
            Microsoft.Win32.TaskScheduler.Task t = taskService.GetTask(kTASK_SCHEDULER_NAME);
            if (t != null)
                taskService.RootFolder.DeleteTask(kTASK_SCHEDULER_NAME, true);
        }
    }
}


20240503_192251.jpg




20240503_192451.png




I attached an image.
I'm trying to substitute it as an argument as you suggested.

How do I implement <adding arguments> to windows_task_scheduler in C# code?

please help me
 
Last edited:
In your screenshot, the commandline arguments are under the Action tab so you should look at the object that represents that, i.e. the ExecAction object, and see whether it has a property that corresponds to that field.
 
Back
Top Bottom