Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to pass a delegate object with parameter as input to a ThreadStart constructor?

How to pass a delegate object with parameter as input to a ThreadStart constructor?

Scheduled Pinned Locked Moved C#
tutorialcsharpquestion
11 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Anthony_Yio
    wrote on last edited by
    #1

    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

    V G N 3 Replies Last reply
    0
    • A Anthony_Yio

      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

      V Offline
      V Offline
      Venkatraman
      wrote on last edited by
      #2

      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"

      A 1 Reply Last reply
      0
      • V Venkatraman

        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"

        A Offline
        A Offline
        Anthony_Yio
        wrote on last edited by
        #3

        Thank you but your help doesn't seem helping.

        V 1 Reply Last reply
        0
        • A Anthony_Yio

          Thank you but your help doesn't seem helping.

          V Offline
          V Offline
          Venkatraman
          wrote on last edited by
          #4

          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"

          A 1 Reply Last reply
          0
          • V Venkatraman

            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"

            A Offline
            A Offline
            Anthony_Yio
            wrote on last edited by
            #5

            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.

            V 1 Reply Last reply
            0
            • A Anthony_Yio

              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.

              V Offline
              V Offline
              Venkatraman
              wrote on last edited by
              #6

              Thanks, i forgot to point to this article. Cheers, Venkatraman Kalyanam Chennai - India "Being Excellent is not a skill, it is an attitude"

              1 Reply Last reply
              0
              • A Anthony_Yio

                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

                G Offline
                G Offline
                Gaul
                wrote on last edited by
                #7

                Why can't you pass the reference to ArrayList in the constructor for ClassA()? Gaulles

                A 1 Reply Last reply
                0
                • A Anthony_Yio

                  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

                  N Offline
                  N Offline
                  neroknights
                  wrote on last edited by
                  #8

                  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.

                  A 1 Reply Last reply
                  0
                  • G Gaul

                    Why can't you pass the reference to ArrayList in the constructor for ClassA()? Gaulles

                    A Offline
                    A Offline
                    Anthony_Yio
                    wrote on last edited by
                    #9

                    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.

                    1 Reply Last reply
                    0
                    • N neroknights

                      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.

                      A Offline
                      A Offline
                      Anthony_Yio
                      wrote on last edited by
                      #10

                      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.

                      A 1 Reply Last reply
                      0
                      • A Anthony_Yio

                        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.

                        A Offline
                        A Offline
                        Anonymous
                        wrote on last edited by
                        #11

                        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 !

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups