如何使用Process.Start("outlook.exe")运行Outlook并取回控件 [英] How to run Outlook using Process.Start("outlook.exe") and get the control back

查看:88
本文介绍了如何使用Process.Start("outlook.exe")运行Outlook并取回控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的C#程序需要启动Office Outlook并获取当前的正在运行的Outlook应用程序". 为此,我实现了以下简单程序(因此,如果您愿意,可以对其进行简单测试):

My C# program needs to launch Office Outlook and get the current "running outlook application". In order to do that I've implemented the following simple program (so if you want you can test it simply):

using Outlook = Microsoft.Office.Interop.Outlook;
using System.Runtime.InteropServices;

static void Main(string[] args)
{
  Outlook.Application outlookObj = null;

  if (Process.GetProcessesByName("OUTLOOK").Count().Equals(0))
  {
    Process.Start("outlook.exe"); // MY PROGRAM STOPS HERE
  }
  var process = Process.GetProcessesByName("OUTLOOK").First();
  while (!process.HasExited)
  {
    try
    {
      outlookObj = (Outlook.Application)Marshal.GetActiveObject("Outlook.Application");
      break;
    }
    catch
    {
      outlookObj = null;
    }
    System.Threading.Thread.Sleep(10);
  }

  string result = (outlookObj== null)? "DOES NOT WORK" : "OK";
  Console.WriteLine(result);
  Console.ReadLine();
}

我的问题是,一旦Office Outlook开始运行,我的C#控制台应用程序就不会继续其工作.在执行Process.Start("outlook.exe");指令之后,我必须单击Visual Studio GUI才能重新启动控制台应用程序,并最终在控制台应用程序上阅读确定".

My problem is that once Office Outlook starts running then my C# console application does not continue its job. After the Process.Start("outlook.exe"); instruction is executed then I must click on Visual Studio GUI in order to restart the console application and finally read "OK" on my console application.

我该如何解决我的问题?

How can I solve my problem?

推荐答案

Microsoft编写了

Microsoft wrote a example about how to log into a outlook instance. Although this is directly what you asked for in your question, the example contains how to start a new outlook application in the intended way

application = new Outlook.Application();

作为旁注:在您的示例中,使用以下代码:

as a side note: in your example you use the following code:

 while (!process.HasExited)
  {
    try
    {
      outlookObj = (Outlook.Application)Marshal.GetActiveObject("Outlook.Application");
      break;
    }
    catch
    {
      outlookObj = null;
    }
    System.Threading.Thread.Sleep(10);
  }

这是在主线程中的不良做法,因为您通过使用thread.sleep来进行忙碌等待".这意味着1.您将在应用程序不执行任何操作时使用CPU电源. 2.使您的GUI完全无响应,并且如果多次调用thread.sleep,Windows会建议关闭进程(整个屏幕变白,最终出现一个弹出窗口,询问您是否要等待还是将其关闭). .net框架中有很多方法可以防止这两个问题(例如使用等待句柄,后台工作程序或锁定)

This is bad practice in your main thread as your applying 'busy waiting' by using the thread.sleep. This means you will 1. use CPU power while your application is doing nothing. 2. make your GUI completely unresponsive and if the thread.sleep is called to many times Windows will suggest to shut the process down (the whole screen gets white and eventually you get a popup asking you if you want to wait or just shut it down). There are plenty of ways in the .net framework to prevent both of these issues (for example using a waithandle, background worker or locking)

这篇关于如何使用Process.Start("outlook.exe")运行Outlook并取回控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆