gpt4 book ai didi

c# - 字典在后台加载

转载 作者:行者123 更新时间:2023-11-30 21:08:09 24 4
gpt4 key购买 nike

这似乎应该如此简单,但显然并非如此。

我有一本字典需要在程序启动时加载大约 40,000 个条目。它只会延迟加载时间大约 5-7 秒,但我想在后台执行此操作以避免这种情况。

在下面的代码中,有 3 个部分处理我目前拥有的 dictionaryBackgroundWorker

我知道这段代码进入了 PopulateZipCodeDictionary() 方法,但由于某种原因它实际上并没有启动该方法。

我做错了什么?

    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...
}

最佳答案

这是我用于后台处理的例程。您只需为 RunBehind 提供一个要执行的操作和一个在处理完成时调用的操作。

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();
}
}

也可能尝试锁定...

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;
}
}
}

关于c# - 字典在后台加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9843371/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com