表单中的实时控制台 [英] Real-Time Console in Form

查看:85
本文介绍了表单中的实时控制台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在开发一个程序,该程序将使运行名为minecraft的游戏的服务器的操作变得更容易.我计划通过这样做来做到这一点,以便主机需要在控制台(CMD)中键入尽可能少的内容.该程序要做的是,无需创建"* .bat"文件来启动服务器,您只需按一下按钮,程序便会在控制台中输入启动服务器所需的内容.在这种情况下,它将在控制台中添加以下行:

So I am working on a program that will make running a server for a game called minecraft easier to do. I plan to do this by making it so the host needs to type as little as possible in the console (CMD). What this program does is instead of having to create a "*.bat" file to start the server you can press a button and the program would enter the what it needs to into the console to start the server. In this case it would put the following line into the console:

"%ProgramFiles%\Java\jre6\bin\java.exe" -Xincgc -Xmx1G -jar craftbukkit-0.0.1-SNAPSHOT.jar



我想在窗体内将TextBox用作控制台的原因是为了帮助降低程序运行时控制台的关闭速度,并使之运行,以便用户可以选择是否显示控制台.我希望控制台位于TextBox内的另一个原因是,我可以尝试从程序的其他部分获取某些信息.例如,会有一个持续显示玩家人数的TextBox,在我看来,我认为我可以通过让计时器不断读取TextBox或控制台中的一行类似于"



The reason I want to have the TextBox inside the form act as the console is to help lower the rate of the console closing while the program is running, and to make it so the user has the option to show the console or not. Another reason that I would like the console to be inside the TextBox is so that I can try and get certain things out of it for other parts of the program. For example there would be a TextBox that is constantly showing the number of players, and in my mind I think I could achieve this by have a timer constantly reading the TextBox or the the console for a line that says something similar to,

"Player" has joined the server.


然后如果计时器读取它,它可以将名称添加到TextBox中,如果读取,则


and then if the timer reads it, it could add the name to the TextBox, and if it reads,

"Player" has logged out.


它将从TextBox中删除该名称.如果不可能的话,这不是我所需要的,但这将是一个不错的功能.

框架:
3.5 SP1或4 SP1(PC出于某种原因同时显示两者.)

套接字:
截至目前,我还没有使用套接字.但是将来,我可能会添加套接字,以便人们可以使用它来运行在其他PC或VPS上托管的程序.
我希望这对你们有足够的信息,以帮助我.

现在我有了这个代码:


it would remove that name from the TextBox. This is not something I will need if it is not possible, but would be a nice feature to have.

Framwork:
3.5 SP1 or 4 SP1 (PC shows both for some reason.)

Sockets:
As of right now, I am not using sockets. But in the future, I might add sockets so people can use this to run a program that is hosted off a different PC, or on a VPS.
I hope that this is enough information for you guys to help me out.

Right now I''ve got this code:

consoleOutput.Text = String.Empty
        Dim outputReader As StreamReader = Nothing
        Dim errorReader As StreamReader = Nothing
        Try
            Dim processStartInfo As ProcessStartInfo = New ProcessStartInfo(Process1.StartInfo.FileName)
            processStartInfo.ErrorDialog = False
            processStartInfo.UseShellExecute = False
            processStartInfo.RedirectStandardError = True
            processStartInfo.RedirectStandardInput = True
            processStartInfo.RedirectStandardOutput = True
            ''Execute the process
            Dim process As Process = New Process
            process.StartInfo = processStartInfo
            Dim processStarted As Boolean = process.Start
            If processStarted Then
                ''Get the output stream
                outputReader = process.StandardOutput
                errorReader = process.StandardError
                process.WaitForExit()
                ''Display the result
                Dim displayText As String
                displayText = (displayText + outputReader.ReadToEnd)
                displayText = (displayText + errorReader.ReadToEnd)
                consoleOutput.Text = displayText
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            If (Not (outputReader) Is Nothing) Then
                outputReader.Close()
            End If
            If (Not (errorReader) Is Nothing) Then
                errorReader.Close()
            End If
        End Try


但它似乎只是在关闭控制台后才更新TextBox.如果我添加代码,则冻结整个程序:


But it only seems to update the TextBox after I close the console. And just freezes the entire program if I add the code:

processStartInfo.CreateNoWindow = True




有人可以在这里帮助我吗,如果有人可以告诉我如何将命令发送到控制台,我也希望获得帮助.我还没有尝试过,但是我非常确定它无法通过使用VB.NET中已经存在的控制台命令来工作,因为所使用的控制台来自某个进程.




Can someone please help me out here, and I would also like it if someone could tell me how I can send a command out to the console. I haven''t tried it yet, but I''m pretty sure it don''t work by using the console command that is already in VB.NET because the console used is from a process.

推荐答案

首先,在课程中,您可以在Forms UI中使用常规控制台.一种方法是创建一个常规Windows应用程序(例如WPF或Forms),然后将应用程序类型更改为"Console Application".使用Visual Studio,您可以在项目的属性中找到它.

现在,命令...什么命令?如果这是一个真正的控制台,则可以使用System.Console.WriteSystem.Console.WriteLine等.

这是一回事.但是,我觉得您需要在您的表单中嵌入一个控制台.我知道没有什么比在这样的控制台上使用ListBox更方便了.请执行以下操作:找到我的CodeProject配置文件并查看我的文章.所有具有演示/测试应用程序的计算机都完全使用System.Windows.Forms和WPF的这种类型的控制台(特别是灵活的).您可以在我的演示代码中找到此类控制台的"API".

—SA
First of all, of course you can use the regular console in your Forms UI. One way of doing that is to create a regular Windows application (WPF or Forms, for example) and then change the application type to "Console Application". With Visual Studio, you will find it in the project''s Properties.

Now, commands… What commands? If this is a real console, you can use System.Console.Write and System.Console.WriteLine, etc.

This is one thing. However, I feel you need a console embedded in your form. I know nothing more convenient than using a ListBox for such console. Do the following: locate my CodeProject profile and look at my articles. All of the has demo/test application using exactly this type of console for both System.Windows.Forms and WPF (especially flexible). You will find the "API" for such console in my demo codes.

—SA


这篇关于表单中的实时控制台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆