c# console - 调用和监听 com 事件
c# console - call and listen to com events
我将如何读取用户输入、处理 com 事件以及从控制台应用程序中的用户输入调用 com 对象的某些函数?
我正在尝试拼凑以下内容:
static void Main(string[] args)
{
// Read user input
string input;
do
{
// Start thread for com here??
input = Console.ReadLine();
if (input == "Function1")
{
// Call Function1 on Com object
}
if (input == "Function2")
{
// Call Function2 on Com object
}
} while (input != null);
// Exit app
}
--
// Call com on separate thread
Thread thread = new Thread(MessagePumpThread);
thread.IsBackground = true;
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
--
void MessagePumpThread()
{
var activex = new activeXObject();
activex.CreateControl();
// Subscribe to events...
Application.Run();
}
我基本上想做在 windows 表单应用程序中但在控制台中很容易完成的事情。
非常感谢任何帮助,谢谢。
我用下面的代码得到了我想要的。我首先将 activex 控件导入 windows 表单应用程序以创建 dll 包装器,然后在控制台应用程序中使用它。 https://msdn.microsoft.com/en-us/library/ms973200.aspx
class Program
{
private static activeXControl _acx = new activeXControl();
[STAThread]
static void Main(string[] args)
{
// User input loop thread, use Ctrl + Z to exit loop
Thread thread = new Thread((ThreadStart)
delegate
{
string input;
do
{
input = Console.ReadLine();
if (string.IsNullOrEmpty(input))
{
continue;
}
switch (input)
{
case "Function1":
acx.Invoke(new Action(() => _acx.Function1()));
break;
case "Function2":
acx_.Invoke(new Action(() => acx_.Function2()));
break;
default:
Console.WriteLine("Method not found");
break;
}
} while (input != null);
});
thread.IsBackground = true;
thread.Start();
// Create control and subscribe to events
_acx.CreateControl();
_acx.Event1 += new System.EventHandler(acx_Event1);
_acx.Event2 += new System.EventHandler(acx_Event2);
// Start message loop
Application.Run();
}
private static void acx_Event1(object sender, EventArgs e)
{
// Write event output to console
}
private static void acx_Event2(object sender, EventArgs e)
{
// Write event output to console
}
}
希望这对某人有所帮助
我将如何读取用户输入、处理 com 事件以及从控制台应用程序中的用户输入调用 com 对象的某些函数?
我正在尝试拼凑以下内容:
static void Main(string[] args)
{
// Read user input
string input;
do
{
// Start thread for com here??
input = Console.ReadLine();
if (input == "Function1")
{
// Call Function1 on Com object
}
if (input == "Function2")
{
// Call Function2 on Com object
}
} while (input != null);
// Exit app
}
--
// Call com on separate thread
Thread thread = new Thread(MessagePumpThread);
thread.IsBackground = true;
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
--
void MessagePumpThread()
{
var activex = new activeXObject();
activex.CreateControl();
// Subscribe to events...
Application.Run();
}
我基本上想做在 windows 表单应用程序中但在控制台中很容易完成的事情。
非常感谢任何帮助,谢谢。
我用下面的代码得到了我想要的。我首先将 activex 控件导入 windows 表单应用程序以创建 dll 包装器,然后在控制台应用程序中使用它。 https://msdn.microsoft.com/en-us/library/ms973200.aspx
class Program
{
private static activeXControl _acx = new activeXControl();
[STAThread]
static void Main(string[] args)
{
// User input loop thread, use Ctrl + Z to exit loop
Thread thread = new Thread((ThreadStart)
delegate
{
string input;
do
{
input = Console.ReadLine();
if (string.IsNullOrEmpty(input))
{
continue;
}
switch (input)
{
case "Function1":
acx.Invoke(new Action(() => _acx.Function1()));
break;
case "Function2":
acx_.Invoke(new Action(() => acx_.Function2()));
break;
default:
Console.WriteLine("Method not found");
break;
}
} while (input != null);
});
thread.IsBackground = true;
thread.Start();
// Create control and subscribe to events
_acx.CreateControl();
_acx.Event1 += new System.EventHandler(acx_Event1);
_acx.Event2 += new System.EventHandler(acx_Event2);
// Start message loop
Application.Run();
}
private static void acx_Event1(object sender, EventArgs e)
{
// Write event output to console
}
private static void acx_Event2(object sender, EventArgs e)
{
// Write event output to console
}
}
希望这对某人有所帮助