1

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 :)

2
  • 4
    Are you calling thread.Start() anywhere? Commented May 15, 2012 at 8:11
  • :-) If you're using .NET Framework 4, there's also a TaskFactory class in the System.Threading.Tasks namespace that's worth looking into. It's a very similar creature and has small trade-offs either way, but does allow a nice fluent syntax for chaining events together. Commented May 15, 2012 at 8:54

2 Answers 2

5

You need to call thread.Start() to start the thread.

On another note, it seems threadUnloadGrammar() uses a variable of type MainWindow. Not sure what MainWindow._recognizer.UnloadGrammar does exactly, but make sure it doesn't access any UI elements in there, unless it uses Control.Invoke for that.

Sign up to request clarification or add additional context in comments.

Comments

4

You need to call Thread.Start to schedule your thread for execution.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.