I would like to be able to show a form and the console and to write text from the form to the console but I'm not able to do it.
Firstly, I tried to make a console program and add System.Windows.Forms to it to create the form. It worked but when I come with the mouse on the form it seems freeze.
Second I thought it was because the form was part of the console thread and while the console thread is waiting a ReadKey
the form freeze. So I made a backgroundworker
to make a second thread where I open my form but it didn't work either.
So it's something I miss and I ask you for a little help. Here is my code so far:
using System.Windows.Forms;
using System.Threading;
namespace testform
{
class Program
{
static void Main(string[] args)
{
BackgroundWorker bgworker = new BackgroundWorker();
bgworker.WorkerSupportsCancellation = true;
bgworker.WorkerReportsProgress = true;
bgworker.DoWork += new DoWorkEventHandler(secondthread.bgworker_DoWork);
bgworker.RunWorkerAsync();
Console.Read();
}
public static class secondthread
{
private static Form myform = new Form();//create my form
private static TextBox mytxtbox = new TextBox();
public static void bgworker_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
mytxtbox.TextChanged += new EventHandler(mytxtbox_TextChanged);
myform.Controls.Add(mytxtbox);
myform.Show();
for (int i = 1; (i <= 10); i++)
{
if ((worker.CancellationPending == true))
{
e.Cancel = true;
break;
}
else
{
// Perform a time consuming operation and report progress.
System.Threading.Thread.Sleep(500);
worker.ReportProgress((i * 10));
}
}
}
private static void mytxtbox_TextChanged(object sender, EventArgs e)
{
Console.WriteLine(mytxtbox.Text);
}
}
}
}
I had to make all static 'cause I start from main.
Could you help me?