Alex Rivera | Logout

Does BeginInvoke() run a separate thread?

Asked 2011-04-07T08:06:48.020
9

In my WPF application, I want to do some work in a non-UI thread so as to avoid the UI from become not-responding. For that I did this:

var caller = new AsyncMethodCaller<Pattern>(this.SetPatternType);
caller.BeginInvoke(_patterns, null, null);

And the delegate is defined as,

public delegate void AsyncMethodCaller<in T>(IEnumerable<T> data);

My question is:

Does BeginInvoke() create a new thread and the callback SetPatternType runs in it? If so, how long this thread last?

Is this approach good in general? If not, what is wrong with it? And what potential problem(s) might I face?

I'm using C# 4.0 and Visual Studio 2010.


EDIT:

Also I need few guidelines regarding these:

When I should create a new thread myself and when should I make use of BeginInvoke()? And when should I use DispatcherObject.Dispatcher.BeginInvoke() object?

Edit
Report

1 Answer

9

It uses the thread pool - so it won't necessarily create a new thread, but it runs in a different thread to the calling thread (unless that's a thread pool thread itself which happens to finish its task before the delegate invocation is scheduled; it would be pretty unlikely to use the same thread).

answered 2011-04-07T08:11:31.443

Your Answer