To achieve the following sequence:
//1. Code until this position
//2. Code to run in a new thread
//3. Code that runs after thread is started
Just do the following:
Use the class ParameterizedThreadStart and pass the "code snippet" as an Action as parameter.
An example looks like this:
Create two methods:
public void RunMethodOnThread(Action action)
{
Thread thread1 = new Thread(new ParameterizedThreadStart(ExecuteInThread));
thread1.Start(action);
}
private void ExecuteInThread(Object a)
{
Action action = a as Action;
action.Invoke();
}
After that you can use the following call in your code, by passing a so called delegate function as an Parameter:
//1. Code until this position
RunMethodOnThread(delegate
{
//2. Code to run in a new thread
});
//3. Code that runs after thread is started
I know the solution seems a little bit complex, but by going this way you can achieve a very good reuse of your code and it is really easy to use it, especially if you have a lot of small "code snippets" that should run in a new thread.