Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8074507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T14:40:22+00:00 2026-06-05T14:40:22+00:00

I’m a bit weak on how some delegates behave, such as passing a method

  • 0

I’m a bit weak on how some delegates behave, such as passing a method as the parameter to be invoked. While trying to do some NUnit test scripts, I have something that I need to run many test with. Each of these tests requires a GUI created and thus the need for an STA thread. So, I have something like

public class MyTest
{

   // the Delegate "ThreadStart" is part of the System.Threading namespace and is defined as
   // public delegate void ThreadStart();
   protected void Start_STA_Thread(ThreadStart whichMethod)
   {
      Thread thread = new Thread(whichMethod);
      thread.SetApartmentState(ApartmentState.STA); //Set the thread to STA
      thread.Start();
      thread.Join();
   }

   [Test]
   public void Test101()
   { 
      // Since the thread issues an INVOKE of a method, I'm having it call the
      // corresponding "FromSTAThread" method, such as
      Start_STA_Thread( Test101FromSTAThread );
   }

   protected void Test101FromSTAThread()
   {
      MySTA_RequiredClass oTmp = new MySTA_RequiredClass();
      Assert.IsTrue( oTmp.DoSomething() );
   }
}

This part all works fine… Now the next step. I now have a different set of tests that ALSO require an STA thread. However, each “thing” I need to do requires two parameters… both strings (for this case).

How do I go about declaring proper delegate so I can pass in the method I need to invoke, AND the two string parameters in one shot… I may have 20+ tests to run with in this pattern and may have future of other similar tests with different parameter counts and types of parameters too.

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T14:40:24+00:00Added an answer on June 5, 2026 at 2:40 pm

    You can use a lambda expression.

    Here is a modified method you want to call in your test accepting parameters:

    protected void Test101FromSTAThread(String arg1, Int32 arg2)
    {
    }
    

    And here is how to call Start_STA_Thread using a lambda expression:

    public void Test101()
    {
       Start_STA_Thread(() => Test101FromSTAThread("foobar", 123));
    }
    

    The lambda expression in this case is:

    () => Test101FromSTAThread("foobar", 123)
    

    This matches the required ThreadStart delegate. Both are methods without parameters having a void return type.

    If you are unfamiliar with lambda expression you can learn more by reading Lambda Expressions (C# Programming Guide) on MSDN. A lambda expression is an anonymous function and here it creates a delegate. The delegate is passed to the thread and when the thread starts the anonymous function is executed. In this case, because a thread is involved, the anonymous function is executed on the thread you create and not the test runner thread.

    When you use a lambda expression in C# the compiler will emit a function with a “funny” name representing the anonymous function. If the lambda expression uses variables from the lexical environment (e.g. a local variable defined before the lambda expression) the compiler will generate the anonymous function inside a special type (again with a “funny” name) to create what is called a closure. This allows the compiler to capture the variables from the lexical environment into the state of the special type and use it when the the anonymous function later is executed. In your case this happens when the thread starts.

    The code above will generate something like this (the “funny” name <Test101>b__0 will of course not compile but it is perfectly valid in IL):

    public void Test101()
    {
       Start_STA_Thread(<Test101>b__0);
    }
    
    // The "anonymous" function.
    void <Test101>b__0()
    {
       Test101FromSTAThread("foobar", 123);
    }
    

    Note how <Test101>b__0 can be used as a ThreadStart delegate.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.