I'm trying to communicate with a console application through a c# program.
the console application is micq a console based ICQ client.
I want to be able to send an receive messages through the micq and then act upon them.
I've been using the System.Diagnostics.Process class and redirecting the input and output stream. R
eading the output works fine, but I am not able to send any commands to the application. Or better, micq doesn't act upon the commands I send him.
See the code below for more detail.
I would be really glad, if anyone could help me. I'm goin mad already. Oh and I'm not a very good programmer, so excuse me if the code isn't neat! :)
Oh I use a new threat to read the output, because the ReadLine() command freezes if it reaches the end of the console output..
----------------------------------code----------------------------------------
Expand|Select|Wrap|Line Numbers
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- using System.Threading;
- using System.IO;
- namespace test6
- {
- public partial class Form1 : Form
- {
- static System.Diagnostics.Process p;
- static string test;
- static System.IO.StreamWriter mystreamwriter ;
- public Form1()
- {
- InitializeComponent();
- p = new System.Diagnostics.Process();
- p.StartInfo.FileName = "cmd.exe";
- p.StartInfo.UseShellExecute = false;
- p.StartInfo.RedirectStandardOutput = true;
- p.StartInfo.RedirectStandardInput = true;
- p.StartInfo.CreateNoWindow = false;
- p.StartInfo.StandardOutputEncoding = Encoding.ASCII;
- }
- ~Form1()
- {
- p.StandardInput.WriteLine("exit");
- }
- private void startbtn_Click(object sender, EventArgs e)
- {
- p.Start();
- label1.Text = "IDLE";
- mystreamwriter = p.StandardInput;
- mystreamwriter.WriteLine("micq -u ICQNUMBER -p PASSWORD -c");
- mystreamwriter.AutoFlush = true;
- }
- private void pwdbtn_Click(object sender, EventArgs e)
- {
- //p.StandardInput.WriteLine("think960");
- //mystreamwriter.Write("");
- }
- private void sendbtn_Click(object sender, EventArgs e)
- {
- mystreamwriter.WriteLine("msg 12345678 test-von-c#");
- mystreamwriter.Flush();
- mystreamwriter.Close();
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- }
- private void getoutputbtn_Click(object sender, EventArgs e)
- {
- test = "";
- getoutput workerObject = new getoutput();
- Thread workerThread = new Thread(workerObject.DoWork);
- // Start the worker thread.
- workerThread.Start();
- //test +="main thread: Starting worker thread...";
- // Loop until worker thread activates.
- while (!workerThread.IsAlive) ;
- // Put the main thread to sleep for 1 millisecond to
- // allow the worker thread to do some work:
- Thread.Sleep(5);
- // Request that the worker thread stop itself:
- workerObject.RequestStop();
- label1.Text += test;
- }
- private void getcharbtn_Click(object sender, EventArgs e)
- {
- }
- public class getoutput
- {
- // This method will be called when the thread is started.
- public void DoWork()
- {
- while (!_shouldStop)
- {
- string sOutput;
- sOutput = p.StandardOutput.ReadLine();
- test += "\n" + sOutput;
- }
- test += "worker thread: terminating gracefully.";
- }
- public void RequestStop()
- {
- _shouldStop = true;
- }
- // Volatile is used as hint to the compiler that this data
- // member will be accessed by multiple threads.
- private volatile bool _shouldStop;
- }//class getoutput
- private void button1_Click(object sender, EventArgs e)
- {
- p.StandardInput.Write("exit\n");
- p.Close();
- }
- }//Form1
- } // namespace