Faster reading and writing of text data

Asked 2 years ago, Updated 2 years ago, 128 views

We currently have a text file between the two applications.
The text file contains the following descriptions:
The number of lines is small, and there are currently only 198 lines.

Enter a description of the image here

This is what the design looks like on the application side.
By checking the check box, we have changed the number after c??: in the previous text file to 0 or 1.

Enter a description of the image here

The problem is that when we display this page, we have prepared a function that reads the numbers in the text file and ticks the check box.
However, due to the slow speed, the check box does not finish until the page drawing is finished.
Specifically, it takes about 5 seconds to control 190 check boxes.
How can I improve my ability to loop text and write faster?

/*Take all check boxes in order and send them to another function*/
private async Task Check IOCclientMod()
{
    int mod, client;
    intcMN = clientModNum;
    intcNN = clientNameNum-1;

    for (mod=0; mod<cMN;mod++)
    {
        for (client=0; client<cNN;client++)
        {
            if (clientModCheck [mod, client]!=null)
            {
                switch(mod)
                {
                    case0:/*God*/wait SetCheckIOCclientMod(mod, client);break;
                    case1: /* InfAmmo*/wait SetCheckIOClientMod(mod, client);break;
                    case2: /* UAV*/wait SetCheckIOCclientMod(mod, client);break;
                    case3: /*VSAT*/wait SetCheckIOCclientMod(mod, client);break;
                    case4: /* Laser*/wait SetCheckIOCclientMod(mod, client);break;
                    case5: /* WalkSpeed*/wait SetCheckIOClientMod(mod, client);break;
                    case6: /* InfKS*/wait SetCheckIOClientMod(mod, client);break;
                    case7: /*Freeze*/wait SetCheckIOClientMod(mod, client);break;
                    case8: /*ToMe*/wait SetCheckIOCclientMod(mod, client);break;
                    case9: /* ToSky*/wait SetCheckIOClientMod(mod, client);break;
                }
            }
        }
    }
}

/* Read the current numeric state from the IO text file from the type and user number sent*/
private async Task SetCheckIOCclientMod (int mod, int client)
{
    string ct = "_c";
    if (client<10) 
        ct+="0";

    string path = def.Ps4Bo3Path;
    string target = "isMod" + def.ioClientModString [mod] + ct+client;
    string enable = def.Enable;

    if (IOs.CheckStat(path, target) == enable)
        clientModCheck [mod, client].Checked=true;
    else
        clientModCheck[mod, client].Checked=false;

    waitwaitTime(1);
}


# region Read the specified status value

public string CheckStat (string path, string kw)
{
    FileStream fs=new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); /* Exclusive Control*/
    StreamReader sr = new StreamReader(fs); /* Exclusive Control*/

    US>//#stringans="; /* initialization*/

    while(sr.Peek()>-1)/* Read all lines*/
    {
        string line = sr.ReadLine(); /* line is substituted with one string */
        If(line.Contains(kw)/*line contains the specified string */
        {
            kw+=":"; /*kw does not contain :, so add */
            int st = kw.Length; /* st with the specified number of characters */
            intls=line.Length; /*ls contains the length of the line read */
            intsp = ls-st; /* read line - specified number of characters = too much */
            ans=line.Substring(st,sp); /*ans with too many - characters between the specified number of characters */
        }
    }

    Return an; /*ans */
}

#endregion

c# visual-studio text-file

2022-09-30 18:07

1 Answers

Profiler should measure the time required for each method to determine the cause.

I think CheckIOClientMod() calls SetCheckIOClientMod() 190 times as designed.However, SetCheckIOClientMod() is calling CheckStat().Also, CheckStat() reads the file

.

In other words, the file has been loaded 190 times, but is it as designed? If it is as designed, there is nothing we can do.
Reading and writing files is very slow, so if possible, you should review the design and load all the data at once.


2022-09-30 18:07

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.