c# - Cleanest way to update user on thread status in multithreaded console application -
i have console application have made multithreaded. hope open thread in new console window, according this article, isn't possible/advisable such thing.
i have following:
do { selection = displaymenu(); switch (selection) { case 1: thread thread1 = new thread(() => method1(param1, param2, param3)); thread1.start(); break; case 2: thread thread2 = new thread(() => method2(param1, param2, param3)); thread2.start(); break; default: break; } } while (selection != 3);
method1
, method2
both have console.writeline()
calls display messages console window indicating user steps have completed, step running etc. works, except fact when displaymenu()
gets displayed after first thread run, kind of gets pushed top because of console
logging methods in switch
statement do, hence why wanted off load thread console window. suggestions?
update
this displaymenu()
does:
public static int displaymenu() { int selection = 0; console.writeline("what want do?"); console.writeline("[1] x"); console.writeline("[2] y"); console.writeline("[3] quit"); console.write("selection: "); selection = convert.toint32(console.readline()); console.writeline(); return selection ; }
update
per accepted answer, trying route console.writeline()
calls windows form instead. have added needed dependencies , windows form console project. have far.
public static void worktodo() { mycustomobj myobj = new mycustomobject(); var form = new outputform(); textbox tb = new textbox(); form.controls.add(tb); form.show(); //code not progress until window closed if (mycustomobject.validate()) { tb.text += "sent console app!"; //this console.writeline() call.
seems can't stream textbox
since process running form , not rest of code in console application.
why need this?
if not need debug second thread/console or share resources use system.diagnostics.process.start("*.exe");
launch additional console window, guessing not want?
you may better off using gui such wpf or windows forms multiple windows, 1 logging , user input? or possibly logging file?
it possible have child process hosting console window. detailed here: http://www.codeproject.com/articles/13368/multiple-consoles-for-a-single-application
create windows forms project , use gui tools add text box etc. see https://msdn.microsoft.com/en-us/library/dd492132.aspx more. edit code behind form so:
using system.windows.forms; namespace streamtextexample { using system.componentmodel; using system.threading; internal sealed partial class form1 : form { public form1() { initializecomponent(); } private void button1_click(object sender, system.eventargs e) { var timer = new backgroundworker(); timer.dowork += dowork; timer.runworkerasync(); } private void dowork(object sender, doworkeventargs doworkeventargs) { (var = 0; < 10; ++i) { var del = new addtext(addtextcallback); invoke(del, string.format("{0} seconds have passed.\r\n", i)); thread.sleep(1000); } } private void addtextcallback(string value) { textbox1.text += value; } private delegate void addtext(string value); } }
given button (button1) , text box (textbox1) add seconds passed since button1 pressed (up 9) text box.
Comments
Post a Comment