2016-06-12 33 views
0

Wie würde ich gehen, um Benutzereingaben zu lesen, com-Ereignisse zu behandeln und bestimmte Funktionen des com-Objekts aus Benutzereingaben in einer Konsolenanwendung aufzurufen?C# console - call and listen to com Ereignisse

Ich versuche, die folgende Stück zusammen:

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(); 
} 

ich im Wesentlichen tun will, was leicht in einem getan wird Windows-Formular-Anwendung, sondern in der Konsole.

Jede Hilfe wird sehr geschätzt, danke.

+1

Wenn Sie nicht bereits haben, sollten Sie wahrscheinlich COMs [Apartment Threading-Modell] bewertet (https://msdn.microsoft.com/en-us/library/ms809971 .aspx). – theB

Antwort

1

Ich bekam, was ich mit dem folgenden Code arbeiten wollte. Ich importierte zuerst das ActiveX-Steuerelement in eine Windows-Formularanwendung, um den Dll-Wrapper zu erstellen, den ich dann in der Konsolenanwendung verwendete. 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 
    } 
} 

Hoffnung hilft dieses jemand