I’m quite new to multithreading and today I ran into this strange problem. I followed some online tutorials and it seemed to me that what I’m doing is the correct.
So I got this code:
GrammarThreading gThread = new GrammarThreading(this, grammar);
Thread thread = new Thread(new ThreadStart(gThread.threadUnloadGrammar));
with it I want to move grammar unloading to another thread, since it takes a couple of seconds. This is how the class of GrammarThreading looks like:
public class GrammarThreading
{
public MainWindow window { get; set; }
public Grammar grammar { get; set; }
public GrammarThreading(MainWindow _window, Grammar _grammar)
{
window = _window;
grammar = _grammar;
}
public void threadUnloadGrammar()
{
window._recognizer.UnloadGrammar(grammar);
}
}
However, I debugged the code and threadUnloadGrammar() method seems never to be called. I’ve got no idea what could the problem be so any help will be greatly appreciated. Thanks 🙂
You need to call
thread.Start()to start the thread.On another note, it seems
threadUnloadGrammar()uses a variable of typeMainWindow. Not sure whatMainWindow._recognizer.UnloadGrammardoes exactly, but make sure it doesn’t access any UI elements in there, unless it usesControl.Invokefor that.