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 can i use delegate to output several param in multi-thread?

how can i use delegate to output several param in multi-thread?

Scheduled Pinned Locked Moved C#
question
7 Posts 4 Posters 1 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.
  • S Offline
    S Offline
    smallkubi
    wrote on last edited by
    #1

    My sample as follows: Now i use Func<> can output 1 param, but i need 3 param output, how can i do it? Func call = y => fun(y, z); //i need output o1,o2, like y=>fun(y,z,out o1,out o2) ? ThreadStart start = () => fun1(x, call); Thread th = new Thread(start);

    OriginalGriffO L B 3 Replies Last reply
    0
    • S smallkubi

      My sample as follows: Now i use Func<> can output 1 param, but i need 3 param output, how can i do it? Func call = y => fun(y, z); //i need output o1,o2, like y=>fun(y,z,out o1,out o2) ? ThreadStart start = () => fun1(x, call); Thread th = new Thread(start);

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      Use a lambda:

          private void DoThread(int i, out int j, out int k)
              {
              j = i \* 2;
              k = i \* 3;
              }
              ...
              int x = -1;
              int y = -1;
              Thread start = new Thread(() => DoThread(6, out x, out y));
              start.Start();
              Thread.Sleep(1000);
              Console.WriteLine("{0},{1}", x, y);
      

      Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      S 1 Reply Last reply
      0
      • OriginalGriffO OriginalGriff

        Use a lambda:

            private void DoThread(int i, out int j, out int k)
                {
                j = i \* 2;
                k = i \* 3;
                }
                ...
                int x = -1;
                int y = -1;
                Thread start = new Thread(() => DoThread(6, out x, out y));
                start.Start();
                Thread.Sleep(1000);
                Console.WriteLine("{0},{1}", x, y);
        

        Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

        S Offline
        S Offline
        smallkubi
        wrote on last edited by
        #3

        oh, DoThread function i want to transfer to Thread as a var, your sample is call immidiately. ThreadO th_o = new ThreadO(x => c( x, n,out F)); //this is Func<>,but i also want to out F Thread th = new Thread(new ThreadStart(th_o.Cons_Mult)); th.Start(); private double c(double x,double n,out double F) { } class ThreadO { public ThreadO(Func fun1) { ... } }

        1 Reply Last reply
        0
        • S smallkubi

          My sample as follows: Now i use Func<> can output 1 param, but i need 3 param output, how can i do it? Func call = y => fun(y, z); //i need output o1,o2, like y=>fun(y,z,out o1,out o2) ? ThreadStart start = () => fun1(x, call); Thread th = new Thread(start);

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          If you can handle one parameter, you can handle multiple; encapsulate all three items in a class, and set an instance of that new class as your parameter.

          class MyClass
          {
          public string o1;
          public object o2;
          public int y { get; set; }
          public int z { get; set; }
          }

          :)

          Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^][](X-Clacks-Overhead: GNU Terry Pratchett)

          S 1 Reply Last reply
          0
          • L Lost User

            If you can handle one parameter, you can handle multiple; encapsulate all three items in a class, and set an instance of that new class as your parameter.

            class MyClass
            {
            public string o1;
            public object o2;
            public int y { get; set; }
            public int z { get; set; }
            }

            :)

            Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^][](X-Clacks-Overhead: GNU Terry Pratchett)

            S Offline
            S Offline
            smallkubi
            wrote on last edited by
            #5

            Yes,haha, you are right, but i want my code simplex and efficient, so i hope no more class, only several variable can solve it. Func<> can output one param, but i want more output param. ,i don't know how to do it. how can i use delegate to define multi-output delegate ?

            L 1 Reply Last reply
            0
            • S smallkubi

              My sample as follows: Now i use Func<> can output 1 param, but i need 3 param output, how can i do it? Func call = y => fun(y, z); //i need output o1,o2, like y=>fun(y,z,out o1,out o2) ? ThreadStart start = () => fun1(x, call); Thread th = new Thread(start);

              B Offline
              B Offline
              BillWoodruff
              wrote on last edited by
              #6

              I think what you want here is a way to "compose" a Func that you can use later

              // sample Struct, but you could use a Class also
              public struct TestStruct
              {
              int X;
              int Y;

              public TestStruct(int x, int y)
              {
                  X = x;
                  Y = y;
              }
              

              }

              // define the Func
              private Func TestFunc = (x, y) =>
              {
              return new TestStruct(x * 4, y * 5);
              };

              private TestStruct testStruct1;

              // test in some method
              testStruct1 = TestFunc(100, 200);

              «Tell me and I forget. Teach me and I remember. Involve me and I learn.» Benjamin Franklin

              1 Reply Last reply
              0
              • S smallkubi

                Yes,haha, you are right, but i want my code simplex and efficient, so i hope no more class, only several variable can solve it. Func<> can output one param, but i want more output param. ,i don't know how to do it. how can i use delegate to define multi-output delegate ?

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                smallkubi wrote:

                i want my code simplex and efficient, so i hope no more class

                How would boxing the result be ineffcient? And no, that was not complex.

                smallkubi wrote:

                how can i use delegate to define multi-output delegate ?

                You can return an struct or object, you can use out parameters. Or even a callback that gets called for each output-variable :)

                Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^][](X-Clacks-Overhead: GNU Terry Pratchett)

                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