c# - Show Form from hidden console application -
i have main application runs console application. console application started hidden (processwindowstyle.hidden
), testing purposes can run window shown.
within console application can have plugins loaded , executed. 1 of plugins tries open winform dialog. works fine if console application visible, doesn't work more if console hidden.
i have tried:
application.enablevisualstyles(); application.setcompatibletextrenderingdefault(false); application.run(new form());
and tried same in new thread.
thread t = new system.threading.thread(start); t.start(); t.join();
where start()
contains things before. in addition tried showdialog()
application.enablevisualstyles(); application.setcompatibletextrenderingdefault(false); var f = new form(); f.showdialog();
none of methods showed window.
in windbg, native callstack includes ntuserwaitmessage()
:
0:000> k childebp retaddr 0038dd58 7b0d8e08 user32!ntuserwaitmessage+0x15
and managed callstack includes waitmessage()
, fpushmessageloop()
, runmessageloop()
:
0:000> !clrstack os thread id: 0x47c4 (0) esp eip 0045e560 76bff5be [inlinedcallframe: 0045e560] system.windows.forms.unsafenativemethods.waitmessage() 0045e55c 7b0d8e08 system.windows.forms.application+componentmanager.system.windows.forms.unsafenativemethods.imsocomponentmanager.fpushmessageloop(int32, int32, int32) 0045e5f8 7b0d88f7 system.windows.forms.application+threadcontext.runmessageloopinner(int32, system.windows.forms.applicationcontext) 0045e64c 7b0d8741 system.windows.forms.application+threadcontext.runmessageloop(int32, system.windows.forms.applicationcontext) 0045e67c 7b5ee597 system.windows.forms.application.rundialog(system.windows.forms.form) 0045e690 7b622d98 system.windows.forms.form.showdialog(system.windows.forms.iwin32window) 0045e71c 7b622faf system.windows.forms.form.showdialog()
how can show winforms form hidden console window?
sscce:
compile windows form application:
public partial class form1 : form { public form1() { initializecomponent(); } private void button1_click(object sender, eventargs e) { var startinfo = new processstartinfo("console.exe"); startinfo.windowstyle = processwindowstyle.hidden; process.start(startinfo); } }
compile console application:
class program { static void main(string[] args) { application.enablevisualstyles(); application.setcompatibletextrenderingdefault(false); var mainform = new form(); // enable next line make show // mainform.visible = true; application.run(mainform); } }
using winspector spy found out window available, doesn't have ws_visible
style. applying style form made visible , shown in windows task bar.
the solution make form visible before showing it, following worked:
application.enablevisualstyles(); application.setcompatibletextrenderingdefault(false); var f = new form() f.visible = true; application.run(f);
because in case wanted return value, should call showdialog()
. however, calling showdialog()
on visible form not allowed, sticked application.run(f)
, retrieved result myself:
var answer = configform.dialogresult;
Comments
Post a Comment