如何通过.NET执行Minitab命令?

时间:2010-07-01 18:49:04

标签: c# vb.net

Minitab帮助文件在有限的范围内提供有关此主题的支持,所有示例都在VB中。我是.NET的新手,但我很快就把它拿起来了。它是命令语法中的东西。

他们在VB中提供了这个例子:

Dim MtbApp As New mtb.Application
Dim MtbProj As mtb.Project
Dim MtbCom As mtb.Command
Dim i, j As Integer

MtbApp.UserInterface.Visible = True
Set MtbProj = MtbApp.ActiveProject
MtbProj.ExecuteCommand "RANDOM 30 C1 - C2"
MtbProj.ExecuteCommand "REGRESS C1 1 C2"

我的代码在C#

中看起来像这样
var MtbApp = new Mtb.Application();
var MtbProj = new Mtb.Project();
MtbProj = MtbApp.ActiveProject;
MtbApp.UserInterface.Visible = true;
MtbProj.ExecuteCommand(<command>);

我期待应该发生的是Minitab应该打开,命令应该执行。但是,最新发生的是Minitab的两个实例正在打开而且都没有显示用户界面,我必须在进程中找到它们。

1 个答案:

答案 0 :(得分:12)

假设您已添加对Minitab COM的引用,这应该可以帮助您入门:

Mtb.Application MtbApp = null;
Mtb.Project MtbProj = null;
Mtb.UserInterface MtbUI = null;

MtbApp = new Mtb.Application();
MtbProj = MtbApp.ActiveProject;
MtbUI = MtbApp.UserInterface;

MtbUI.Visible = true;
MtbProj.ExecuteCommand("RANDOM 30 C1-C2", Type.Missing); //with C# optional params required
MtbApp.Quit();

Marshal.ReleaseComObject(MtbUI); MtbUI = null;
Marshal.ReleaseComObject(MtbProj); MtbProj = null;
Marshal.ReleaseComObject(MtbApp); MtbApp = null;

将C对象用于C#可能会非常棘手。特别是当你完成后释放它们。

请记住,作为一般规则永远不会加倍。不要这样做:

MtbApp.UserInterface.Visible = true;

相反:

Mtb.UserInterface MtbUI = null;
MtbUI = MtbApp.UserInterface;
MtbUI.Visible = true;

因此,稍后可以释放MtbUI对象。