如何 运行 在后台控制应用程序 [C#]
How to run console app in background [C#]
我想在后台 运行 这段代码(不显示控制台),所以我将输出类型更改为 Windows Application 但随后该程序无法运行。
当我将输出更改回 控制台应用程序 时,一切正常,但它不会在后台 运行ning... 如果你知道更好的话解决方案,或者你知道一个已经编写好的软件可以做同样的事情,请在这里评论:)
该代码基本上是在按下某个键时将文本从资源复制到剪贴板。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Resources;
using System.Reflection;
namespace EzClipBoard
{
class Program
{
[STAThreadAttribute]
static void Main(string[] args)
{
int exit = 1;
string bubblesort = codes.ResourceManager.GetString("bubblesort");
string insertion = codes.ResourceManager.GetString("insertion");
while(exit != 0)
{
if (Console.ReadKey(true).Key == ConsoleKey.X)
{
Clipboard.SetText(bubblesort);
}
else if (Console.ReadKey(true).Key == ConsoleKey.Y)
{
Clipboard.SetText(insertion);
}
else if (Console.ReadKey(true).Key == ConsoleKey.Escape)
{
exit = 0;
}
}
}
}
}
所以我解决您的问题的方法不一定是 运行 后台进程。只是一种不同的解决方法。
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;
}
}
}
}
}
}
这里是键列表的 link:
https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.keys?redirectedfrom=MSDN&view=netframework-4.7.2
所以以这种方式思考解决方案:我们的 OS 总是在检查我们是否按下了键,所以为什么不使用它呢?这个 "user32.dll" 是我们的 OS 用户界面。所以我们只是与这个存在的接口 UI。
我想在后台 运行 这段代码(不显示控制台),所以我将输出类型更改为 Windows Application 但随后该程序无法运行。
当我将输出更改回 控制台应用程序 时,一切正常,但它不会在后台 运行ning... 如果你知道更好的话解决方案,或者你知道一个已经编写好的软件可以做同样的事情,请在这里评论:)
该代码基本上是在按下某个键时将文本从资源复制到剪贴板。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Resources;
using System.Reflection;
namespace EzClipBoard
{
class Program
{
[STAThreadAttribute]
static void Main(string[] args)
{
int exit = 1;
string bubblesort = codes.ResourceManager.GetString("bubblesort");
string insertion = codes.ResourceManager.GetString("insertion");
while(exit != 0)
{
if (Console.ReadKey(true).Key == ConsoleKey.X)
{
Clipboard.SetText(bubblesort);
}
else if (Console.ReadKey(true).Key == ConsoleKey.Y)
{
Clipboard.SetText(insertion);
}
else if (Console.ReadKey(true).Key == ConsoleKey.Escape)
{
exit = 0;
}
}
}
}
}
所以我解决您的问题的方法不一定是 运行 后台进程。只是一种不同的解决方法。
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;
}
}
}
}
}
}
这里是键列表的 link: https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.keys?redirectedfrom=MSDN&view=netframework-4.7.2 所以以这种方式思考解决方案:我们的 OS 总是在检查我们是否按下了键,所以为什么不使用它呢?这个 "user32.dll" 是我们的 OS 用户界面。所以我们只是与这个存在的接口 UI。