Results 1 to 7 of 7

Thread: [RESOLVED] how to create new 'Thread' when delegate has parameters...

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,168

    Resolved [RESOLVED] how to create new 'Thread' when delegate has parameters...

    I have this procedure that looks like this:

    Code:
    private void proc1(int x, int y)
    {
    //code
    }
    
    //i'm trying to create thread here
    Thread t;
    t = new Thread(new ThreadStart(proc1)) //also tried ThreadStart(proc1(x,y))
    t.Start();
    This code does not compile. How do I do this correctly?

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: how to create new 'Thread' when delegate has parameters...

    What are your compile errors?

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,168

    Re: how to create new 'Thread' when delegate has parameters...

    I get this error when my delegate has a parameter and i declare my thread like this:

    t = new Thread(new ThreadStart(proc1))

    error:
    Method 'Prog.Form1.btnGoProc(int)' does not match delegate 'void System.Threading.ThreadStart()'

    when:

    t = new Thread(new ThreadStart(proc1(int_value)))

    error:
    Method name expected


    I guess a better question would be, is it possible to create a new thread with a delegate that has parameters?

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: how to create new 'Thread' when delegate has parameters...

    No it isn't possible. If you want to give your thread data then you can set class-level variables that the new thread can then retrieve. Another option is to create a new object specifically for the new thread. You pass the data for your thread to properties of the object and then set a method of the object as the thread entry point.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: how to create new 'Thread' when delegate has parameters...

    Note that .NET 2.0 adds a new way to do what you want. You can create a ParameterizedThreadStart delegate instead of a ThreadStart. You can then pass a single Object to Thread.Start as a parameter for the method actiang as the thread entry point. You could rewrite your code like this in C# 2005:
    Code:
    private void proc1(Point p)
    {
    //code
    }
    
    //i'm trying to create thread here
    Thread t;
    t = new Thread(new ParameterizedThreadStart(proc1)) //also tried ThreadStart(proc1(x,y))
    t.Start(new Point(10, 20));
    MSDN2 points out that this method is not type-safe as you can pass any object to Thread.Start and the compiler will not check whether it is the correct type for the method that will be called. It goes on to recommend the second method I mentioned above as a better way to ensure type-safety.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,168

    Re: how to create new 'Thread' when delegate has parameters...

    thanks.

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: how to create new 'Thread' when delegate has parameters...

    Don't forget to resolve your thread from the Thread Tools menu if you have all the information you need.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width