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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T17:35:20+00:00 2026-05-31T17:35:20+00:00

This seems like it should be so simple, but apparently it isn’t. I have

  • 0

This seems like it should be so simple, but apparently it isn’t.

I have a dictionary that needs to load ~40,000 entries on program startup.
It only delays load time by about 5-7 seconds, but I’d like to do it in the background to avoid this.

In the code below, there are 3 sections that deal with the dictionary and BackgroundWorkerI currently have.

I know that this code makes it to the PopulateZipCodeDictionary() method, but for some reason it doesn’t actually launch that method.

What am I doing wrong?

    static BackgroundWorker populateZipCodeDictionary;

    public MainWindow()
    {
        populateZipCodeDictionary = new BackgroundWorker();
        populateZipCodeDictionary.DoWork += populateZipCodeDictionary_DoWork;
        populateZipCodeDictionary.RunWorkerCompleted += populateZipCodeDictionary_RunWorkerCompleted;
        populateZipCodeDictionary.RunWorkerAsync();         
    }

    static void populateZipCodeDictionary_DoWork(object sender, DoWorkEventArgs e)
    {
        ZipCodes.PopulateZipCodeDictionary();
    }

    static void populateZipCodeDictionary_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        MessageBox.Show("Dictionary loaded");
    }

static class ZipCodes
{
    #region Methods
    public static string GetValue(string key)
    {
        string result;
        if (zipCodeDictionary.TryGetValue(key, out result))
        {
            return result;
        }
        else
        {
            return null;
        }
    }
    #endregion


    #region ZipCode Dictionary Definition
    static Dictionary<string, string> zipCodeDictionary = new Dictionary<string, string>();

    public static void PopulateZipCodeDictionary()
    {
        zipCodeDictionary.Add( "00501", "Holtsville, NY" );
        zipCodeDictionary.Add( "00544", "Holtsville, NY" );
        zipCodeDictionary.Add( "00601", "Adjuntas, PR" );
        zipCodeDictionary.Add( "00602", "Aguada, PR" );
        zipCodeDictionary.Add( "00603", "Aguadilla, PR" );
        zipCodeDictionary.Add( "00604", "Aguadilla, PR" );
        //Continues on adding ~40k entries...
    }
  • 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-31T17:35:21+00:00Added an answer on May 31, 2026 at 5:35 pm

    This is a routine I use for background processing. You simply give RunBehind an action to perform and an action to call when processing is complete.

    public class Worker
    {
        private Dispatcher _appDispatcher = Dispatcher.CurrentDispatcher;
        private Dispatcher _workerDispatcher;
        private Thread _workerThread;
    
        public Worker()
        {
            _workerThread = new Thread(RunWorkerThread);
            _workerThread.Start();
        }
    
        public void RunBehind(Action a_action, Action a_callback)
        {
            _workerDispatcher.BeginInvoke(new Action(() =>
            {
                a_action();
                _appDispatcher.BeginInvoke(a_callback);
            }));
        }
    
        private void RunWorkerThread()
        {
            Thread.CurrentThread.Name = "AppWorker";
            _workerDispatcher = Dispatcher.CurrentDispatcher;
    
            Dispatcher.Run();
        }
    }
    

    Also maybe try locking…

    static class ZipCodes
    {
        private static Object zipLocker = new Object();
    
        #region Methods
        public static string GetValue(string key)
        {
            lock (zipLocker)
            {
                string result;
                if (zipCodeDictionary.TryGetValue(key, out result))
                {
                    return result;
                }
                else
                {
                    return null;
                }
            }
        }
        #endregion
    
    
        #region ZipCode Dictionary Definition
        static Dictionary<string, string> zipCodeDictionary = null;
    
        public static void PopulateZipCodeDictionary()
        {
            Dictionary<string, string> workingDictionary = new Dictionary<string, string>();
    
            workingDictionary.Add( "00501", "Holtsville, NY" );
            workingDictionary.Add( "00544", "Holtsville, NY" );
            workingDictionary.Add( "00601", "Adjuntas, PR" );
            workingDictionary.Add( "00602", "Aguada, PR" );
            workingDictionary.Add( "00603", "Aguadilla, PR" );
            workingDictionary.Add( "00604", "Aguadilla, PR" );
            //Continues on adding ~40k entries...
    
            lock (zipLocker)
            {
                zipCodeDictionary = workingDictionary;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This seems like it should have a simple solution but I can't seem to
This seems like it should be really simple, but I'm unable to figure this
Seems like this should be simple, but powershell is winning another battle with me.
This feels like it should be pretty simple, but not much seems to be
This seems like it should be pretty straight forward, but I'm apparently confused. I
This seems like it should be simple but it's driving me up the wall.
Seems like this should be simple enough but for whatever reason I am unable
This seems like it should be simple, but I can't figure it out. I'm
This seems like it should be simple enough, but I can't seem to get
This seems like it should be simple but I can't work out how to

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.