Starting an STA thread, but with parameters to the final function

Posted by DRapp on Stack Overflow See other posts from Stack Overflow or by DRapp
Published on 2012-06-11T15:06:13Z Indexed on 2012/06/11 16:40 UTC
Read the original article Hit count: 214

Filed under:
|
|

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.

© Stack Overflow or respective owner

Related posts about c#

Related posts about delegates