[英]Run a windows form while my console application still running
I'm new at C# programming and i'm lost with a thing that could be simple. 我是C#编程的新手,我迷上了一件可能很简单的事情。
Executing a console application, at a moment i need to call a Windows Form that will show statics of the execution but when i call the form1.ShowDialog(); 执行控制台应用程序时,此刻我需要调用Windows窗体,该窗体将显示执行的静态信息,但是当我调用form1.ShowDialog()时; this stop the console runtime.
这将停止控制台运行时。
How do i keep my console execution alive while i show a Windows form screen ? 在显示Windows窗体屏幕时如何保持控制台执行状态?
class Program
{
static Form1 form = new Form1();
public static bool run = true;
static void Main(string[] args)
{
work();
}
public static void work()
{
form.Show();
while (run)
{
Console.WriteLine("Console still running");
}
}
}
try this it work on me 试试这个对我有用
using System.Windows.Forms;
using System.Threading;
namespace ConsoleApplication1
{
class Program
{
public static bool run = true;
static void Main(string[] args)
{
Startthread();
Application.Run(new Form1());
Console.ReadLine();
}
private static void Startthread()
{
var thread = new Thread(() =>
{
while (run)
{
Console.WriteLine("console is running...");
Thread.Sleep(1000);
}
});
thread.Start();
}
}
}
Threading is like "process inside a process" in my own understanding. 以我自己的理解,线程就像“进程内部的进程”。
See this question . 看到这个问题 。 You have to use
Form1.Show()
because Form1.ShowDialog()
pauses execution until the form is closed. 您必须使用
Form1.Show()
因为Form1.ShowDialog()
暂停执行直到关闭窗体。
Update This seems to be working (with Application.Run):- 更新这似乎正在工作(与Application.Run):-
public static Form1 form = new Form1();
public static bool run = true;
[MTAThread]
static void Main(string[] args)
{
new Thread(() => Application.Run(form)).Start();
new Thread(work).Start();
}
public static void work()
{
while (run)
{
Console.WriteLine("Console Running");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.