locked
Multi-thread RRS feed

  • Question

  •  

    Hi fellows, I am starting to write a windows app (C#),  that has to call a method using threads. I have 2 doubts regarding it...

     

    1) All the examples Ive seen so far show the called methods with no parameters. My method to be delegated has 2 parameters. I tried to put the whole signature inside the thread command, but it didnt accept...does anyone know how to do this?

     

    2) Most examples Ive seen dont use ThreadPool. What is the difference or actual advantage in using it? Is it mandatory? Does it have something to do with Garbage collection?

     

    One possible way around might be using ParameterizedThreadStart, but it only accepts 1 parameter, and I need to pass 2!

    Tuesday, January 29, 2008 8:53 PM

Answers

  •  

    Not really the right forum, you'd probably get better responses in the C# forums.

     

    As you can see, ParameterizedThreadStart accepts an object, so is not type safe.  You could have worked around that by passing a struct to the parameterized start by having the struct as the parameter to the method that the thread will call to start up, but the article below details an alternative, it is for VS 2008, if you have that available.

     

     

    See:

     

    http://msdn2.microsoft.com/en-us/library/ts553s52.aspx

     

    Particularly section on passing data to threads and retrieving data from threads.

     

    I hope that this helps,

     

    Martin Platt.

     

    Tuesday, January 29, 2008 11:17 PM

All replies

  •  

    Not really the right forum, you'd probably get better responses in the C# forums.

     

    As you can see, ParameterizedThreadStart accepts an object, so is not type safe.  You could have worked around that by passing a struct to the parameterized start by having the struct as the parameter to the method that the thread will call to start up, but the article below details an alternative, it is for VS 2008, if you have that available.

     

     

    See:

     

    http://msdn2.microsoft.com/en-us/library/ts553s52.aspx

     

    Particularly section on passing data to threads and retrieving data from threads.

     

    I hope that this helps,

     

    Martin Platt.

     

    Tuesday, January 29, 2008 11:17 PM
  • Hi Martin,

     

    I am not that fond of passing a struct, neither I use VS2008 (at least for now), so I guess the best option would be passing the 2 parameters first calling an instance of the class, and then delegating it with no parameters.

     

    I would like to ask you another thing. Would you see webservices as an alternative for working with threads? The main reason why I chose to work with thread is the asyncronous way it works with code execution. Webservices are natively asyncronous objects, so I suppose they might replace it well. What do you think about it?

    Wednesday, January 30, 2008 9:28 AM