2012-12-20 64 views
-4

我正在使用Windows窗體。我有兩個應用程序,一個是父母,另一個是孩子。在我的主應用程序中,我有一堆記錄的網格。用戶需要選擇一個記錄並單擊按鈕,我需要的是所選記錄的數據發送到子應用程序像參數,在兩個應用程序之間傳遞數據

Process.Start("path to child exe","selected record's data"); 

之後,我會得到發送數據到子應用程序,

Environment.GetEnvironmentVariables()[0]; 

現在當父數據發送處理完成後,子應用程序需要自動關閉。之後,在父應用程序中,所選的gridview行需要向前移動一行並將選定的行信息發送給子應用程序,並且該過程需要迭代,直到遍歷主應用程序中的gridview中的所有記錄。

+2

好了,有什麼問題嗎? – ken2k

+0

我怎麼能在使用C#的窗體中實現這一點。請指導我。 –

+0

是否可以/允許修改子應用程序? –

回答

2

好了,首先你會注意到,

Process.Start() 

返回到具有Exited事件System.Diagnostics.Process對象的引用。 然後你就可以將一個處理器,將繼續到另一行,並再次重新啓動過程:

var process = Process.Start("path to child exe","selected record's data"); 
process.Exited += (sender, args) => 
{ 
    // do your stuff 
} 

然後,我建議檢查其他的選項要做到這一點:

  • 這是一個。淨託管應用程序?你可以在項目中引用.exe程序集並通過提供的接口調用必要的方法嗎?
  • 如果這是一個非託管Windows應用程序,您仍然可以調用它的方法。使用的DllImport爲:

    [DllImport(LibraryName, EntryPoint = FunctionName)]

    private static extern int Function(string param);

然後你就可以從你的代碼中調用功能。 Google瞭解詳情。

此外,看一看:How to call a Managed DLL File in C#?

0

假設你不需要從子應用程序將數據傳遞迴主:

在你的主應用程序創建一個遞歸的方法來啓動應用程序兒童用參數。在我的例子listBox1包含數據項的列表:

private void button1_Click(object sender, EventArgs e) 
{ 
    listBox1.SelectedIndex = 0; 
    startChild((string)listBox1.SelectedItem); 
} 

void startChild(string data) 
{ 
    ProcessStartInfo psi = new ProcessStartInfo("child.exe", data); 
    Process p = Process.Start(psi); 
    p.WaitForExit(); 
    if (p.HasExited) 
    { 
     if ((listBox1.SelectedIndex + 1) < listBox1.Items.Count) 
     { 
      listBox1.SelectedIndex++; 
      startChild((string)listBox1.SelectedItem); 
     } 
    } 
} 

然後在你的孩子應用拿起參數並對其進行處理:

public Child() 
{ 
    InitializeComponent(); 
    var data = Environment.GetCommandLineArgs()[1]; 
    ProcessDataAndExit(data); 
} 
+0

但是我個人認爲我會沿着WCF路線走(因爲它更有趣) – StaWho

相關問題