How to pass a delegate object with parameter as input to a ThreadStart constructor?
-
Code example : ClassA oClassA = new ClassA(); Thread oWorker = new Thread(new ThreadStart(oClassA.read)); //How??, this is only partially done My ClassA has a method "read(out ArrayList Something)" which expect a reference to ArrayList as input paratemer. How could I do so in C# with the code above? Thank You
Anthony_Yio wrote: read(out ArrayList Something)" i guess the delegate ThreadStart encapsulates any methods without any parameters. Cheers, Venkatraman Kalyanam Chennai - India "Being Excellent is not a skill, it is an attitude"
-
Anthony_Yio wrote: read(out ArrayList Something)" i guess the delegate ThreadStart encapsulates any methods without any parameters. Cheers, Venkatraman Kalyanam Chennai - India "Being Excellent is not a skill, it is an attitude"
Thank you but your help doesn't seem helping.
-
Thank you but your help doesn't seem helping.
Anthony_Yio wrote: your help doesn't seem helping Ok. One solution could be use module level variable for storing the parameter and manipulate it from with in the function. Cheers, Venkatraman Kalyanam Chennai - India "Being Excellent is not a skill, it is an attitude"
-
Anthony_Yio wrote: your help doesn't seem helping Ok. One solution could be use module level variable for storing the parameter and manipulate it from with in the function. Cheers, Venkatraman Kalyanam Chennai - India "Being Excellent is not a skill, it is an attitude"
Ok, thank you for this time although I had found some solutions in the web already. I thought you were just one of those irresponsible people where they like to post and run. Apparently, I am glad you are not because you are trying :) Some of the solutions I found was Thread local storage or Save it into the object state.
-
Ok, thank you for this time although I had found some solutions in the web already. I thought you were just one of those irresponsible people where they like to post and run. Apparently, I am glad you are not because you are trying :) Some of the solutions I found was Thread local storage or Save it into the object state.
Thanks, i forgot to point to this article. Cheers, Venkatraman Kalyanam Chennai - India "Being Excellent is not a skill, it is an attitude"
-
Code example : ClassA oClassA = new ClassA(); Thread oWorker = new Thread(new ThreadStart(oClassA.read)); //How??, this is only partially done My ClassA has a method "read(out ArrayList Something)" which expect a reference to ArrayList as input paratemer. How could I do so in C# with the code above? Thank You
-
Code example : ClassA oClassA = new ClassA(); Thread oWorker = new Thread(new ThreadStart(oClassA.read)); //How??, this is only partially done My ClassA has a method "read(out ArrayList Something)" which expect a reference to ArrayList as input paratemer. How could I do so in C# with the code above? Thank You
You could also and is probably better than spawning a new thread is to use the BeginInvoke(string str, AsyncCallback asc, object stateobject). You then call the EndInvoke(IAsyncResult result) to retrieve the answer from the function. You pass a reference to the Arraylist in the stateobject and your AsyncCallback method handles and the parameter within it's function. The good thing here is that you don't have to create a new thread, but rather the delagate function is executing on a thread from the thread pool - no creation is necessary. I understand that this is probably hard to understand if you've never seen it before but you can look up BeginInvoke. .NET supports asynchronous method calls wonderfully with delagate.BeginInvoke.
-
Reason is that I will need to process the reference parameter in the contructor itself unless I would to store the refence to a pointer but it would make my program to be unsafe. :( I guess this is not an old C++ style programming anymore.
-
You could also and is probably better than spawning a new thread is to use the BeginInvoke(string str, AsyncCallback asc, object stateobject). You then call the EndInvoke(IAsyncResult result) to retrieve the answer from the function. You pass a reference to the Arraylist in the stateobject and your AsyncCallback method handles and the parameter within it's function. The good thing here is that you don't have to create a new thread, but rather the delagate function is executing on a thread from the thread pool - no creation is necessary. I understand that this is probably hard to understand if you've never seen it before but you can look up BeginInvoke. .NET supports asynchronous method calls wonderfully with delagate.BeginInvoke.
Thank you for your suggestion. I will look into it. Actually, I just want to make a simple thread call with parameter but it ends up in complexities. Gosh, sometimes I just hate new technologies.
-
Thank you for your suggestion. I will look into it. Actually, I just want to make a simple thread call with parameter but it ends up in complexities. Gosh, sometimes I just hate new technologies.
Actually it is already simple. Take a look ... public delegate int StringDelegate(string str); . . . private int GetLength(string str) { return str.Length; } . . . StringDelegate d = new StringDelegate(GetLength); IAsyncResult ar; ar = d.BeginInvoke("Hello", null, null); ar.AsyncWaitHandle.WaitOne(); // Wait for the function to finish int length = d.EndInvoke(ar); // Voila, the result // Note that you can also have a callback function defined in the // second parameter. If defined, this function will be called once // the delagate has completed. In addition, you can pass a state // object to your GetLength method like so ... // private int GetLength(string str, object state) ... // You now have other information in addition to the passed "Hello" // parameter. :-D // Clearly delagates are the way to go. They are simple and are // extremely flexible. Also remember that the thread runs within the // thread pool. No creation is required !