簡體   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