2

I have a method which I instantiate as follows:

new DatabaseHarness.MemoryStressTest().ExecuteTest("Thread 1",1,1,1)

What is the standard way to make this call via a thread, passing the parameters as shown above, using the threading capabilities of .NET 4?

Thanks very much

3 Answers 3

3

How about:

Thread thread = new Thread(() => {
    new DatabaseHarness.MemoryStressTest().ExecuteTest("Thread 1",1,1,1);
});
thread.Start();
Sign up to request clarification or add additional context in comments.

2 Comments

Since this uses a lambda for the ThreadStart, if you were to change from hardcoded parameter values ("Thread 1", 1, etc.) to variables (threadName, processId, etc.), it would complicate things, correct? So you would want to create local variables within the scope of the lambda to pass in as the parameters?
@Joel - sure, but without variables in the question it is hard to show correct parameterisation in the answer, in particular when talking about captured variables.
2

The .NET 4.0 way of doing it is to use a Task.

var task = Task.Factory.StartNew(
  () => 
  {
    new DatabaseHarness.MemoryStressTest().ExecuteTest("Thread 1", 1, 1, 1);
  },
  TaskCreationOptions.LongRunning
);

Comments

0

There are at least two ways of doing it. Check this link for more info: http://www.dotnetspider.com/resources/4698-Making-Parameterized-reads-C.aspx.

First option involves creating ParameterizedThreadStart delegate, second option is creating anonymous method call that wraps your parameters (using second method may cause unwanted closures).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.