首页 > 解决方案 > 我想使用 WinForms C# 修改由 Windows 服务执行的对象

问题描述

我有一个运行服务的程序,如下面的代码:

public static void runService()
{
    var exitCode = HostFactory.Run(x =>
    {
        x.Service<StayingAlive>(s =>
        {
            s.ConstructUsing(stayingAlive =>
            {
                sa = new StayingAlive();
                return sa;
            });
            s.WhenStarted(stayingAlive => stayingAlive.Start());
            s.WhenStopped(stayingAlive => stayingAlive.Stop());
        });

        x.RunAsLocalSystem();
        x.SetServiceName("SimpleService");
        x.SetDisplayName("SimpleService");
        x.SetDescription("This is the simple service");
    });
    int exitCodeValue = (int)Convert.ChangeType(exitCode, exitCode.GetTypeCode());
    Environment.ExitCode = exitCodeValue;
}

此方法在主 .cs 文件中调用。然后,我有了StayingAlive具有许多属性和方法的类。

现在,我想同时执行一个 WinForm 以便使用 Button 修改类中的一个属性StayingAlive()

有没有可能?我试过使用Tasks 或Threads 但我卡住了。

标签: c#multithreadingwinformswindows-servicestask

解决方案


推荐阅读