Tôi sử dụng mã sau đây để cấp phát một Bảng điều khiển cho ứng dụng WinForm. Cửa sổ Console hiển thị thành công và đầu ra ở đó. Nhưng khi tôi đóng cửa sổ Console, ứng dụng WinForm của tôi được đóng cùng một lúc. Tại sao? Tôi muốn giữ cửa sổ WinForm.Phân bổ bảng điều khiển cho ứng dụng WinForm
private void btn_to_console_Click(object sender, EventArgs e)
{
if (NativeMethods.AllocConsole())
{
lbl_console_alloc_result.Text = "Console allocation successfully!";
IntPtr stdHandle = NativeMethods.GetStdHandle(NativeMethods.STD_OUTPUT_HANDLE);
Console.WriteLine("from WinForm to Console!");
lbl_console_alloc_result.Text = Console.ReadLine();
}
else
lbl_console_alloc_result.Text = "Console allocation failed!";
}
[System.Runtime.InteropServices.DllImportAttribute("kernel32.dll", EntryPoint = "GetStdHandle")]
public static extern System.IntPtr GetStdHandle(Int32 nStdHandle);
/// Return Type: BOOL->int
[System.Runtime.InteropServices.DllImportAttribute("kernel32.dll", EntryPoint = "AllocConsole")]
[return: System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)]
public static extern bool AllocConsole();
Cảm ơn trước ...
Kiểm tra chủ đề này: http://stackoverflow.com/questions/533491/how-to-use-setconsolehandler-to-block-exit-calls –
cảm ơn, nobugz. Nó giúp. : D – smwikipedia