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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:23:23+00:00 2026-05-17T00:23:23+00:00

I’ve got a windows application that creates some threads to parse some txt files.

  • 0

I’ve got a windows application that creates some threads to parse some txt files. Nothing too crazy.

I’m using StructureMap Dependency Injection to create any instances of my Services and Repositories. So my Winform has a static property which returns the instance, based on what’s been registered. Works great .. except I’m not sure when I should be disposing of my unit of work?

I’ve got a single UnitOfWork per thread. Now, the threads don’t really stop. they keep on ticking over. Of course, the main Forms app is it’s own thread, too .. and that doesn’t stop until you kill it. So, i’m not sure when I should be disposing of the UnitOfWork (which then closes the context connection).

Traditionally, I’ve been doing web app programming (ASP.NET MVC now) so I can dispose at the end of the request … and the same UnitOfWork was being used throughout that same request. That works nice … But there is not end request, in a winform 🙁

Any ideas?

Here’s some code to explain what I’m doing…

Registering my Repository classes…

public class EntityFrameworkRepositoryRegistry : Registry
{
    public EntityFrameworkRepositoryRegistry(string connectionString)
    {
        For<IUnitOfWork>()
                .HybridHttpOrThreadLocalScoped()
                .Use<SqlServerUnitOfWork>()
                .Ctor<string>("connectionString")
                    .Is(connectionString)
                .Ctor<ILoggingService>("loggingService")
                    .Is(new NLogLoggingService(GetType().ToString()))
                .SetProperty(x => x.EntityContainerName = "Entities");

        EntityFrameworkProfiler.Initialize(); // Hi Ayende :) Love this :)

        Scan(x =>
                    {
                        x.TheCallingAssembly();

                        x.ExcludeNamespaceContainingType<FakeContext>();

                        x.WithDefaultConventions();
                    }
            );
    }
}

and using the repository in the Form … (yes, this should be a service, but i’ve not abstracted that out, just yet…

private static IUserRepository GetUserRepository()
{
    return ObjectFactory.GetInstance<IUserRepository>();
}

private void LoadUserInformation()
{
    DisplayText("Reading in active users... ", false);
    _users = GetUserRepository().Find().WhereIsActive().ToList();
    DisplayText("done.", false, true);
    DisplayText("Found " + _users.Count + " active users.");
}

.. now I can just call Dispose(); after I use call ToList() but then nothing else can use that UnitOfWork ???

Any ideas ?

  • 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-17T00:23:23+00:00Added an answer on May 17, 2026 at 12:23 am

    I have not written many desktop apps, but the most common pattern I know of is to scope your unit of work to a screen (like a new or edit CRUD screen for example). It looks like your app is a single form though, so maybe you are fine with a single unit of work that lives for the lifetime of the application. Since the DataContext does not hold an open connection to the database, that should not be a problem.

    The other option would be to use a unit of work per user action (every button click gets its own instance). If you decide to go that route, make sure your unit of work is registered as transient and manage the lifetime yourself.

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

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have thousands of HTML files to process using Groovy/Java and I need to
I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the

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.