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

  • SEARCH
  • Home
  • 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 3441906
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:36:35+00:00 2026-05-18T08:36:35+00:00

I have a method that looks like this: protected void OnBarcodeScan(BarcodeScannerEventArgs e) { //

  • 0

I have a method that looks like this:

protected void OnBarcodeScan(BarcodeScannerEventArgs e)
{
    // We need to do this on a seperate thread so we don't block the main thread.
    ThreadStart starter = () => SendScanMessage(e, _scanDelegates);
    Thread scanThread = new Thread(starter);

    scanThread.Start();
}

Then the thread goes off and does some logic (and ends up calling a delegate in my test).

My problem is that my unit test finishes before the thread does. So my test fails.

I can just add in System.Threading.Thread.Sleep(1000); and hope that the logic never takes more than a second (it should not). But that seems like a hack.

The problem is that I don’t want to expose that thread to the outside world or even to the rest of the class.

Is there some cool way to find that thread again and wait for it in my unit test?

Something like this:

[TestMethod]
[HostType("Moles")]
public void AddDelegateToScanner_ScanHappens_ScanDelegateIsCalled()
{
    // Arrange
    bool scanCalled = false;
    MCoreDLL.GetTopWindow = () => (new IntPtr(FauxHandle));

    // Act
    _scanner.AddDelegateToScanner(_formIdentity, ((evnt) => { scanCalled = true; }));
    _scanner.SendScan(new BarcodeScannerEventArgs("12345678910"));

    // This line is fake!
    System.Threading.Thread.CoolMethodToFindMyThread().Join();

    // Assert
    Assert.IsTrue(scanCalled);
}

I obviously made up the CoolMethodToFindMyThread method. But is there some why to do that?

  • 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-05-18T08:36:35+00:00Added an answer on May 18, 2026 at 8:36 am

    So if I understand how this works, then the delegates you register are the ones being called on the second thread, right? In that case, you can use thread synchronization in your test and the delegate that gets called. I do this kind of thing in my unit tests all the time.

    Something like this:

    [TestMethod]
    [HostType("Moles")]
    public void AddDelegateToScanner_ScanHappens_ScanDelegateIsCalled()
    {
        // Arrange
        var scanCalledEvent = new ManualResetEvent(false);
        MCoreDLL.GetTopWindow = () => (new IntPtr(FauxHandle));
    
        // Act
        _scanner.AddDelegateToScanner(_formIdentity, ((evnt) => { scanCalledEvent.Set(); }));
        _scanner.SendScan(new BarcodeScannerEventArgs("12345678910"));
    
        // Wait for event to fire
        bool scanCalledInTime = scanCalledEvent.WaitOne(SOME_TIMEOUT_IN_MILLISECONDS);
    
        // Assert
        Assert.IsTrue(scanCalledInTime);
    }
    

    It’s important to have some sort of timeout in there, otherwise if something goes wrong your test just locks up and that’s kind of hard to debug. WaitOne will block until the event gets set or the timeout expires, the return value tells you which happened.

    (WARNING: I may have the return value backwards – I don’t remember off the top of my head if true means the event got set or if true means the timeout expired. Check the docs.)

    There are several sync primitives you can use here, which one depends on what you want to do. ManualResetEvent usually works pretty well for me.

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

Sidebar

Related Questions

I have a C++ method signature that looks like this: static extern void ImageProcessing(
I have a method that looks like this public static <T extends MyClass, X
I have an action method that looks like this: public ActionResult DoSomething(string par, IEnumerable<string>
I have a UIScrollView decendent that implements a takeScreenshot method that looks like this:
I have a class that looks like this: public class TextField : TextBox {
I have code that looks like this: public class Polynomial { List<Term> term =
Some existing web services I consume have methods that look something like this: List<Employee>
Ok so I have a number of methods that look like this:- which sorts
I have a repeater that is looping a user control, like this: <asp:Repeater ID=repItems
I have class method that returns a list of employees that I can iterate

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.