0
私にはウィンドウがあり、コマンドウィンドウを持つプログラムWFAがあります。私はAllocConsole()でウィンドウを開きます。コンソールウィンドウを閉じると、FreeConsole()を使用します。しかし、私がAllocConsole()で再び開くと、私はそれを書いて読みたいと思って例外をスローします。 コード:ハンドルが無効です:それはがmscorlib.dll追加情報で「System.IO.IOException」を出し投げていることフォームアプリケーションでコンソールウィンドウを使用すると例外が発生する
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Management;
using System.Management.Instrumentation;
using System.Collections.Specialized;
using System.Threading;
using System.Runtime.InteropServices;
namespace WindowsFormsApplication2
{
class classx
{
[DllImport("kernel32.dll")]
public static extern Int32 AllocConsole();
[DllImport("kernel32.dll")]
public static extern bool FreeConsole();
[DllImport("kernel32")]
public static extern bool AttachConsole();
[DllImport("kernel32")]
public static extern bool GetConsoleWindow();
public static bool z = false;
[DllImport("kernel32")]
public static extern bool SetConsoleCtrlHandler(HandlerRoutine HandlerRoutine, bool Add);
public delegate bool HandlerRoutine(uint dwControlType);
}
public partial class Form1 : Form
{
NotifyIcon icontask;
Icon iconone_active;
Icon iconone_inactive;
/*Icon icontwo;
Icon iconthree;
Icon iconfour;
Icon iconfive;*/
Thread Threadworkermy;
public Form1()
{
InitializeComponent();
this.WindowState = FormWindowState.Minimized;
this.ShowInTaskbar = false;
iconone_active = new Icon(".../iconone_active.ico");
iconone_inactive = new Icon(".../iconone_inactive.ico");
icontask = new NotifyIcon();
icontask.Icon = iconone_active;
icontask.Visible = true;
Threadworkermy = new Thread(new ThreadStart(checkActivityThread));
Threadworkermy.Start();
MenuItem Nameapp = new MenuItem("xr");
MenuItem quitappitem = new MenuItem("quit program");
MenuItem OpenGUI = new MenuItem("Open GUI");
MenuItem Advancedmodewindow = new MenuItem("x");
ContextMenu contextmenu = new ContextMenu();
quitappitem.Click += quitappitem_click;
OpenGUI.Click += OpenGUI_click;
Advancedmodewindow.Click += Advancedmodewindow_click;
contextmenu.MenuItems.Add(Nameapp);
contextmenu.MenuItems[0].Enabled = false;
contextmenu.MenuItems.Add("-");
contextmenu.MenuItems.Add(OpenGUI);
contextmenu.MenuItems.Add(Advancedmodewindow);
contextmenu.MenuItems.Add("-");
contextmenu.MenuItems.Add(quitappitem);
icontask.ContextMenu = contextmenu;
icontask.Icon = iconone_active;
icontask.Visible = true;
}
private void Advancedmodewindow_click(object sender, EventArgs e)
{
classx.AllocConsole();
Console.WriteLine("X");
classx.FreeConsole();
}
private void OpenGUI_click(object sender, EventArgs e)
{
this.ShowInTaskbar = true;
this.WindowState = FormWindowState.Normal;
}
private void quitappitem_click(object sender, EventArgs e)
{
Threadworkermy.Abort();
icontask.Dispose();
this.Close();
}
public void checkActivityThread()
{
try
{
while(true)
{
Thread.Sleep(100);
}
} catch(ThreadAbortException tbe)
{
}
}
private void button1_Click(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Minimized;
this.ShowInTaskbar = false;
}
}
}
例外。 タイプを変更すると言っている人には、私はできません。
しないのはなぜ
CONSOLE.WRITELINE
と同様のコード作業(それがWFAのアプリケーションである必要がありますか)?
私はConsole.ReadLine()と入力しようとするので、その後の入力を行う方法。それは同じ例外をスローする – dkman
これは書き込みのために働く場合、コードを複製し、StreamWriterの代わりにStreamReaderを使用する必要があります – Armin
ThxはPCに戻るとすぐにコードに入れます – dkman