-1

I have a large text files for example 39000 lines and above , i want to load it into richtextbox , using normal way like this:

Richtextbox1.Text=File.ReadAllLines(file);

or

Richtextbox1.LoadFile(...);

it take long time and freeze the UI even i use BackgroundWorker, so i decide to split file into parts each part is 1000 lines and append them to the richtextbox , i mean i read 1000 lines from file into string and then append it to the richtextbox, here is my code:

private void Worker_DoWork(object sender, DoWorkEventArgs e)
        {
            StreamReader reader = null;
            
                try
            {
                //
                int ProgressPercentage = 0;
                
                
                //enable stop Correct button
                BTN_Send_Ref.Invoke((MethodInvoker)(() =>
                {
                    BTN_Send_Ref.Enabled = true;
                }));

                
                
              string[] Lines = File.ReadAllLines(InputTextFile);
                
                //MessageBox.Show("Lines : " + Lines.Length);
                int i= 0;
                int j = 0;
                string LinesCollection = "";

                while (true)
                {
                    if (worker.CancellationPending)
                    {
                        if (reader != null)
                        {
                            reader.Close();
                            reader = null;
                        }
                        e.Cancel = true;
                        //cancel backgroundworker
                        return;
                    }

                    LinesCollection = "";
                    
                    if (i > Lines.Length ||
                        j >= Lines.Length)
                    {
                        break;
                    }

                    i += 1000;

                    if (i >= Lines.Length)
                    {
                        i = Lines.Length ;
                    }
                    
                    while ( j < i)
                    {
                        
                        if (worker.CancellationPending)
                        {
                            if (reader != null)
                            {
                                reader.Close();
                                reader = null;
                            }
                            e.Cancel = true;
                            //cancel backgroundworker
                            return;
                        }

                        if (j < Lines.Length)
                        {
                            if (LinesCollection == "")
                            {
                                LinesCollection = Lines[j];
                            }
                            else
                            {
                                LinesCollection += Environment.NewLine + Lines[j];
                            }
                        }

                        ProgressPercentage = (int)Math.Round((((double)j) / Lines.Length) * 100);

                        if (ProgressPercentage < 100)
                        {
                            //report progress
                            worker.ReportProgress(ProgressPercentage);
                        }
                        ProgressPercentage++;
                        j++;
                    }

                    RichTXT_OutText_Ref.Invoke((MethodInvoker)(() =>
                    {
                        RichTXT_OutText_Ref.AppendText(LinesCollection +Environment.NewLine);
                    }));

                    LBL_CountOfChars_Ref.Invoke((MethodInvoker)(() =>
                    {
                        LBL_CountOfChars_Ref.Text = "(" + RichTXT_OutText_Ref.Text.Length + ") Char";
                        LBL_CountOfChars_Ref.Update();
                    }));

                    LBL_CountOfLines_Ref.Invoke((MethodInvoker)(() =>
                    {
                        LBL_CountOfLines_Ref.Text = "(" + RichTXT_OutText_Ref.Lines.Length + ") Line";
                        LBL_CountOfLines_Ref.Update();
                    }));

                    
                    
                    
                }
          
                /*int Tmp_Count = 0;
                if (RichTXT_OutText_Ref.InvokeRequired)
                {
                    RichTXT_OutText_Ref.Invoke((MethodInvoker)(() =>
                    {
                        RichTXT_OutText_Ref.LoadFile(InputTextFile, RichTextBoxStreamType.PlainText);
                    }));
                }
                else
                {
                    RichTXT_OutText_Ref.LoadFile(InputTextFile, RichTextBoxStreamType.PlainText);
                }
                LBL_CountOfChars_Ref.Invoke((MethodInvoker)(() =>
                {
                    LBL_CountOfChars_Ref.Text = "(" + RichTXT_OutText_Ref.Text.Length + ") Char";
                    LBL_CountOfChars_Ref.Update();
                }));

                LBL_CountOfLines_Ref.Invoke((MethodInvoker)(() =>
                {
                    LBL_CountOfLines_Ref.Text = "(" + RichTXT_OutText_Ref.Lines.Length + ") Line";
                    LBL_CountOfLines_Ref.Update();
                }));
                */

                /*reader= File.OpenText(InputTextFile);
                 
                   string Line = "";
                
                int LinesCounter = 0;
                while ((Line = reader.ReadLine()) != null)
                {
                    if (worker.CancellationPending)
                    {
                        if (reader != null)
                        {
                            reader.Close();
                            reader = null;
                        }
                        e.Cancel = true;
                        //cancel backgroundworker
                        return;
                    }

                    if(LinesCounter >0 && LinesCounter <= 1000)
                    {
                        RichTXT_OutText_Ref.Invoke((MethodInvoker)(() =>
                        {
                            RichTXT_OutText_Ref.AppendText(LinesCollection);
                        }));
                        LinesCollection = "";
                        LinesCounter = 0;
                    }
                    else
                    {
                        if (LinesCollection == "")
                        {
                           LinesCollection= Line;
                        }
                        else
                        {
                            LinesCollection += Environment.NewLine + Line; 
                        }
                    }
                        

                    
                        LBL_CountOfChars_Ref.Invoke((MethodInvoker)(() =>
                        {
                            LBL_CountOfChars_Ref.Text = "(" + RichTXT_OutText_Ref.Text.Length + ") Char";
                            LBL_CountOfChars_Ref.Update();
                        }));

                    LBL_CountOfLines_Ref.Invoke((MethodInvoker)(() =>
                    {
                        LBL_CountOfLines_Ref.Text = "(" + RichTXT_OutText_Ref.Lines.Length + ") Line";
                        LBL_CountOfLines_Ref.Update();
                    }));

                    //calculate progress value
                    ProgressPercentage = (int)Math.Round((((double)PercentageCounter) / Lines.Length) * 100);

                    if (ProgressPercentage < 100)
                    {
                        //report progress
                        worker.ReportProgress(ProgressPercentage);
                    }
                        TempCount++;

                        PercentageCounter++;

                        Tmp_Count++;
                    
                    }
               */
                if (reader != null)
                {
                    reader.Close();
                    reader = null;
                }

                //disable Correct button because
                //we are out of loop
                if (BTN_Send_Ref != null)
                {
                    //we call this btn Correct
                    //from another thread
                    //so we must use Invoke
                    BTN_Send_Ref.Invoke((MethodInvoker)(() =>
                    {
                        BTN_Send_Ref.Enabled = false;
                    }));

                }

                
            }
            catch (Exception ex)
            {
                if (reader != null)
                {
                    reader.Close();
                    reader = null;
                }

                MessageBox.Show(ex.Message+"\n"+ex.StackTrace.ToString());
                throw ex;
            }
        }

the problem is that the code work fine with some file say less than 1000 but when i use large file has size greater than 1000,the count of lines is less than actual file lines

for example i attach text file my text file which has size 62077 but the after use my code the richtextbox lines is 62028, and count of characters also is less the actual count of characters of file, but the start and the end of content in richtextbox and file are the same, i do not know where is the error, all i want is to read text file , combine every 1000 lines in one string append it the richtextbox repeat the operation until end of file content. i do not want to use Richtextbox1.LoadFile(...)and Richtextbox1.Text=File.ReadAllLines(file); because they freeze and hung the UI this happen when using large files more than 39000 lines, i want to know what is the wrong with code?why dose not get right result? i hope you help me.

Fath Bakri
  • 161
  • 1
  • 12
  • 1
    Does this answer your question? [In C#, Loading large file into winform richtextbox](https://stackoverflow.com/questions/31214687/in-c-loading-large-file-into-winform-richtextbox) –  Jun 20 '21 at 16:22
  • [How to: Load Files into the Windows Forms RichTextBox Control](https://learn.microsoft.com/dotnet/desktop/winforms/controls/how-to-load-files-into-the-windows-forms-richtextbox-control) –  Jun 20 '21 at 16:23
  • Have you tried `Richtextbox1.Lines = File.ReadAllLines(InputTextFile);`? – tinstaafl Jun 20 '21 at 16:24
  • reading large file above 39000 lines with Richtextbox1.Lines = File.ReadAllLines(InputTextFile); will hung the UI even i use backgroundworker, so i decide to add every 1000 lines to the richtextbox until finish all lines – Fath Bakri Jun 20 '21 at 16:56
  • 1
    I don't think reading in chunks of 1000 will really help any. What will your user do with 39000 lines of text on a text box? – Caius Jard Jun 20 '21 at 17:30
  • they want to replace and edit some text and save changes to file – Fath Bakri Jun 20 '21 at 19:23

1 Answers1

0

i use this code and it work fine:

private void Worker_DoWork(object sender, DoWorkEventArgs e)
        {
            
            
                try
            {
                //
                int ProgressPercentage = 0;
                
                
                
                BTN_Send_Ref.Invoke((MethodInvoker)(() =>
                {
                    BTN_Send_Ref.Enabled = true;
                }));

                  
              string[] Lines = File.ReadAllLines(InputTextFile);
                

                string LinesCollection = "";

                for(int x = 0; x < Lines.Length; x++)
                {

                    if (worker.CancellationPending)
                    {
                      
                        e.Cancel = true;
                        //cancel backgroundworker
                        return;
                    }
                    
                       LinesCollection +=  Lines[x] + Environment.NewLine;
                    
                    if (x>0 && x % 1000 == 0)
                    {
                        
                        RichTXT_OutText_Ref.Invoke((MethodInvoker)(() =>
                        {
                            RichTXT_OutText_Ref.AppendText(LinesCollection );
                        }));
                        LinesCollection = "";
                        LBL_CountOfChars_Ref.Invoke((MethodInvoker)(() =>
                        {
                            LBL_CountOfChars_Ref.Text = "(" + RichTXT_OutText_Ref.Text.Length + ") Char";
                            LBL_CountOfChars_Ref.Update();
                        }));

                        LBL_CountOfLines_Ref.Invoke((MethodInvoker)(() =>
                        {
                            LBL_CountOfLines_Ref.Text = "(" + RichTXT_OutText_Ref.Lines.Length + ") Line";
                            LBL_CountOfLines_Ref.Update();
                        }));
                    }

                    if(x==Lines.Length-1 && LinesCollection != "")
                    {
                        
                        RichTXT_OutText_Ref.Invoke((MethodInvoker)(() =>
                        {
                            RichTXT_OutText_Ref.AppendText(LinesCollection.TrimEnd(Environment.NewLine.ToCharArray()) );
                        }));
                        LinesCollection = "";
                        LBL_CountOfChars_Ref.Invoke((MethodInvoker)(() =>
                        {
                            LBL_CountOfChars_Ref.Text = "(" + RichTXT_OutText_Ref.Text.Length + ") Char";
                            LBL_CountOfChars_Ref.Update();
                        }));

                        LBL_CountOfLines_Ref.Invoke((MethodInvoker)(() =>
                        {
                            LBL_CountOfLines_Ref.Text = "(" + RichTXT_OutText_Ref.Lines.Length + ") Line";
                            LBL_CountOfLines_Ref.Update();
                        }));
                    }

                    ProgressPercentage = (int)Math.Round((((double)x) / Lines.Length) * 100);

                    if (ProgressPercentage < 100)
                    {
                        //report progress
                        worker.ReportProgress(ProgressPercentage);
                    }
                }
                
                //disable Correct button because
                //we are out of loop
                if (BTN_Send_Ref != null)
                {
                    
                    BTN_Send_Ref.Invoke((MethodInvoker)(() =>
                    {
                        BTN_Send_Ref.Enabled = false;
                    }));

                }

                
            }
            catch (Exception ex)
            {
               

                MessageBox.Show(ex.Message+"\n"+ex.StackTrace.ToString());
                throw ex;
            }
        }
Fath Bakri
  • 161
  • 1
  • 12