Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm new at C# programming and i'm lost with a thing that could be simple.

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(); this stop the console runtime.

How do i keep my console execution alive while i show a Windows form screen ?

 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");
        }
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
133 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...