multithreading - C# - Live text feed from one thread to another -


in thread "a", want read long file, , happens, want send each new line read thread "b", -something- them.

basically, don't want wait file-loading finish before start processing lines. (i want 2 threads , communication between them; i've never done before , wanna learn)

so, how go doing this? thread should wait thread b finish processing "current line", before thread sends line thread b. won't efficient; how buffer in thread b?(to catch lines)

also, please give example of methods have use cross thread communication since haven't found/seen useful examples.

thank you.

first of all, it's not clear 2 threads useful here. single thread reading 1 line @ time (which pretty easy streamreader) , processing each line go might perform @ least well. file reads buffered, , os can read ahead of code requesting data, in case of reads either complete because next line has been read off disk in advance os, or both of threads have wait because data isn't there on disk. (and having 2 threads sat waiting disk doesn't make things happen faster having 1 thread sat waiting.) possible benefit avoid dead time getting next read underway before finish processing previous one, os in case. benefits of multithreading marginal @ best here.

however, since you're doing learning exercise, may not problem...

i'd use blockingcollection<string> mechanism passing data 1 thread another. (as long you're using .net 4 or later. , if not...i suggest move .net 4 - simplify task considerably.) you'll read line file , put collection 1 thread:

string nextline = myfilereader.readline(); myblockingcollection.add(nextline); 

and other thread can retrieve lines that:

while (true) {     string linetoprocess = myblockingcollection.take();     processline(linetoprocess); } 

that'll let reading thread run through file fast disk let it, while processing thread processes data @ whatever rate can. take method sits , waits if processing thread gets ahead of file reading thread.

one problem reading thread might way ahead if file large , processing slow - program might attempt read gigabytes of data file while having processed first few kilobytes. there's not point reading data way ahead of processing - want read little in advance. use blockingcollection<t>'s boundedcapacity property throttle things - if set number, call add block if collection has number of lines in it, , reading thread won't proceed until processing loop processes next line.

it interesting compare performance of program using two-threaded technique against 1 reads lines out of file , processes them in loop on single thread. able see what, if any, benefit multithreaded approach here.

incidentally, if processing cpu intensive, use variation on theme have multiple processing threads (and still single file-reading thread), because blockingcollection<t> happy have numerous consumers reading out of collection. of course, if order in finish processing lines of file matters, won't option, because although you'll start processing in right order, if have multiple processing threads, it's possible 1 thread might overtake one, causing out-of-order completion.


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -