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

查看:46
本文介绍了使用文本框和自定义词典进行拼写会减慢我在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_USER \ Software \ Microsoft \ Spelling \ Dictionaries中的 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_USER\Software\Microsoft\Spelling\Dictionaries. 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?

非常感谢收到任何建议.

Any advice gratefully received.

推荐答案

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

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

  1. 我确保使用CustomDictionaries.在所有带有自定义词典的文本框中删除它们,然后关闭它们所在的表单.这样可以消除_GLOBAL_中的新条目以及AppData \ Local \ Temp中的相关文件.

但是有时还是会出现问题,或者用户只是结束任务而将_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_中没有匹配文件的条目(如上面引用的上一篇文章中所建议),而且还将删除所有引用AppData \ Local \ Temp中的.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 AppData\Local\Temp. 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_USER\Software\Microsoft\Spelling\Dictionaries", "_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(@"\AppData\Local\Temp\"))
                    {
                        // Assuming that anyone who wants to keep a permanent .dic file will not store it in \AppData\Local\Temp
                        // 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_USER\Software\Microsoft\Spelling\Dictionaries", "_Global_", realDictionaries.ToArray());
            }
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(this, "Error clearing up old dictionary files.\n\nFull message:\n\n" + ex.Message, "Unable to delete file", MessageBoxButtons.OK, MessageBoxIcon.Warning);
    }

我仍然想知道,清除_GLOBAL_中引用AppData \ Local \ Temp中文件的条目是否完全安全.人们当然不应该将重要的内容留在临时文件夹中吗?

I am still wondering if it is totally safe to clear entries in _GLOBAL_ that refer to files in AppData\Local\Temp. 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天全站免登陆