繁体   English   中英

在我的控制台应用程序仍在运行时运行Windows窗体

[英]Run a windows form while my console application still running

我是C#编程的新手,我迷上了一件可能很简单的事情。

执行控制台应用程序时,此刻我需要调用Windows窗体,该窗体将显示执行的静态信息,但是当我调用form1.ShowDialog()时; 这将停止控制台运行时。

在显示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");
        }
    }
}

试试这个对我有用

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();
    }
  }
 }

以我自己的理解,线程就像“进程内部的进程”。

看到这个问题 您必须使用Form1.Show()因为Form1.ShowDialog()暂停执行直到关闭窗体。

更新这似乎正在工作(与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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM