Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Starting a Windows application in the background

Tags:

c#

.net

winforms

I have a Windows application written in C# using winforms. I want to make sure that whenever someone starts it from any other application using process.start the UI is not shown and the application should start silently.

I do not have control over other applications so I can not use:

var p = new Process();
p.StartInfo = new ProcessStartInfo(); 
p.StartInfo.UseShellExecute = true; 
p.StartInfo.WorkingDirectory = ConfigurationManager.AppSettings["exeFolder"].ToString(); p.StartInfo.WindowStyle = ProcessWindowStyle.Normal; 
p.StartInfo.FileName = ConfigurationManager.AppSettings["exeName"].ToString(); 
p.Start();
like image 562
Any Avatar asked Dec 06 '25 08:12

Any


1 Answers

Thanks for your replies. I am sorry to not have provided my solution earlier. I have resolved the problem and have documented the solution for someone else to use in future.

You can find the solution here.

How To · Create a new windows project and delete the default form (Form1). · In Program.cs create a new class and inherit it from Form. · Please refer the code below. · Now change the Main method. In Application.Run change the startup object from Form1 to ApplicationStartUp.

using System;
using System.Drawing;
using System.Windows.Forms;
using BackgroundApp.Properties;

namespace BackgroundApp
{
    static class Program
    {
        /// <summary>


   /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new ApplicationStartUp());
    }
}

public class ApplicationStartUp : Form
{
    private NotifyIcon trayIcon;
    private ContextMenu trayMenu;

    private void InitializeComponent()
    {
        trayMenu = new ContextMenu();
        trayMenu.MenuItems.Add("Exit", OnExit);
        trayIcon = new NotifyIcon();
        trayIcon.Text = Resources.TrayIcon;
        trayIcon.Icon = new                            Icon(global::BackgroundApp.Properties.Resources.IntegratedServer, 40, 40);
        trayIcon.ContextMenu = trayMenu;
        trayIcon.Visible = true;
    }

    //Ctor
    public ApplicationStartUp()
    {
        InitializeComponent();
    }

    protected override void OnLoad(EventArgs e)
    {
        Visible = false;
        ShowInTaskbar = false;
        base.OnLoad(e);
    }

    private void OnExit(object sender, EventArgs e)
    {
        // Release the icon resource.
        trayIcon.Dispose();
        Application.Exit();
    }

    protected override void Dispose(bool isDisposing)
    {
        if (isDisposing)
        {
            // Release the icon resource.
            trayIcon.Dispose();
        }
        base.Dispose(isDisposing);
    }
}
like image 87
Any Avatar answered Dec 08 '25 20:12

Any