如何在后台运行控制台应用程序[C#]

如何在后台运行控制台应用程序[C#],c#,windows,console-application,background-process,C#,Windows,Console Application,Background Process,我想在后台运行这段代码(没有显示控制台),因此我将输出类型更改为Windows应用程序,但程序无法运行 当我将输出更改回控制台应用程序时,一切正常,但它不在后台运行。。。如果您知道任何更好的解决方案,或者您知道一个已经编写的软件可以做同样的事情,请在此处进行评论:) 当按下一个键时,代码基本上是将文本从资源复制到剪贴板 因此,我解决问题的方法不一定是在后台运行流程。这是一种完全不同的解决方案 class Program { [DllImport("user32.dll")] pu

我想在后台运行这段代码(没有显示控制台),因此我将输出类型更改为Windows应用程序,但程序无法运行

当我将输出更改回控制台应用程序时,一切正常,但它不在后台运行。。。如果您知道任何更好的解决方案,或者您知道一个已经编写的软件可以做同样的事情,请在此处进行评论:)

当按下一个键时,代码基本上是将文本从资源复制到剪贴板


因此,我解决问题的方法不一定是在后台运行流程。这是一种完全不同的解决方案

class Program
{
    [DllImport("user32.dll")]
    public static extern int GetAsyncKeyState(Int32 i);

    static void Main(string[] args)
    {

        while (true)
        {
            Thread.Sleep(100);

            for (int i = 0; i < 255; i++)
            {
                int keyState = GetAsyncKeyState(i);
                if (keyState == 1 || keyState == -32767)
                {
                    Console.WriteLine(i);
                    if (i == 88 ) // X Key
                    {
                        Console.WriteLine(i);
                        //Write what's gonning to happen when 'Y' pressed

                        break;
                    }
                    else if (i == 89) // Y Key
                    {
                        Console.WriteLine(i);
                        //Write what's gonning to happen when 'Y' pressed
                        break;
                    }
                    else if (i == 27) // Escape Key
                    {
                        Console.WriteLine(i);
                        //Write what's gonning to happen when 'Y' pressed

                        break;
                    }
                }
            }
        }
    }
}
类程序
{
[DllImport(“user32.dll”)]
公共静态外部intgetAsyncKeyState(int32i);
静态void Main(字符串[]参数)
{
while(true)
{
睡眠(100);
对于(int i=0;i<255;i++)
{
int keyState=GetAsyncKeyState(i);
如果(键状态==1 | |键状态===32767)
{
控制台写入线(i);
如果(i==88)//X键
{
控制台写入线(i);
//写下当按下“Y”时将发生什么
打破
}
else如果(i==89)//Y键
{
控制台写入线(i);
//写下当按下“Y”时将发生什么
打破
}
else如果(i==27)//转义键
{
控制台写入线(i);
//写下当按下“Y”时将发生什么
打破
}
}
}
}
}
}
以下是密钥列表的链接:
因此,请这样考虑解决方案:我们的操作系统总是检查我们是否按下了键,为什么不使用它呢?这个“user32.dll”是我们的操作系统用户界面。所以我们只需要与这个现有的UI接口

如果windows应用程序中没有控制台,则无法使用console.readkey,这就是它似乎无法工作的原因。。
class Program
{
    [DllImport("user32.dll")]
    public static extern int GetAsyncKeyState(Int32 i);

    static void Main(string[] args)
    {

        while (true)
        {
            Thread.Sleep(100);

            for (int i = 0; i < 255; i++)
            {
                int keyState = GetAsyncKeyState(i);
                if (keyState == 1 || keyState == -32767)
                {
                    Console.WriteLine(i);
                    if (i == 88 ) // X Key
                    {
                        Console.WriteLine(i);
                        //Write what's gonning to happen when 'Y' pressed

                        break;
                    }
                    else if (i == 89) // Y Key
                    {
                        Console.WriteLine(i);
                        //Write what's gonning to happen when 'Y' pressed
                        break;
                    }
                    else if (i == 27) // Escape Key
                    {
                        Console.WriteLine(i);
                        //Write what's gonning to happen when 'Y' pressed

                        break;
                    }
                }
            }
        }
    }
}