如何同时运行控制台和窗体?c#
我一直在努力寻找解决办法,但这两种方法都没有奏效。 我想让程序同时打开Windows窗体和控制台应用程序。但每当我输入这样的代码时:如何同时运行控制台和窗体?c#,c#,.net,winforms,testing,console,C#,.net,Winforms,Testing,Console,我一直在努力寻找解决办法,但这两种方法都没有奏效。 我想让程序同时打开Windows窗体和控制台应用程序。但每当我输入这样的代码时: static void Main(string[] args) { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); NativeMethods.AllocConsole(); Checklogs.LogL
static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
NativeMethods.AllocConsole();
Checklogs.LogLogic();
var form = new Form1();
NativeMethods.FreeConsole();
Application.Run(new Form1());
form.Show();
}
它只显示控制台。但是当我删除Checklogs.LogLogic的代码时。
它显示了一切。有人知道解决办法吗?
Checlogs代码:
public static void LogLogic()
{
int l = 0;
int t = 0;
while (true)
{
//read
for (l = 65; l < 91; l++)
{
t = GetAsyncKeyState(l);
if (t == -32767 && GetAsyncKeyState(0x14) != 0x14)
{
Console.WriteLine((char)(l + 32));
}
else if (t == -32767 && GetAsyncKeyState(0x14) == 0x14)
{
Console.WriteLine((char)l);
}
}
}
}
publicstaticvoidloglogic()
{
int l=0;
int t=0;
while(true)
{
//阅读
对于(l=65;l<91;l++)
{
t=GetAsyncKeyState(l);
如果(t==-32767&&GetAsyncKeyState(0x14)!=0x14)
{
控制台写入线((字符)(l+32));
}
else if(t==32767&&GetAsyncKeyState(0x14)==0x14)
{
控制台写入线((字符)l);
}
}
}
}
如果您遵循程序流程,它会卡在while(true)循环中。Checklogs.LogLogic()代码>是一个阻塞调用。执行不会到达var form=new Form1()
直到LogLogic
返回,由于无限while
循环,它从未返回
如果您不假装了解当前程序的细节,或者您的整个设置是一个好主意(乍一看似乎不是),那么如果您在调用LogLogic
之前简单地显示表单(非阻塞),您可能会得到更接近预期的结果:
...
var form = new Form1();
NativeMethods.FreeConsole();
Application.Run(new Form1());
form.Show();
Checklogs.LogLogic();
我不知道你想做什么,但是这个怎么样:
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
if (NativeMethods.AllocConsole()) //allocconsole
{
var th = new Thread(CommunicateWithConsole); //create a new thread and pass our endless running method, as to not block the UI Thread
th.Start();
}
//register to ApplicationExit if you want to free the Console when this happens
Application.ApplicationExit += Application_ApplicationExit;
//show Form1
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
private static void Application_ApplicationExit(object sender, EventArgs e)
{
NativeMethods.FreeConsole();
}
//our method to check the console and write to it
private static void CommunicateWithConsole()
{
try
{
while (true)
{
var x = Console.ReadLine(); //stay here till console returns a line, can be changed to Console.ReadKey()
if (x == "Hello WinForms") //if line says this
Console.WriteLine("Hello ConsoleWindow"); //output this to console, waring only outputs to your console if application is not run with a debugger
}
}
catch (IOException e)
{
//when we close our app and call freeconsole an IOException can occurr, handle that case
}
}
}
public partial class NativeMethods
{
[System.Runtime.InteropServices.DllImportAttribute("kernel32.dll", EntryPoint = "AllocConsole")]
[return: System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)]
public static extern bool AllocConsole();
[System.Runtime.InteropServices.DllImportAttribute("kernel32.dll", EntryPoint = "FreeConsole")]
[return: System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)]
public static extern bool FreeConsole();
}
静态类程序
{
///
///应用程序的主要入口点。
///
[状态线程]
静态void Main()
{
if(NativeMethods.alloconsole())//alloconsole
{
var th=new Thread(CommunicateWithConsole);//创建一个新线程并传递我们的无休止的运行方法,以免阻塞UI线程
th.Start();
}
//如果要在发生这种情况时释放控制台,请注册到ApplicationExit
Application.ApplicationExit+=Application\u ApplicationExit;
//显示表格1
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(新Form1());
}
私有静态无效应用程序\u ApplicationExit(对象发送方,事件参数e)
{
NativeMethods.FreeConsole();
}
//检查控制台并写入控制台的方法
专用静态void CommunicateWithConsole()
{
尝试
{
while(true)
{
var x=Console.ReadLine();//留在这里,直到Console返回一行,可以更改为Console.ReadKey()
if(x==“Hello WinForms”)//如果行中这样说
WriteLine(“Hello ConsoleWindow”);//将此输出到控制台,如果应用程序未使用调试器运行,则仅警告输出到控制台
}
}
捕获(IOE异常)
{
//当我们关闭应用程序并调用freeconsole时,可能会发生IOException,请处理该情况
}
}
}
公共部分类NativeMethods
{
[System.Runtime.InteropServices.DllImportAttribute(“kernel32.dll”,EntryPoint=“AllocConsole”)]
[返回:System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)]
公共静态外部bool alloconsole();
[System.Runtime.InteropServices.DllImportAttribute(“kernel32.dll”,EntryPoint=“FreeConsole”)]
[返回:System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)]
公共静态外部bool FreeConsole();
}
你能解释一下你想做什么吗?这不会阻止现在在Checklogs.LogLogic()上的UI线程吗代码>用户界面将无响应?@RandRandom是的,这就是为什么我说整个设置是错误的。但他在问为什么表格没有显示出来,这就是我的回答。@bial654321如果不理解你想表达什么,很难说;查看您当前的代码一点也不清楚。解释您正在尝试做什么,而不是您糟糕的实现有什么问题()