I finished my little project that does some heavy lifting. i realized in this short calculation time, my GUI freezes. So I did some research and I found this => http://www.codeproject.com/Articles/4381/Threading-out-tasks-in-a-C-NET-GUI
I started to implement this is my project, but i realized that this particular implementation does not work in my project.
In my project i have many classes and one ” manager ” that controls all other classes. If i initilize this Manager class , it already does the heavy lifting in the constructor.
To my Question :
How do i start a new thread with a contructor ?
private void fileWatcher_Changed(object sender, System.IO.FileSystemEventArgs e)
{
if (System.IO.File.Exists(e.FullPath) == true)
{
Manager mgr = new Manager(e, handreader); // here starts the heavy lifting
Thread mgrThread = new Thread(new ThreadStart(mgr)); // what to do ?
sl.Text = mgr.test();
txtLog.Text = mgr.output();
}
}
EDIT :
okay i decided to recode my program. now the heavy lifting is in one function but i think i made a mistake.
the whole program looks like this :
private void fileWatcher_Changed(object sender, System.IO.FileSystemEventArgs e)
{
if (System.IO.File.Exists(e.FullPath) == true)
{
Manager mgr = new Manager(e, handreader, txtLog, sl);
//sl.Invoke(new MethodInvoker(mgr.test));
sl.Invoke(new MethodInvoker(mgr.test)); // first try
Thread mgrThread = new Thread(new ThreadStart(mgr.test)); // second try
}
}
the sl.Invoke(new MethodInvoker(mgr.test)); // first try works but it still freezes my GUI.
Thread mgrThread = new Thread(new ThreadStart(mgr.test)); // second try
and this line does nothing.
my test function :
public void test()
{
StringBuilder builder = new StringBuilder();
foreach (PlayerController pc in fm.lPc)
{
Range range = new Range(handReader.hand, handReader.handversus, pc);
builder.Append(pc.getHeroCardsSimple()+" vs 100% range = "+range.vsRange()+"\r\n");
}
sl.Text = builder.ToString();
}
You should use a different approach for this. Your constructor is still being invoked on the GUI thread.