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 88689
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T22:35:57+00:00 2026-05-10T22:35:57+00:00

So I’m a newbie to TDD, and I successfully created a nice little sample

  • 0

So I’m a newbie to TDD, and I successfully created a nice little sample app using the MVP pattern. The major problem to my current solution is that its blocking the UI thread, So I was trying to setup the Presenter to use the SynchronizationContext.Current, but when I run my tests the SynchronizationContext.Current is null.

Presenter Before Threading

public class FtpPresenter : IFtpPresenter {     ...     void _view_GetFilesClicked(object sender, EventArgs e)     {         _view.StatusMessage = Messages.Loading;          try         {             var settings = new FtpAuthenticationSettings()             {                 Site = _view.FtpSite,                 Username = _view.FtpUsername,                 Password = _view.FtpPassword             };             var files = _ftpService.GetFiles(settings);              _view.FilesDataSource = files;             _view.StatusMessage = Messages.Done;                 }         catch (Exception ex)         {             _view.StatusMessage = ex.Message;         }     }     ... } 

Test Before Threading

[TestMethod] public void Can_Get_Files() {     var view = new FakeFtpView();     var presenter = new FtpPresenter(view, new FakeFtpService(), new FakeFileValidator());      view.GetFiles();     Assert.AreEqual(Messages.Done, view.StatusMessage); } 

Now after I added a SynchronizationContext Threading to the Presenter I tried to set a AutoResetEvent on my Fake View for the StatusMessage, but when I run the test the SynchronizationContext.Current is null. I realize that the threading model I’m using in my new Presenter isn’t perfect, but is this the right technique for Testing Multithreading? Why is my SynchronizationContext.Current null? What should I do instead?

Presenter After Threading

public class FtpPresenter : IFtpPresenter {     ...     void _view_GetFilesClicked(object sender, EventArgs e)     {         _view.StatusMessage = Messages.Loading;          try         {             var settings = new FtpAuthenticationSettings()             {                 Site = _view.FtpSite,                 Username = _view.FtpUsername,                 Password = _view.FtpPassword             };             // Wrap the GetFiles in a ThreadStart             var syncContext = SynchronizationContext.Current;             new Thread(new ThreadStart(delegate             {                 var files = _ftpService.GetFiles(settings);                 syncContext.Send(delegate                 {                     _view.FilesDataSource = files;                     _view.StatusMessage = Messages.Done;                 }, null);             })).Start();         }         catch (Exception ex)         {             _view.StatusMessage = ex.Message;         }     }     ... } 

Test after threading

[TestMethod] public void Can_Get_Files() {     var view = new FakeFtpView();     var presenter = new FtpPresenter(view, new FakeFtpService(), new FakeFileValidator());      view.GetFiles();     view.GetFilesWait.WaitOne();     Assert.AreEqual(Messages.Done, view.StatusMessage); } 

Fake View

public class FakeFtpView : IFtpView {     ...     public AutoResetEvent GetFilesWait = new AutoResetEvent(false);     public event EventHandler GetFilesClicked = delegate { };     public void GetFiles()     {         GetFilesClicked(this, EventArgs.Empty);     }     ...     private List<string> _statusHistory = new List<string>();     public List<string> StatusMessageHistory     {         get { return _statusHistory; }     }     public string StatusMessage     {         get         {             return _statusHistory.LastOrDefault();         }         set         {             _statusHistory.Add(value);             if (value != Messages.Loading)                 GetFilesWait.Set();         }     }     ... } 
  • 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. 2026-05-10T22:35:58+00:00Added an answer on May 10, 2026 at 10:35 pm

    I’ve run into similar problems with ASP.NET MVC where it is the HttpContext that is missing. One thing you can do is provide an alternate constructor that allows you to inject a mock SynchronizationContext or expose a public setter that does the same thing. If you can’t change the SynchronizationContext internally, then make a property that you set to the SynchronizationContext.Current in the default constructor and use that property throughout your code. In your alternate constructor, you can assign the mock context to the property — or you can assign to it directly if you give it a public setter.

    public class FtpPresenter : IFtpPresenter { public SynchronizationContext CurrentContext { get; set; }

       public FtpPresenter() : this(null) { }     public FtpPresenter( SynchronizationContext context )    {        this.CurrentContext = context ?? SynchronizationContext.Current;    }     void _view_GetFilesClicked(object sender, EventArgs e)    {      ....      new Thread(new ThreadStart(delegate         {             var files = _ftpService.GetFiles(settings);             this.CurrentContext.Send(delegate             {                 _view.FilesDataSource = files;                 _view.StatusMessage = Messages.Done;             }, null);         })).Start();      ...    } 

    One other observation that I would make is that I would probably have your presenter depend on an interface to the Thread class rather than on Thread directly. I don’t think that your unit tests should be creating new threads but rather interacting with a mock class that just ensures that the proper methods to create threads get called. You could inject that dependency as well.

    If the SynchronizationContext.Current doesn’t exist when the constructor is called, you may need to move the assignment logic to Current into the getter and do lazy load.

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

Sidebar

Ask A Question

Stats

  • Questions 74k
  • Answers 74k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer The important point is that Golomb codes are not meant… May 11, 2026 at 2:18 pm
  • added an answer Look at indy IdURI unit, it has two static methods… May 11, 2026 at 2:18 pm
  • added an answer SELECT DISTINCT sc.StoreID, dbo.GetPhoneNumber10(sc.Phone) DISTINCT is wrong. It will allow… May 11, 2026 at 2:18 pm

Related Questions

So I'm getting a new job working with databases (Microsoft SQL Server to be
So I have a Sybase stored proc that takes 1 parameter that's a comma
So I'm embarking on an ASP.NET MVC project and while the experience has been
So I've got a JPanel implementing MouseListener and MouseMotionListener : import javax.swing.*; import java.awt.*;
So I wrote some perl that would parse results returned from the Amazon Web

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.