简体   繁体   English

文件加载挂起时间长| 二进制| C#| 列表

[英]Long Hang Time On File Load| Binary| C#| List

I am wondering why the load time for the file is so long.我想知道为什么文件的加载时间这么长。 i would appreciate it if you would take time to look where it says如果你能花时间看看上面写着的地方,我将不胜感激

if (ReadType == 1)

Around 12,000 items loading it takes nearly 12 seconds to load a file with a short structure i don't think this is right.加载大约 12,000 个项目需要将近 12 秒才能加载结构较短的文件,我认为这是不对的。 I'm new to c# and could use any pointers attached below is the code and the file structure: here also is attached a video of the issue: video screenshot of file: structureloaded我是 c# 的新手,可以使用下面附加的任何指针是代码和文件结构:这里还附上了问题的视频:文件的视频截图:结构加载

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StringEditor
{
 public class ItemStr
  {
    public int a_index;
    public byte[] a_name { get; set; }
    public byte[] a_descr1 { get; set; }
   }
}

private void tsbOpen_Click(object sender, EventArgs e)
    {
        OpenFileDialog ofd = new OpenFileDialog();
        ofd.Filter = "String|*.lod";
        if (ofd.ShowDialog() != DialogResult.OK)
            return;
        if (!ofd.FileName.Contains("strItem") && !ofd.FileName.Contains("strSkill")) //check to see if user isn't opening the right files if not return;
            return;
        else if (ofd.FileName.Contains("strItem"))
            ReadType = 1;
        else if (ofd.FileName.Contains("strSkill"))
            ReadType = 2;


        FileStream fs = new FileStream(ofd.FileName, FileMode.Open);
        BinaryReader br = new BinaryReader(fs);
        if (ReadType == 1)
        {
            int max = br.ReadInt32();
            int max1 = br.ReadInt32();

            for (int i = 0; br.BaseStream.Position < br.BaseStream.Length; i++)
            {
                ItemStr itemstr = new ItemStr();
                itemstr.a_index = br.ReadInt32();
                itemstr.a_name = br.ReadBytes(br.ReadInt32());
                itemstr.a_descr1 = br.ReadBytes(br.ReadInt32());
                itemStringList.Add(itemstr);
                listBox1.Items.Add(itemstr.a_index.ToString() + " - " + Encoding.GetEncoding(ISO).GetString(itemstr.a_name));


            }
            EnableFields();


        }
        fs.Close();
        br.Close();
        if (ReadType == 2)

        {
            int max = br.ReadInt32();
            int max1 = br.ReadInt32();
            for (int i = 0; i < max; i++)
            {
                skillStr skillStr = new skillStr();

                skillStr.a_index = br.ReadInt32();
                skillStr.a_name = br.ReadString();
                skillStr.a_tool_tip = br.ReadString();
                skillStr.a_descr1 = br.ReadString();
                skillStringList.Add(skillStr);
                string test = skillStr.a_index + "- " + skillStr.a_name;
                listBox1.Items.Add(test);


            }
            EnableFields();


        }
        fs.Close();
        br.Close();

    }

I wrote a small test on my core i5 machine.我在我的核心 i5 机器上写了一个小测试。 New form, one button, one listbox:新表单,一个按钮,一个列表框:

    private void button1_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < 30000; i++)
            listBox1.Items.Add(i.ToString());
    }

(I wrote it guessing at the index numbers in your screenshot). (我是根据你截图中的索引号写的)。 Clicked go.单击 go。 Had to wait 11 seconds before the UI became usable again.在 UI 再次可用之前必须等待 11 秒。

I modified it to this:我将其修改为:

    private void button1_Click(object sender, EventArgs e)
    {
        listBox1.BeginUpdate();
        for (int i = 0; i < 30000; i++)
            listBox1.Items.Add(i.ToString());
        listBox1.EndUpdate();
    }

And there was a barely perceptible delay before it was usable again在它再次可用之前有一个几乎无法察觉的延迟


The majority of the problem isn't reading the file, it's having the listbox refresh itself X thousands of times as you add one by one.大部分问题不是读取文件,而是让列表框在您一一添加时自行刷新 X 数千次。 Use Begin/End update to signal that you're loading a large amount of items...使用 Begin/End update 表示您正在加载大量项目...

...but then again, ask yourself what is a user REALLY going to do with X tens of thousands of items in a listbox? ...但话又说回来,问问自己,用户真正打算对列表框中的 X 万个项目做什么? As a UI/UX guideline, avoid loading more than about 20 to 30 items into a list.作为 UI/UX 指南,避免将超过 20 到 30 个项目加载到列表中。 Beyond that it's getting into unnavigable, especially at the quantities you're loading.除此之外,它变得无法导航,尤其是在您装载的数量上。 Consider a type to search box - a one pixel jump of the scroll bar is going to move through more items than can fit vertically in your list!考虑一种类型的搜索框 - 滚动条的一个像素跳跃将穿过更多的项目,而不是垂直适合您的列表!

If you're loading a lot of data from a file (or anywhere) into a list box, consider using the VirtualList approach - an example can be found here:如果您将文件(或任何地方)中的大量数据加载到列表框中,请考虑使用 VirtualList 方法 - 可以在此处找到示例:

ListView.VirtualMode Property ListView.VirtualMode 属性

You probably also want to consider performing the load in a background thread so that the user doesn't experience the apparent "hanging" delay that loading a lot of data can produce.您可能还想考虑在后台线程中执行加载,以便用户不会遇到加载大量数据可能产生的明显“挂起”延迟。

Listbox.beginupdate() and listbox.endupdate() fixed my problem thanks for the help guys. Listbox.beginupdate() 和 listbox.endupdate() 解决了我的问题,感谢大家的帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM