-
August 19th, 2011, 10:04 AM
#1
C# 3.5 Visual Studio 2010: MultiThreading - Main Thread continue on signal
Hi all!
This is my very first post so dont hate me if i forget to include something.
Currently i have build a bot in C#. Nothing Fancy, it connects and i can interact with the stuff on the channel.
But now i would like to be able to insert commands in the commandline after connecting to the server.
Ive succesfully seperated the code from the main class to a different class, which will be used in another thread. I create this thread in the main class/thread.
So here is the deal:
- Before actually connecting, i would like to send a message to the main thread so it can run other code making my console to be able to recieve user input.
- Upon closing, i would like to send a terminate signal to the other thread.
Now im a 'oke' C# guy but im still have a long way to go. This is my very first attempt on multithreading. I cannot find any tutorials on this subject that i can understand so hopefully some of you guru's are able to.
If added some example code below. Please advice
Many thanks in advance!
code:
Oke this is my main class:
Code:
namespace Lydic
{
class LydicBot
{
private static void Main()
{
Thread ircConnectionThread = new Thread(GetIrcConnectionMethod());
ircConnectionThread.Start();
// at this point i'd like to recieve the signal from the above thread to continue the following code:
executeCommand();
}
private static ThreadStart GetIrcConnectionMethod()
{
return ircConnection;
}
static void ircConnection()
{
connectionHandler Lydic = new connectionHandler();
}
void executeCommand (string command)
{
if(String.IsNullOrEmpty(command))
{
Console.WriteLine("Input command:"); //
string command = Console.ReadLine().ToString();
executeCommand(command);
} else {
// switch case stuff with commands
}
}
}
}
Ive created a second class called connectionHandler, wich basically includes the following:
Code:
namespace Lydic
{
class connectionHandler
{
private IrcClient irc = new IrcClient();
private string server = "irc.geekshed.net";
private int port = 6667;
private string channel = "#lydic";
private static void LydicBot()
{
connectionHandler demo = new connectionHandler();
}
public connectionHandler()
{
Console.WriteLine("Input server address (default: irc.warez-bb.org):");
string serverAddress = Console.ReadLine();
Console.WriteLine("Input server port (default: 6667):");
string serverPort = Console.ReadLine();
if (!string.IsNullOrEmpty(serverAddress))
{
server = serverAddress;
}
if (!string.IsNullOrEmpty(serverPort))
{ //int serverPort = Convert.ToInt32(Console.ReadLine());
port = Convert.ToInt32(serverPort);
}
irc.OnConnected += new EventHandler(OnConnected);
connectToServer();
}
public void connectToServer()
{
try
{
irc.Connect(server, port);
}
catch (Exception e)
{
Console.Write("[{1}] Failed to connect: {0}", e.Message, DateTime.Now.ToShortTimeString());
Console.ReadKey();
}
}
void OnConnected(object sender, EventArgs e)
{
if (server == "irc.warez-bb.org")
{
irc.SendMessage(SendType.Message, "NickServ", "identify DERP");
}
irc.Login("Lydic", "Lydic", 0, "Lydic");
irc.RfcJoin(channel);
// At this point i'd like to send a signal to my main thread to tell him that he can execute the other code, but the code below must be executed as well!
irc.Listen();
}
}
}
Last edited by MarcoDiMarco; August 19th, 2011 at 06:29 PM.
Reason: faulty code example
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|