3

I have a method in a class that I need to execute twice asynchronously. The class has a constructor which accepts URL as a parameter :

ABC abc= new ABC(Url);

// Create the thread object, passing in the abc.Read() method
// via a ThreadStart delegate. This does not start the thread.
Thread oThread = new Thread(new ThreadStart(abc.Read());


ABC abc1= new ABC(Url2)

// Create the thread object, passing in the abc.Read() method
// via a ThreadStart delegate. This does not start the thread.
Thread oThread1 = new Thread(new ThreadStart(abc1.Read());

// Start the thread
oThread.Start();
// Start the thread
oThread1.Start();

Is this the way it works? Can anyone help?

1
  • you can also try BackgroundWorker if it suits you Commented Jan 24, 2012 at 19:27

3 Answers 3

3

Drop the parentheses to create a delegate:

Thread oThread = new Thread(new ThreadStart(abc.Read));

And do the same for oThread1. Here's MSDN's Delegates Tutorial.

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

8 Comments

thanks minitech.. how do you pass the parameter URL to the constructor?
@srikanth: It looks like you're already doing that... abc.Read can still refer to this properly.
i need to send two differenc URL for each instance of ABC class.
@srikanth: Here's a demo: ideone.com/OVMpA Are you storing the URL you pass to the constructor? Just access it with this as usual...
got it thank you very much. i can execute the Read method simultaneously with different url's. it's working fine. will it create any issues in prod?
|
3

You need to change your ThreadStart creation to use the method as a target instead of invoking the method

Thread oThread = new Thread(new ThreadStart(abc.Read);

Notice how I used abc.Read instead of abc.Read(). This version causes the ThreadStart delegate to point to the method abc.Read. The original version abc.Read() was invoking the Read method immediately and trying to convert the result to a ThreadStart delegate. This is likely not what you intended

3 Comments

@JaredPar.. how do you pass the parameter URL to the constructor of ABC class? i need to send two differenc URL for each instance of ABC class.
@srikanth are you trying to create the instances on the background thread?
r... i figured it out. Otherwise, the code that i have is working fine.. asynchronously executing the Read() method with different url's. Is that thread safe and will it create any issues once we move to Prod?
1

You can also do this:

Thread oThread1 = new Thread(() => abc1.Read());

You can pass a lambda in to the Thread constructor instead of newing up a new ThreadStart object.

Joseph Albahari has a great online resource about threading. Very easy to read and lots of examples.

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.