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. Can't We pass Parameterized Method to a thread??

Can't We pass Parameterized Method to a thread??

Scheduled Pinned Locked Moved C#
helpquestion
3 Posts 2 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.
  • R Offline
    R Offline
    Ron S
    wrote on last edited by
    #1

    Hello All, I am trying to call a method through thread . it is working if the method does not contain any parameter, if it has got a parameter then it will pop up error stating that method name expected. Am i missing something here to perform this action. Thanks

    Bharath.S Ron

    E 1 Reply Last reply
    0
    • R Ron S

      Hello All, I am trying to call a method through thread . it is working if the method does not contain any parameter, if it has got a parameter then it will pop up error stating that method name expected. Am i missing something here to perform this action. Thanks

      Bharath.S Ron

      E Offline
      E Offline
      ekynox
      wrote on last edited by
      #2

      you need to use the ParameterizedThreadStart delegate which is part of the Threading namespace in .NET 2.0. i.e

      public void RunThread()
      {
       int parameter = 20;
       Thread t = new Thread(new ParameterizedThreadStart(SampleMethodWithParameters));
       t.Start(parameter);
      }
      
      
      private void SampleMethodWithParameters(object parameter)
      {
       for(int index = 0; index < Convert.ToInt32(parameter); index++)
       {
         Console.WriteLine("HelloWorld " + index.ToString());
         Thread.Sleep(1000);
       }
      }
      

      edit: no idea why my half my FOR loop is being displayed but hopefully that should give you an idea. -- modified at 3:28 Wednesday 25th July, 2007

      R 1 Reply Last reply
      0
      • E ekynox

        you need to use the ParameterizedThreadStart delegate which is part of the Threading namespace in .NET 2.0. i.e

        public void RunThread()
        {
         int parameter = 20;
         Thread t = new Thread(new ParameterizedThreadStart(SampleMethodWithParameters));
         t.Start(parameter);
        }
        
        
        private void SampleMethodWithParameters(object parameter)
        {
         for(int index = 0; index < Convert.ToInt32(parameter); index++)
         {
           Console.WriteLine("HelloWorld " + index.ToString());
           Thread.Sleep(1000);
         }
        }
        

        edit: no idea why my half my FOR loop is being displayed but hopefully that should give you an idea. -- modified at 3:28 Wednesday 25th July, 2007

        R Offline
        R Offline
        Ron S
        wrote on last edited by
        #3

        thank you very much

        Bharath.S Ron

        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