task to read big text files in C#

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 8217 Views - Last Post: 24 July 2011 - 10:29 AM Rate Topic: -----

#1 Esoo.S  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 23-July 11

task to read big text files in C#

Posted 23 July 2011 - 04:03 PM

Hi all,

I have big text file and I want to read it , i search for a soltuion and i found that i should use Threading , I don't know how to use Threading Reading Files in C#.

I know the simple way to read text file in C#

FileStream fs = new FileStream("My.txt", FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs);
while (sr.Peek() != -1)
{
    sr.ReadLine();
}



but i don't know how to use Threading To read the file with big size .

can any one help me quickly??

please it's important to me :(

Thanks in Advance

Is This A Good Question/Topic? 0
  • +

Replies To: task to read big text files in C#

#2 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: task to read big text files in C#

Posted 23 July 2011 - 04:23 PM

Whether or not you need threading depends on what you want to do with the file. Why do you need to read it? What do you consider a 'big text file'? Also, is this a console application or a Winforms application?

This post has been edited by Momerath: 23 July 2011 - 04:25 PM

Was This Post Helpful? 0
  • +
  • -

#3 Esoo.S  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 23-July 11

Re: task to read big text files in C#

Posted 23 July 2011 - 04:32 PM

I need Threading because it's required from my instructor , and i want to use Threading in reading a text file with big size like 1GB
I want to read this file because i am going to encrypt it using AES Algorithm (it's a task :D)
and it's a windows Form Application

Please if you can help me i will be Thankful to you :)

Thanks in Advance
Was This Post Helpful? 0
  • +
  • -

#4 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4425
  • View blog
  • Posts: 7,694
  • Joined: 08-June 10

Re: task to read big text files in C#

Posted 23 July 2011 - 04:44 PM

What version of .NET are you on? Both BackgroundWorker and Threading.Tasks are much easier to work with than raw Threads, and are still considered "threading".
Was This Post Helpful? 0
  • +
  • -

#5 Esoo.S  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 23-July 11

Re: task to read big text files in C#

Posted 23 July 2011 - 04:54 PM

I am using Visual Studio 2010 with .Net Framework 4

but i want to ask if i use Background worker is that will speed up the reading of the file ??

Thanks for Advance :)
Was This Post Helpful? 0
  • +
  • -

#6 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1252
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: task to read big text files in C#

Posted 23 July 2011 - 05:09 PM

If you're using .NET 4, I recommend using Tasks. It's cleaner to use and more straight forward. :)

There's a tutorial around here somewhere but I can't seem to find it.
Was This Post Helpful? 0
  • +
  • -

#7 Esoo.S  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 23-July 11

Re: task to read big text files in C#

Posted 23 July 2011 - 05:17 PM

can Tasks make me able to run the reading of my file in more than one thread ??
Was This Post Helpful? 0
  • +
  • -

#8 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6035
  • View blog
  • Posts: 23,417
  • Joined: 23-August 08

Re: task to read big text files in C#

Posted 23 July 2011 - 05:26 PM

Threading in C#.
Was This Post Helpful? 0
  • +
  • -

#9 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2240
  • View blog
  • Posts: 9,410
  • Joined: 29-May 08

Re: task to read big text files in C#

Posted 23 July 2011 - 05:42 PM

Threading isn't going solve all the problem, as the limiting factor is how fast can the hardware access the file. 1GB is a big file.
Was This Post Helpful? 1
  • +
  • -

#10 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4425
  • View blog
  • Posts: 7,694
  • Joined: 08-June 10

Re: task to read big text files in C#

Posted 23 July 2011 - 06:44 PM

There's a fundamental misunderstanding here. You're not going to be able to access the file on more than one thread, so you're not going to gain any speed loading it. What you will do is not freeze your GUI while loading it.

Also, how are you loading this? You'll get an OutOfMemoryException if you try to read the whole file into a string or string[].
Was This Post Helpful? 1
  • +
  • -

#11 Esoo.S  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 23-July 11

Re: task to read big text files in C#

Posted 24 July 2011 - 03:35 AM

ok, i will try to explain my task so you can help me.
I will have a big file that i should read it and then i am goning to convert the data that i read to byte array and then i will encrypt the data and then write it in another file.
the other part of task is to get the encrypted file and decrypt the data and write it in another file.
and i should use Threading in my task but i don't know exactly where should i use it??

can any one suggest a solution to me ??

Thanks for all of you :)
Was This Post Helpful? 0
  • +
  • -

#12 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6035
  • View blog
  • Posts: 23,417
  • Joined: 23-August 08

Re: task to read big text files in C#

Posted 24 July 2011 - 04:45 AM

Seems like this is simply a Producer-Consumer problem, which is mentioned in the link I provided, and there are certainly no shortage of examples out there on the Internet.
Was This Post Helpful? 0
  • +
  • -

#13 Esoo.S  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 23-July 11

Re: task to read big text files in C#

Posted 24 July 2011 - 05:47 AM

mmm The process that i do is to read a file into byte array ... but i try to run file with size 1.99 GB but the program gives me exception OutOfMemory.

what should i do to solve the problem of files with big sizes??

This post has been edited by Esoo.S: 24 July 2011 - 05:49 AM

Was This Post Helpful? 0
  • +
  • -

#14 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6035
  • View blog
  • Posts: 23,417
  • Joined: 23-August 08

Re: task to read big text files in C#

Posted 24 July 2011 - 07:39 AM

Why are you stuck on reading the entire file in at once? Read it in one chunk at a time, process the chunk, write out the processed chunk to a new file. I actually agree with what was said above; I don't think you need to use threading here, you just need to process the file as I just stated.
Was This Post Helpful? 0
  • +
  • -

#15 Esoo.S  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 23-July 11

Re: task to read big text files in C#

Posted 24 July 2011 - 08:14 AM

I will answer the question of why should i use Threading in my project ?? ...
First : My instructor said that we should use it to make the program run faster.(so it's required :( )
Second : I try the normal way to read chunk of file and i found that the time of just reading is 1 or 2 min (The file size is 1.99 GB)

if i can't use threading in reading the file , so where should i use it ??

I am sorry if i make you tired but really i an stuck on this problem and can't solve it :(

Thanks for all of you :)
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2