简体   繁体   中英

How to launch a windows form in an application from Windows service?

I create a windows form application and a windows service, I can start my service from my windows form but if I try too launch the windows form application it doesn't work.

To launch my application I use System.Process.Start , I see in Task Manager in tab processes my windows form application name but it doesn't show my form.

public partial class testService: ServiceBase
{
    Timer tm = new Timer();
    int n = 0;

    public testService()
    {
        InitializeComponent();
    }

    protected override void OnStart(string[] args)
    {
        tm.Interval = 10000;
        tm.Elapsed += Tm_Elapsed;
        tm.Start();
    }

    private void Tm_Elapsed(object sender, ElapsedEventArgs e)
    {
        if (n == 2)
        {
            try
            {
               ProcessStartInfo info = new ProcessStartInfo(@"pathName\appName.exe");
               info.WorkingDirectory = Path.GetDirectoryName(@"pathName");
               Process.Start(info);
            }
            catch (Exception exception)
            {
                Log.writeEventLog(exception.Message);
            }

            var service = new ServiceController("testService");
            service.Stop();
        }
        n++;
    }

    protected override void OnStop()
    {
        tm.Stop();
    }
}

Someone have any idea why it doesn't work?

UPDATE

I need to do that because I have to ask with a windows form every 10min if the user is active or not.

Windows services are not allowed to interact with the desktop since Windows Vista. This was for many reasons. The checkbox is a legacy thing.

How do you launch stuff from a service then? You don't. That isn't how a service is supposed to work.

On the other hand, you can run a background process as the user to accomplish the same thing.

You can do this by starting your "service" via the registry, startup folder, or task scheduler.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM