multithreading - Threading in C# issue -
i have piece of code runs 1 method , runs same method again different parameters. method takes 45 seconds complete minute , half in total. looks like:
//start dbinfopackage migrateddata = runfirstqueries(connectsqlserver, log); dbinfopackage nonmigrateddata = runfirstqueries(connectoracle, log); //end
is possible run methods @ same time using threading?
just since people giving lower-level answers, using system.threading.thread
, whatnot, i'd do:
task<dbinfopackage> migrateddatatask = task.run<dbinfopackage>(() => runfirstqueries(connectsqlserver, log)); task<dbinfopackage> nonmigrateddatatask = task.run<dbinfopackage>(() => runfirstqueries(connectoracle, log)); task.waitall(migrateddatatask, nonmigrateddatatask); var migrateddata = migrateddatatask.result; var nonmigrateddata = nonmigrateddatatask.result;
that'll run them in 2 concurrent tasks, wait them both finish, , grab results. you'll want add in error handling, that's easy enough.
if can work workflow (and can), it's better use await task.whenall(migrateddatatask, nonmigrateddatatask)
instead of waitall
, used latter in example sustain thread-blocking behavior.
Comments
Post a Comment