简体   繁体   English

如何在 Windows 服务的应用程序中启动 windows 表单?

[英]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. 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.要启动我的应用程序,我使用System.Process.Start ,我在选项卡中的任务管理器中看到我的 windows 表单应用程序名称,但它没有显示我的表单。

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.我需要这样做,因为如果用户处于活动状态,我必须每 10 分钟询问一次 windows 表格。

Windows services are not allowed to interact with the desktop since Windows Vista. Windows 服务不允许与桌面交互, 因为 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.您可以通过注册表、启动文件夹或任务计划程序启动“服务”来完成此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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