none
How to use threading on static class with parameters? RRS feed

  • Question

  • I am attempting to use the threading class to try and run a Static method in my program. Using .Net 2.0.

    i.e.

     class MyClass
        {
            public static bool MyMethod(string Param1, int Param2)
            {

                  //Do things...

            }

    }

    Then on my main Form:

     private void RunTHread()
            {
                Thread mythread = new Thread(new ThreadStart(MyClass.MyMethod));
            }

    This gives me an error: No overload for 'MyMethod' matches delegate 'System.Threading.ThreadStart'

    So I try adding the params and get error: Method name expected

    Thread mythread = new Thread(new ThreadStart(MyClass.MyMethod(Param1, Param2)));

    What am I doing wrong here?

    Saturday, May 5, 2012 12:17 AM

Answers

  • The problem is that ThreadStart, which is what the Thread constructor uses, is a delegate defining a method that takes no arguments.

    As such, you need to make a method that takes a single Object as a parameter, and use ParameterizedThreadStart instead, and then have that method undo your parameters.  Since you're using .NET 2, this will likely require you to make a custom class to hold your string and int values, create that, pass it into mythread.Start(yourObject), and then have the method (which takes an object) cast back to your class, extract the two values, and call MyMethod.

    Note that this is far simpler in the newer releases of C#, as you can use a closure (this won't work in .NET 2/VS 2005):

     private void RunThread()
            {
                // Need values - could be passed in
                string arg1 = "Foo";
                int arg2 = 42;
                Thread mythread = new Thread(() => MyClass.MyMethod(arg1, arg2));
                mythread.Start();
            }


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Saturday, May 5, 2012 2:17 AM
    Moderator
  • Hi, 

    Cross-thread exception occurs when you trying to update control from the thread other than thread that created control, These controls (winform) can only be modified by the Thread that has created them. For these cases you can check whether the execution thread is the thread that has created them by using control.InvokeRequired.

    how we use?

    if(textBox1.InvokeRequired)
    {
     textBox1.Invoke(new MethodInvoker(() => {//perform}) );
    }

    You can also use UI thread's SynchronizationContext to perform 

    var uiContext = SynchronizationContext.Current; Thread performTask = new Thread(new ParameterizedThreadStart(

    (object o) => { var context = o as SynchronizationContext; context.Send(//delegate, //state); }));

    performTask.Start(uiContext);

    I hope this helps you...


    If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful".


    • Edited by Kris444 Saturday, May 5, 2012 7:15 AM
    • Proposed as answer by Ashwini47 Sunday, May 6, 2012 7:46 AM
    • Marked as answer by Mike FengModerator Wednesday, May 16, 2012 12:53 PM
    Saturday, May 5, 2012 7:07 AM

All replies

  • The problem is that ThreadStart, which is what the Thread constructor uses, is a delegate defining a method that takes no arguments.

    As such, you need to make a method that takes a single Object as a parameter, and use ParameterizedThreadStart instead, and then have that method undo your parameters.  Since you're using .NET 2, this will likely require you to make a custom class to hold your string and int values, create that, pass it into mythread.Start(yourObject), and then have the method (which takes an object) cast back to your class, extract the two values, and call MyMethod.

    Note that this is far simpler in the newer releases of C#, as you can use a closure (this won't work in .NET 2/VS 2005):

     private void RunThread()
            {
                // Need values - could be passed in
                string arg1 = "Foo";
                int arg2 = 42;
                Thread mythread = new Thread(() => MyClass.MyMethod(arg1, arg2));
                mythread.Start();
            }


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Saturday, May 5, 2012 2:17 AM
    Moderator
  • Thanks.

    I admit I am still trying to grasp this threading stuff.

    Using delegate fixes the problem I started with, but now having to work on my next problem.

    "Cross-thread operation not valid: Control 'Main' accessed from a thread other than the thread it was created on."

    Saturday, May 5, 2012 4:50 AM
  • Hi, 

    Cross-thread exception occurs when you trying to update control from the thread other than thread that created control, These controls (winform) can only be modified by the Thread that has created them. For these cases you can check whether the execution thread is the thread that has created them by using control.InvokeRequired.

    how we use?

    if(textBox1.InvokeRequired)
    {
     textBox1.Invoke(new MethodInvoker(() => {//perform}) );
    }

    You can also use UI thread's SynchronizationContext to perform 

    var uiContext = SynchronizationContext.Current; Thread performTask = new Thread(new ParameterizedThreadStart(

    (object o) => { var context = o as SynchronizationContext; context.Send(//delegate, //state); }));

    performTask.Start(uiContext);

    I hope this helps you...


    If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful".


    • Edited by Kris444 Saturday, May 5, 2012 7:15 AM
    • Proposed as answer by Ashwini47 Sunday, May 6, 2012 7:46 AM
    • Marked as answer by Mike FengModerator Wednesday, May 16, 2012 12:53 PM
    Saturday, May 5, 2012 7:07 AM