How would I go about reading user input, handling com events and calling certain functions of the com object from user input in a console application?
I'm trying to piece together the following:
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();
}
I essentially want to do what is easily done in a windows form application but in the console.
Any help is greatly appreciated, thanks.
I got what I wanted working with the following code. I first imported the activex control into a windows form application to create the dll wrapper which I then used in the console application. 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
}
}
Hope this helps someone