2015-04-26 55 views
0

我是我從使用模板從http://www.ctsfutures.com/wiki/T4%20API%2040.MainPage.ashx更新從GUI線程的委託方法,無需從GUI

這裏寫一個例子C#程序是一些代碼調用事件的值:

聲明moAccounts這是從他們的API

internal AccountList moAccounts; 

創建委託

private delegate void OnPositionUpdateDelegate(T4.API.Position poPosition); 
初始化的

()內將註冊

moAccounts.PositionUpdate += new T4.API.AccountList.PositionUpdateEventHandler(moAccounts_PositionUpdate); 

我相信當進入一個貿易和位置更新了這個被調用。位置就好像你是在100萬股谷歌等..所以它也不過100

//' Event that is raised when positions for accounts have changed. 
    private void moAccounts_PositionUpdate(AccountList.PositionUpdateList poPositions) 
    { 

     // Display the position details. 

     { 

      foreach (AccountList.PositionUpdateList.PositionUpdate oUpdate in poPositions) 
      { 
       // If the position is for the current account 
       // then update the value. 

       if (object.ReferenceEquals(oUpdate.Account, moAccount)) 
       { 
        // Invoke the update. 
        // This places process on GUI thread. 
        // Must use a delegate to pass arguments. 
        if (this.InvokeRequired) 
        { 
         this.BeginInvoke(new OnPositionUpdateDelegate(OnPositionUpdate), new object[] { oUpdate.Position }); 
        } 
        else 
        { 
         OnPositionUpdate(oUpdate.Position); 
        } 

        break; // TODO: might not be correct. Was : Exit For 

       } 

      } 
     } 

    } 

然後調用此

 private void OnPositionUpdate(T4.API.Position poPosition) 
    { 

     if (object.ReferenceEquals(poPosition.Market, moMarket)) 
     { 

      // Display the position details. 
      DisplayPosition(poPosition.Market, 1); 
     } 

    } 

然後這個被調用其更新保存在文本框職位數

private void DisplayAccount(Account poAccount) 
      { 

       if ((moAccount != null)) 
       { 

        try 
        { 
         // Lock the host while we retrive details. 
         moHost.EnterLock("DisplayAccount"); 

         // Display the current account balance. 
         txtCash.Text = String.Format("{0:#,###,##0.00}", moAccount.AvailableCash); 

        } 
        catch (Exception ex) 
        { 
         // Trace the error. 
         Trace.WriteLine("Error: " + ex.ToString()); 

        } 
        finally 
        { 
         // Unlock the host object. 
         moHost.ExitLock("DisplayAccount"); 

        } 

       } 

      } 


      private void DisplayPosition(Market poMarket, int piID) 
      { 
       string strNet = ""; 
       string strBuys = ""; 
       string strSells = ""; 

       bool blnLocked = false; 

       try 
       { 

        if ((poMarket != null) && (moAccount != null)) 
        { 
         // Lock the host while we retrive details. 
         moHost.EnterLock("DisplayPositions"); 

         // Update the locked flag. 
         blnLocked = true; 


         // Temporary position object used for referencing the account's positions. 
         Position oPosition = default(Position); 

         // Display positions for current account and market1. 

         // Reference the market's positions. 
         oPosition = moAccount.Positions[poMarket.MarketID]; 

         Trace.WriteLine("Inside of DisplayPosition: " + oPosition.ToString()); 

         if ((oPosition != null)) 
         { 
          // Reference the net position. 
          strNet = oPosition.Net.ToString(); 
          strBuys = oPosition.Buys.ToString(); 
          strSells = oPosition.Sells.ToString(); 

          // this part is what I added 
          // this is an int value 
          _positionQuantity = oPosition.Net; 

         } 

         switch (piID) 
         { 
          case 1: 
           // Display the net position. 
           txtNetPosition.Text = strNet; 
           break; 
         } 

        } 

       } 
       catch (Exception ex) 
       { 
        // Trace the error. 
        Trace.WriteLine("Error " + ex.ToString()); 

       } 
       finally 
       { 
        // Unlock the host object. 
        if (blnLocked) 
         moHost.ExitLock("DisplayPositions"); 

       } 

      } 

我注意到如果我從Trace.WriteLine調用_positionQuantity,它不會更新。但是,當我從MessageBox.Show(_positionQuantitiy)調用它時,它會更新。

我的問題是我有內部計算,必須完成沒有GUI項目。我怎樣才能不必從一個GUI事件類型的東西把它更新_positionQuantity,只是有更新它,當我需要它

感謝 斯賓塞

回答

0

更新到Windows窗體控件必須創建在同一線程上進行他們。 (這是,他們有線程關聯)。爲了執行在UI線程上的委託,調用形式的Invoke方法,像這樣:

myForm.Invoke(() => { 
         doThis(someControl); 
         doThat(anotherControl); 
         doTheOther(anotherControl, 42); 
        }); 

在大括號中的東西被稱爲匿名方法。單行那些工作了,當然:

myForm.Invoke(() => {someControl.Text = "Threads!";}); 

如果您要立即更新形式的視覺狀態,您可以使用theForm.Refresh();