Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi
When I try converting text file it goes for over 2000 lines and hangup
can you explain why ?

Thanks in advance

Ajith

What I have tried:

DateTime datetime;

           OpenFileDialog choofdlog = new OpenFileDialog();
           choofdlog.Filter = "All Files (*.DAT)|*.DAT";
           choofdlog.FilterIndex = 1;
           choofdlog.Multiselect = true;
           string cToken;
           string cToken1;
           string cToken2;
           string cTime;
           string cDate;
           string cIdno;
           int nRecnt = 0;

           DataTable userdt = new DataTable();
           userdt = this.timeattDataSet.data  ;

           if (choofdlog.ShowDialog() == DialogResult.OK)
           {
               foreach (string sFileName in choofdlog.FileNames)
               {
                   using (FileStream fs = new FileStream(sFileName, FileMode.Open, FileAccess.Read))
                   {

                       StreamReader sr = new StreamReader(fs);

                       sr.BaseStream.Seek(0, SeekOrigin.Begin);

                       string str = sr.ReadLine();

                       while (str != null)

                       {

                           // Console.WriteLine(str);

                           str = sr.ReadLine();
                           if (str != null)
                           {
                               cToken1 = str.ToString().Substring(0, 9).Trim();
                               cToken2 = '5' + cToken1.PadLeft(5, '0');
                               cToken = cToken2.Substring(0, 6);
                               cDate = str.ToString().Substring(10, 10);
                               cTime = str.ToString().Substring(21, 2) + str.ToString().Substring(24, 2);
                               cIdno = str.ToString().Substring(30, 1);
                               datetime = System.Convert.ToDateTime(cDate);

                               nRecnt = nRecnt + 1;
                               this.textBox2.Text = nRecnt.ToString();
                               this.textBox2.Refresh();

                               string queryString = "Token_no = '" + cToken + "' AND Date = '" + datetime +"' AND Time ='" + cTime + "'";
                               bool rowExists = userdt.AsEnumerable().Any(row => string.Equals(row.Field<string>("Token_no"), cToken, StringComparison.OrdinalIgnoreCase) && row.Field<DateTime>("Date") == datetime && row.Field<string>("Time") == cTime);

                               if (rowExists == false)
                                   {
                                       this.textBox1.Text = cToken + '-'+ datetime;
                                       this.textBox1.Refresh();
                                       DataRow newCustomersRow = this.timeattDataSet.Tables["data"].NewRow();
                                       newCustomersRow["Token_no"] = cToken;
                                       newCustomersRow["Date"] = datetime;
                                       newCustomersRow["Time"] = cTime;
                                       newCustomersRow["Terminal"] = cIdno;
                                       newCustomersRow["Mode"] = "T";
                                       newCustomersRow["In_out"] = "";
                                       newCustomersRow["Updated"] = false;

                             }

                           }
                           else
                           {
                               break;
                           }
                       }

                       sr.Close();

                       fs.Close();

                   }
               }

           }
Posted
Updated 18-May-17 6:12am
Comments
ZurdoDev 18-May-17 11:56am    
What do you mean "hang up?" If you mean the UI becomes unresponsive that's because you are doing this on the UI thread so that's what will happen.
phil.o 18-May-17 12:05pm    
You should not call the ToString() method on a string variable.
And if there are a lot of files and/or they have a lot of lines, then moving the whole process to a background thread would leave your UI responsive.

1 solution

When you write a Windows application, it starts with one thread - which means that it can do one thing at a time. If you are busy reading and processing lines from a file, then your application can't do anything else until you finish doing that - and that includes not responding to user input, or requests to change the display: because Windows works by sending messages to your app which it processes when it gets back to it's "idle loop" and looks for something to do. If you are keeping it busy looking at lines from files, then it never runs out of things to do and never looks at it's collection of messages.

So instead you need to start a second thread to do all the "donkey work" behind the scenes. But that introduces it's own problems, because you can only access display controls from a single thread - the UI thread that your app started with. If you try to change the text on a TextBox from a different thread for example, you will get a "Cross Threading Exception" and you app will fail. There are ways around that - it's called Invoking the control - but that gets complicated to explain.

So instead, look at the BackgroundWorker Class (System.ComponentModel)[^] - it allows you to very simply set up a second thread, and report progress events back to the main thread so it can update the controls to reflect it. Have a look at the example, and also at the ProgressChangedEventArgs as there is a UserState property which lets you send back complicated info on how the job is progressing to your main thread.
 
Share this answer
 
Comments
Member 12931315 18-May-17 12:43pm    
Thanks Griff,Ryan,phil I will try that
OriginalGriff 18-May-17 14:00pm    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900