2

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.

5 Answers 5

0

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.

Sign up to request clarification or add additional context in comments.

2 Comments

The reason for multithreading is that i can have a main thread for in/output which is in my console and having (several) background threads performing chat related commands. I'll have a look at your suggested code. Thank you !
@Roberto: That makes sense, you might want to have a look at BackgroundWorkers, they offer a simplified mechanism for threading and could easily be integrated with the code above to provide you what you're looking for. (i.e. a non-blocking background command)
0

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?

1 Comment

Thank you for your reply as well, very grateful for that! I may have confused you cause of my bad english (which isnt my native language). I do not have a GUI, I have a console based application. It all runs in a console. But I looked at the Q&A you linked me, Its very handy and i saved it. I'm pretty sure im going to use it in another project i currently have running, so thank you very much for that!
0

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.

1 Comment

first of all: thank you for your comment. I will read that e-book. I thought of multithreading the program like you described: having a main thread for in/output which is in my console and having (several) background threads performing chat related commands. Basically i wanted to create 2 type of commands: terminalCommands and ircCommands. Where ircCommands are called from within the IRC channel and terminalCommands from within the application (console).
0

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

Comments

0

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.)

2 Comments

That would be really awesome to do, but I think it would also be a little overkill for my little IRC bot project. What would be the advantages for such setup? Except that you can hook up more then 1 UI's and that the service will restart upon machine reboot.
Hmm... I must have read your question wrong. I thought you were looking to create something with a GUI frontend, but having reread your question I don't see that anymore. For what it's worth, though, a Windows Service isn't that difficult to create and would give you some awesome extensibility in the future (if you want to eventually create a desktop GUI or web page).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.