可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I have been working with C# for a few projects, nothing big or something. So I want to start working on a real project now. I've created a IRC bot in C#.
This IRC bot runs without a GUI in a commandline. Now when it runs, it outputs every command and stuff.
What i want to do is create a interface for the application inside the commandline.
So that im able to type commands into the application and output the results of the command.
I've tried reading several topics on multi-threading my software cause i believe this is needed for me to achieve this functionality. But i dont know for sure.
I've read threads like Command-line interface in Java and http://msdn.microsoft.com/en-us/library/aa288457(v=vs.71).aspx. But im still very much confused.
I've tried googling on the matter for any toolkits like http://alexis.royer.free.fr/CLI/
I would like to ask for advice on this matter and, if possible, help on the basics for this program. If someone strongly suggest me not to search for this functionality within C# then i would be happy to hear his/her opinion on why not and advice on what i should use/do.
Many thanks in advance!
Roberto.
P.s. My apologies for my incorrect English grammar.
回答1:
Here's a very simple command line, with an example of how to do different colours and handle arguments:
using System;
using System.Linq;
namespace SOSimpleCLI
{
class Program
{
static void Main(string[] args)
{
string cmdLine = null;
while (cmdLine != "end")
{
Console.ForegroundColor = ConsoleColor.Green;
Console.Write("SOSimpleCLI: ");
Console.ForegroundColor = ConsoleColor.White;
cmdLine = Console.ReadLine();
string[] cmd = cmdLine.Split(' ');
switch (cmd.FirstOrDefault())
{
case "cmd1":
Console.WriteLine(Cmd1());
break;
case "cmd2":
if (cmd.Length != 2)
{
Console.WriteLine("Wrong number of args for cmd2");
}
else
{
Console.WriteLine(Cmd2(cmd[1]));
}
break;
}
}
}
static string Cmd1()
{
return "Came from command 1";
}
static string Cmd2(string arg)
{
return string.Format("Came from command 2: {0}", arg);
}
}
}
I'm not sure why you want it to be multithreaded, but you could add that in by interacting with the threads inside the different methods.
回答2:
In Windows, a process image can startup either in GUI mode (with windows on-screen... or nothing at all) or as a "Console" process (so a command window will be opened at the same time as the process). However the two are not mutually exclusive: a "Console" process can create windows, and a GUI process can create and own its own command-line windows. However a GUI application that creates a console window will not open that console in any parent console session.
From what I understand of your question you have an existing GUI application and you want to enable some kind of console (like the Quake console) to your application rather than convert it into a console-only program.
This can be done using the AllocConsole function in Win32. I suggest you read this Q&A: How do I include a console in Winforms?
回答3:
If you're confused about whether or not you need to multi-thread your application, I'd recomend first reading a little bit about the advantages & disadvantages of threading. The first chapter of Joe Albahari's web book is a great introduction.
Without understanding the desgin of your program fully, I would recomend that you perform all of the Chat bot behavior on a background thread. Additionally, any commands you type into the console should also trigger background work, which leaves the console free to either print output or accept input, but not perform any of the actual chat duties. Please let me know if you have any questions.
回答4:
For parsing Commands you really don't need to use threading here is some information on the best way to accomplish this
There are several command line parsers over @ Codeplex.com. They are going to be very complex so depending on how much commandline parsing you are wanting to do you can use those.
I would just start out with using string.substring
for CONNECT [ []]
string command = "Connect irc://QuakeNet"
int end = command.IndexOf(" ");
string cmd = command.substring(0,end);
switch(cmd.ToLower())
{
case "connect":
string site = command.SubString(end + 1, cmd.Length -1)
//Do what you need to do to connect to the site
break;
}
if you really get to the point you need to use threading you can always use a backgroundworker thread
Tutorial on how to use a background thread
回答5:
Have you considered creating a Windows Service? The Service would contain all of your code which "does stuff". You can then issue commands to the service (via ExecuteCommand).
You could then create multiple UIs, including console and GUIs, which simply call into the service and then receive information back. This would enable you to issue commands via a standard command line window and see the results in a GUI without the complication of trying to manage both windows from inside the same project.
(This would also give you a multi-process application without having to explicitly manage the processes yourself.)