简体   繁体   English

如何使用c#同时运行表单和控制台?

[英]How to run form and console at the same time with c#?

Hey I am fairly new to the c# programming language and have enjoyed my experience so far. 嘿,我是c#编程语言的新手,并且到目前为止享受了我的经验。 I normally develop applications in java but a need for the win32 library has lead me to c# so far the languages seem relatively similar. 我通常在java中开发应用程序,但是对win32库的需求导致我使用c#到目前为止语言似乎相似。 however a problem I am having at the moment is a simple piece of code that can be best explained in this example 但是我目前遇到的一个问题是一段简单的代码,在这个例子中可以得到最好的解释

I am trying to print a piece of string to console then display the windows form then print another piece of string to console. 我正在尝试将一段字符串打印到控制台然后显示窗体然后打印另一段字符串到控制台。 however the result i get is first string is printed then the form is displayed, i then have to close the form before the last string is printed. 但是我得到的结果是打印第一个字符串然后显示表单,然后我必须在打印最后一个字符串之前关闭表单。 the question i would like to ask is is there anyway to get this working so the second print to console is displayed immediately after the form is displayed. 我想问的问题是,无论如何都要使其工作,以便在显示表单后立即显示第二个打印到控制台。 im guessing it has something to do with threading but I am not entirely sure how to implement this 我猜测它与线程有关,但我不完全确定如何实现它

The solution is quite simple: Just create a Windows Forms Application (when creating the project - which you probably did) and then go to Properties (in context menu "Project", it's the last item) and set "Output type" to "Console Application". 解决方案非常简单:只需创建一个Windows窗体应用程序(在创建项目时 - 您可能已经执行过),然后转到属性(在上下文菜单“项目”中,它是最后一项)并将“输出类型”设置为“控制台”应用”。 The forms will work as before and furthermore the application will now open the console too. 表单将像以前一样工作,此外应用程序现在也将打开控制台。 You can then freely write to or read from the console. 然后,您可以自由地写入控制台或从控制台读取。

It sounds like you want to be able to output messages to the console while the form is being displayed, correct? 听起来您希望能够在显示表单时将消息输出到控制台,对吗? The basic issue is that as long as the form is visible, there must be a message loop running, handling events for the form. 基本问题是,只要表单可见,就必须运行一个消息循环,处理表单的事件。 The message loop is inside Application.Run, so once you call it, it won't return until the form is closed (as you discovered). 消息循环在Application.Run中,因此一旦调用它,它将不会返回,直到窗体关闭(如您所发现的)。

So if you want to write to the console while the form is visible, you have a couple of options. 因此,如果您希望在窗体可见时写入控制台,则可以使用多种选项。 One, as you mentioned, is to use multiple threads. 正如您所提到的,一个是使用多个线程。 Let your main thread run the message loop, and start up a second thread to write to the console. 让你的主线程运行消息循环,并启动第二个线程来写入控制台。 But that's not necessary--you can also write to the console from within an event handler, directly or indirectly. 但这不是必需的 - 您也可以直接或间接地在事件处理程序中写入控制台。 There's nothing wrong with doing a Console.WriteLine from within a button click handler. 从按钮单击处理程序中执行Console.WriteLine没有任何问题。 Or you can have your button handler call a method in your Program class, and do the writing there. 或者你可以让你的按钮处理程序在你的Program类中调用一个方法,然后在那里写。

As for which solution is better, it would help to know more about what you're trying to accomplish. 至于哪种解决方案更好,有助于更多地了解您要完成的任务。 I assume that you don't just want to write stuff to the console--what else is it that you want to do while the form is being displayed? 我假设您不只是想将内容写入控制台 - 在显示表单时您还想做什么?

My suggestion would be to start with a Console application. 我的建议是从控制台应用程序开始。 You can always open a WinForm from a console application...which would give you exactly what you're looking for. 您始终可以从控制台应用程序中打开WinForm ...这将为您提供您正在寻找的内容。 (you might want to think about multi-threading as well. (您可能也想考虑多线程。

static class Program
{
    [STAThread]
    static void Main()
    {
        Console.WriteLine("first string");
        var form = new Form1();
        form.Show();
        Console.WriteLine("the second string");
        Application.Run();
    }
}

Hey everyone thanks for your answers I made some progress with what im trying to achieve but im not sure how correct or thread safe it is here is the code i got to run 嘿大家感谢您的回答我在尝试实现的目标方面取得了一些进展,但我不确定它的正确性或线程安全性是我运行的代码

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Windows.Forms;

    namespace Project1
    {
    class Class2
    {

        [STAThread]
        static void Main()
        {
           Console.WriteLine("hello");
           Class2 t = new Class2();
           t.test();
           Console.WriteLine("second string");
        }



        public void test()
        {
            Thread t = new Thread(new ThreadStart(StartNewStaThrea));
            t.Start();
        }


        private void StartNewStaThrea()
        { 
            Application.Run(new Form1()); 
        }

    }
}

pls let me know what you think of this solution 请告诉我您对此解决方案的看法

are you using form.showDialog() or form.show() ? 你在使用form.showDialog()form.show()吗?

form.showDialog() will block until the form is closed, while form.show() will not. form.showDialog()将阻塞,直到窗体关闭,而form.show()不会。

I would either start with a console app, and run the application from the static main function. 我要么从一个控制台应用程序开始,并从静态main函数运行该应用程序。 This is from memory - I haven't had to mess with Winforms for years: 这是来自内存 - 多年来我一直没有弄乱Winforms:

[STAThread]
static void Main()
{
  Application.Run(new YourMainForm()); 
}

or 要么

I would redirect Console.Out and shove that stream into a some sort of control like a text box or list box. 我会重定向Console.Out并将该流推送到某种控件,如文本框或列表框。 This is a lot of code but doable... I have written this before for on-site debugging but don't have the code handy. 这是很多代码,但可行...我之前已经写过这个用于现场调试,但没有代码方便。

Why don't you just create a new windows form with multiline textbox and dock it to the form, set BackColor as Black ForeColor as White and create a public method to send text to that textBox. 为什么不用多行文本框创建一个新的窗体并将其停靠在窗体上,将BackColor设置为Black ForeColor为White,并创建一个公共方法将文本发送到该文本框。

If your aim is a one Form application this works pretty much brilliantly. 如果您的目标是单一表格应用程序,那么它的工作非常出色。

I know this is a super-old question, but I'm going to answer in hopes of karma. 我知道这是一个超级老问题,但我会回答希望业力。 One interesting work-around that works well for this comes up if you're using Visual Studio (which is a fairly safe assumption). 如果您使用的是Visual Studio(这是一个相当安全的假设),那么可以使用一个有趣的解决方法。 If you start a forms project in C# in Visual Studio, then make your appropriate forms, but then change the project type to a console application (by right clicking the project-->properties-->Console Application), it will still spawn your forms, but will also keep a Console open, now all you need to do is put one Console.Write statement before your form is created, and the other after, and maybe add a flush statement! 如果您在Visual Studio中开始的C#形式的项目,然后进行适当的形式,但随后(单击该项目通过右键- >属性- >控制台应用程序), 更改项目类型的控制台应用程序,它仍然会酿出你表单,但也将保持控制台打开,现在你需要做的就是在创建表单之前放置​​一个Console.Write语句,然后在另一个之后放置一个,并且可能添加一个flush语句!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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