I am trying to run a series of tasks in the background of my Telnet / SSH program. sadly it seems I am not able to find out how to get this to work on the background.
I have tried using Tasks :
Task ReindexStock = new Task(delegate
{
this.Invoke(new Action(() =>
{
btnReindexStock.PerformClick();
txtBoxInput.Text = command[1];
ExecuteCommand();
}
));
});
ReindexStock.Start();
ReindexStock.Wait();
Task Product_attribute = new Task(delegate
{
this.Invoke(new Action(() =>
{
btnReindexProduct_Attribute.PerformClick();
txtBoxInput.Text = command[2];
ExecuteCommand();
}
));
});
Product_attribute.Start();
Product_attribute.Wait();
I also tried threads :
new Thread(() =>
{
btnReindexStock.PerformClick();
txtBoxInput.Text = command[1];
ExecuteCommand();
}).Start();
new Thread(() =>
{
btnReindexProduct_Attribute.PerformClick();
txtBoxInput.Text = command[2];
ExecuteCommand();
}).Start();
as well as this ( plucked this one from the net, hoped it would work ):
ThreadPool.QueueUserWorkItem(delegate
{
btnReindexStock.PerformClick();
txtBoxInput.Text = command[1];
ExecuteCommand();
});
ThreadPool.QueueUserWorkItem(delegate
{
btnReindexProduct_Attribute.PerformClick();
txtBoxInput.Text = command[2];
ExecuteCommand();
});
But for some reason my program still freezes when it's doing ExecuteCommand (
var cmd = SSH.client.CreateCommand(txtBoxInput.Text);
var result = cmd.Execute();
this.Invoke(new Action(() =>
{
rTxtBoxOutput.Text += result;
var reader = new StreamReader(cmd.ExtendedOutputStream);
rTxtBoxOutput.Text += "\n" + reader.ReadToEnd();
}
));
)
I have tried several things in the backgroundworker_doWork aswell but none of them seemed to work.. I tried to start a new thread like this
new Thread(new ThreadStart(ReindexAll)).Start();
also like this but I guess this is practically the same but larger
Thread t = new Thread(new ThreadStart(ReindexAll));
t.Start();
t.IsBackground = true;
and
Task.Factory.StartNew(() => ReindexAll());
as well as the plain and simple
ReindexAll();
but as I said before none of it seems to work, the moment I execute my command the program freezes.
Now my question is if someone is able to tell me what I am doing wrong and hopefully help me