使用文本框和自定义词典进行拼写会减慢我在 C# WPF 中的应用程序 [英] Spelling with textboxes and custom dictionaries slowing down my application in C# WPF

查看:10
本文介绍了使用文本框和自定义词典进行拼写会减慢我在 C# WPF 中的应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的 WinForm 应用程序中使用 WPF 文本框进行拼写检查.每次创建一个时,我都会将相同的文件加载为 CustomDictionary.直到最近一切都很好.现在,它们需要很长时间才能加载,最多一秒钟.有些表格有 30 个或更多,这意味着延迟将近半分钟.这似乎是 Windows 10(不是我最初发布的 Windows 8)的情况.该应用程序在DotNet 4.0下运行,我尝试过4.5和4.6(不是4.61),所有版本都很慢.

I am using a WPF TextBoxes inside my WinForm application for spell checking. Each time I create one, I load the same file in as a CustomDictionary. All has been fine until recently. Now, they take a long time to load, up to a second. Some forms have 30 or more, meaning delays of nearly half a minute. This seems to be the case Windows 10 (not Windows 8 as I originally posted). The application is running under DotNet 4.0, I have tried 4.5 and 4.6 (not 4.61) and all versions are slow.

我看过 sfaust 的问题 Win10 中的拼写检查文本框 - 慢 和 am7zd 的回答.多亏了这些,我查看了 HKEY_CURRENT_USERSoftwareMicrosoftSpellingDictionaries 中的 GLOBAL 注册表项.我有 580 个条目(在删除没有匹配文件的条目之后),但速度仍然很慢.

I have seen sfaust’s question Spell check textbox in Win10 - Slow and am7zd’s answer. Thanks to these, I looked at the GLOBAL registry key in HKEY_CURRENT_USERSoftwareMicrosoftSpellingDictionaries. I have 580 entries (after pruning out entries without matching files) and still things are slow.

目前,每次创建TextBox并为其添加自定义字典时,_GLOBAL_中似乎都会生成一个新条目

At present, every time I create a TextBox and add a custom dictionary to it, a new entry seems to be generated in _GLOBAL_

  • 有没有比每次都从文件中加载自定义字典更好的方法?
  • 有没有办法每次都重复使用 _GLOBAL_ 中的同一个条目而不是创建一个新条目?
  • 在关闭应用程序(或重新启动应用程序)时,是否有一种干净的方法可以清除我的应用程序创建的 GLOBAL 中以前的条目及其匹配的 .dic 文件?
  • 每次启动应用程序时,我都可以完全清除 _GLOBAL_.这带来了我想要的速度,但缺点是什么?
  • Is there a better way of doing things than loading the custom dictionary in from file every time?
  • Is there a way of re-using the same entry in _GLOBAL_ every time instead of creating a new one?
  • Is there a clean way of clearing previous entries in GLOBAL created by my application and their matching .dic files when closing the application (or on restarting it)?
  • I could clear _GLOBAL_ completely each time I start my application. This brings back the speed I want, but what is the downside?

感谢您的任何建议.

推荐答案

没有其他人的答案,所以这就是我所做的:

No answers from anyone else, so this is what I have done:

  1. 在关闭它们所在的表单之前,我确保在所有带有自定义词典的文本框上都使用了 CustomDictionaries.Remove.这消除了 _GLOBAL_ 中的新条目以及 AppDataLocalTemp 中的相关文件.

但有时会出现问题或用户刚刚结束任务,留下 _GLOBAL_ 条目和 .dic 文件,所以:

But there will be times when things go wrong or the user just ends the task, leaving _GLOBAL_ entries and .dic files in place, so:

  1. 我决定更进一步.当我启动我的应用程序时,我不仅会清除 _GLOBAL_ 中没有匹配文件的条目(如上面引用的上一篇文章中所建议的那样),还会删除引用 AppDataLocalTemp 中的 .dic 文件的所有条目.我的理论是任何留下条目的人都不是故意的,否则他们可能会将 .dic 文件保存在不同的文件夹中(就像 Microsoft Office 所做的那样).

  1. I decided to take things a stage further. When I start my application, I will not only clean entries in _GLOBAL_ that don't have matching files (as suggested in the previous post referenced above), but also to remove all entries referring to .dic files in AppDataLocalTemp. My theory being that anyone who has left entries there didn't mean to, otherwise they would probably have saved the .dic file in a different folder (as Microsoft Office does).

    try
    {

        string[] allDictionaries = (string[])Registry.GetValue(@"HKEY_CURRENT_USERSoftwareMicrosoftSpellingDictionaries", "_Global_",  new string[0]);

        if (allDictionaries.Count() > 0)
        {
            List<string> realDictionaries = new List<string>();
            bool changedSomething = false;

            foreach (string thisD in allDictionaries)
            {
                if (File.Exists(thisD))
                {
                    if (thisD.Contains(@"AppDataLocalTemp"))
                    {
                        // Assuming that anyone who wants to keep a permanent .dic file will not store it in AppDataLocalTemp
                        // So delete the file and don't copy the name of the dictionary into the list of good dictionaries.
                        File.Delete(thisD);
                        changedSomething = true;
                    }
                    else
                    {
                        realDictionaries.Add(thisD);
                    }
                }
                else
                {
                    // File does not exist, so don't copy the name of the dictionary into the list of good dictionaries.
                    changedSomething = true;
                }
            }

            if (changedSomething)
            {
                Registry.SetValue(@"HKEY_CURRENT_USERSoftwareMicrosoftSpellingDictionaries", "_Global_", realDictionaries.ToArray());
            }
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(this, "Error clearing up old dictionary files.

Full message:

" + ex.Message, "Unable to delete file", MessageBoxButtons.OK, MessageBoxIcon.Warning);
    }

我仍然想知道清除 _GLOBAL_ 中引用 AppDataLocalTemp 中文件的条目是否完全安全.当然人们不应该将重要的东西留在临时文件夹中......他们应该吗?

I am still wondering if it is totally safe to clear entries in _GLOBAL_ that refer to files in AppDataLocalTemp. Surely people shouldn't be leaving important stuff in a temp folder... should they?

真正好的是 CustomDictionaries.Add 的重载,它允许我们设置 .dic 文件的名称和文件夹,允许同一应用程序中的所有文本框共享同一个 .dic 文件并确保我们不要一开始就留下一堆看似随机名称的冗余条目和文件......请微软.

What would be really nice would be an overload to CustomDictionaries.Add that allows us to set the name and folder of the .dic file, allowing all the textboxes in the same application to share the same .dic file and making sure we don't leave a load of redundant entries and files with seemingly random names hanging around in the first place..... please Microsoft.

这篇关于使用文本框和自定义词典进行拼写会减慢我在 C# WPF 中的应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆