2017-06-12 73 views
0

我正在爲CAM/CAD軟件編寫插件/界面,並使用此代碼打開「SaveWindow」。如何不能在CAM界面上同時打開多個窗口

public void Run(string theMode) 
    { 
    try 
    { 
     if (theMode == "SaveWindow") 
     { 
      string aPictureString = GetPictureString(); 
      StartInterface(null, theMode, CreateAndSaveTheToolList(aPictureString)); 
     } 
     else 
     { 
      string aPipeId = GetRandomString(); 
      itsServerStream = new NamedPipeServerStream(aPipeId, PipeDirection.In, 1); 
      ThreadPool.QueueUserWorkItem(this.ListenToStream); 
      StartInterface(aPipeId, theMode, ""); 
      StartNamedPipe(aPipeId); 
      itsRefreshThread = new Thread(this.RefreshTools); 
      itsRefreshThread.Start(); 

      if (!InitLogger(Path.GetDirectoryName(this.GetType().Assembly.Location))) 
      { 
       MessageBox.Show(//Secured code); 
       return; 
      } 
     } 
     itsLogger.Info("Run execute was successful."); 
    } 
    catch (Exception aException) 
    { 
     //Secured code 
    } 
    LogManager.ResetConfiguration(); 
    } 

如果有一個接口打開,我再次單擊插件按鈕,它會打開另一個多個。如果第一個打開,我該如何編碼才能打開第二個。

+0

最好的是要求支持它,因爲也許你錯過了某種「獨家」插件選項。否則,你可以嘗試IPC同步,例如[命名互斥體](https://stackoverflow.com/q/2186747/1997232)。 – Sinatr

回答

1

您目前的情況是一樣的東西

if (condition) 
{ 
    // Open your window 
} 
else 
{ 
    // Do something else 
} 

所以每次你滿足你的條件你的窗口的另一個實例被打開。

可以解決這個問題通過檢查你的窗口是否已經打開這樣

bool isOpen = false; 

if (!isOpen) 
{ 
    // The window isn't open so open it 
    isOpen = true; 
} 
else 
{ 
    // The window is already open so don't open it again 
} 

在這種情況下,問題是在情況下,當你的條件得到滿足,但你的窗戶是打開的,你想什麼去做?

只需添加isOpen檢查您打開的窗口路徑這樣

if (condition && !isOpen) 
{ 
    // Open your window 
    isOpen = true; 
} 
else 
{ 
    // Do something else 
} 

是否意味着,只要您滿足條件,且窗口已經打開您的代碼將「做別的事情。」

另一種方法是這樣的

if (condition) 
{ 
    if (!isOpen) 
    { 
     // Open your window 
     isOpen = true; 
    } 
    else 
    { 
     // Do something else 
    } 
} 
else 
{ 
    // Do something else 
} 

這意味着,當你的條件滿足你打開窗口,如果它是不開放的,做其他事,如果它是。那麼你的第三種情況是第二種「做別的事情」的路徑,當條件不滿意時。

+0

@感謝Gareth。問題已解決。我沒有正確解釋,但我想通了。無論如何,謝謝你的回答。 – rapsoulCecil

相關問題