Friday, 15 August 2014

c# 4.0 - Why is parallel.Invoke not working in this case -



c# 4.0 - Why is parallel.Invoke not working in this case -

i have array of files this..

string[] unzippedfiles; thought want parse these files in paralle. parsed record gets placed on concurrentbag. record getting placed want kick of update function.

here doing in main():

foreach(var file in unzippedfiles) { parallel.invoke ( () => importfiles(file), () => updatetest() ); }

this code of update loooks like.

static void updatetest( ) { console.writeline("updating/inserting merchant information."); while (!merchcollection.isempty || producingrecords ) { merchant x; if (merchcollection.trytake(out x)) { update_merchant(x.m_id, x.minfo, x.month, x.year); } } }

this import code looks like. it's pretty much giant string parser.

system.io.streamreader sr = new system.io.streamreader(filename); long counter = 0; stringbuilder contents = new stringbuilder( ); string m_id = ""; string bof_delimiter = "%%ms_skey_0000_000_pdf:"; string eof_delimiter = "%%eof"; seek { record_count = 0; producingrecords = true; (counter = 0; counter <= sr.basestream.length - 1; counter++) { if (sr.endofstream) { break; } contents.appendline(strings.trim(sr.readline())); contents.appendline(system.environment.newline); //contents += strings.trim(sr.readline()); //contents += strings.chr(10); if (contents.tostring().indexof((eof_delimiter)) > -1) { if (contents.tostring().startswith(bof_delimiter) & contents.tostring().indexof(eof_delimiter) > -1) { string info = contents.tostring(); m_id = data.substring(data.indexof("_m") + 2, data.substring(data.indexof("_m") + 2).indexof("_")); console.writeline("merchant: " + m_id); merchant newmerch; newmerch.m_id = m_id; newmerch.minfo = data.substring(0, (data.indexof(eof_delimiter) + 5)); newmerch.month = datetime.now.addmonths(-1).month; newmerch.year = datetime.now.addmonths(-1).year; //update(newmerch); merchcollection.add(newmerch); } contents.clear(); //gc.collect(); } } sr.close(); // updatetest(); } grab (exception ex) { producingrecords = false; } { producingrecords = false; } }

the problem having update runs 1 time , importfile function takes on , not yield update function. ideas on doing wrong of great help.

here's stab @ fixing thread synchronisation. note haven't changed of code functional standpoint (with exception of taking out catch - it's bad idea; exceptions need propagated).

forgive if doesn't compile - i'm writing based on incomplete snippets.

main

foreach(var file in unzippedfiles) { using (var merchcollection = new blockingcollection<merchant>()) { parallel.invoke ( () => importfiles(file, merchcollection), () => updatetest(merchcollection) ); } }

update

private void updatetest(blockingcollection<merchant> merchcollection) { console.writeline("updating/inserting merchant information."); foreach (merchant x in merchcollection.getconsumingenumerable()) { update_merchant(x.m_id, x.minfo, x.month, x.year); } }

import

don't forget pass in merchcollection parameter - should not static.

system.io.streamreader sr = new system.io.streamreader(filename); long counter = 0; stringbuilder contents = new stringbuilder( ); string m_id = ""; string bof_delimiter = "%%ms_skey_0000_000_pdf:"; string eof_delimiter = "%%eof"; seek { record_count = 0; (counter = 0; counter <= sr.basestream.length - 1; counter++) { if (sr.endofstream) { break; } contents.appendline(strings.trim(sr.readline())); contents.appendline(system.environment.newline); //contents += strings.trim(sr.readline()); //contents += strings.chr(10); if (contents.tostring().indexof((eof_delimiter)) > -1) { if (contents.tostring().startswith(bof_delimiter) & contents.tostring().indexof(eof_delimiter) > -1) { string info = contents.tostring(); m_id = data.substring(data.indexof("_m") + 2, data.substring(data.indexof("_m") + 2).indexof("_")); console.writeline("merchant: " + m_id); merchant newmerch; newmerch.m_id = m_id; newmerch.minfo = data.substring(0, (data.indexof(eof_delimiter) + 5)); newmerch.month = datetime.now.addmonths(-1).month; newmerch.year = datetime.now.addmonths(-1).year; //update(newmerch); merchcollection.add(newmerch); } contents.clear(); //gc.collect(); } } sr.close(); // updatetest(); } { merchcollection.completeadding(); } }

c#-4.0 task-parallel-library parallel.foreach

No comments:

Post a Comment