|  | 
 
 樓主|
發表於 13-1-31 11:25
|
顯示全部樓層 
| D大您好 我改了K大的程式碼 我的作法是
 1.直接只開一個Service(CATDDE->E-Leader) Topic -> FUTOPT<FO>TXFB3
 當我按btnAddConnect_Click啟動後直接開兩個ITEM(CurPrice, TickVol)
 
 
 public partial class DDEClientFrm : Form
 {
 private Hashtable ht_conn;   //保存以 (連線字串) 為 key; 所建立的 DdeClient instance 為 object.
 //private Hashtable ht_item;    //保存以 (連線字串+項目名稱) 為 key; 項目值為 object
 private Hashtable ht_gdv;    // 保存以 (連線字串+項目名稱) 為 key;
 private Queue myQueue = new Queue();   // 保存DDEClient 收到的 Item
 public DDEClientFrm()
 {
 InitializeComponent();
 ht_conn = new Hashtable();
 //ht_item  = new Hashtable();
 ht_gdv   = new Hashtable();
 //Queue myQueue = new Queue();
 }
 
 /** 新增 DDE連線 的事件處理
 *
 */
 private void btnAddConnect_Click(object sender, EventArgs e)
 {
 if (ht_conn.ContainsKey(txtService.Text + "|" + txtTopic.Text))
 {
 MessageBox.Show("連線字串不能重覆!");
 return;
 }
 DdeClient dc = new DdeClient(txtService.Text, txtTopic.Text);
 //register the event handler
 dc.Disconnected += client_Disconnected;
 dc.Advise += client_Advise;
 try
 {
 // Connect to the server.  It must be running or an exception will be thrown.
 dc.Connect();
 
 dgConnection.Rows.Add(txtService.Text, txtTopic.Text, "已連線");
 
 //利用 "service|topic" 為 HashTable 的 Key; DdeClient 為 Object.
 string key = txtService.Text + "|" + txtTopic.Text;
 ht_conn.Add(key, dc);
 
 }
 catch (Exception thrown)
 {
 MessageBox.Show("無法連結 DDE Server:" + thrown.Message);
 }
 /* Added By Jerry
 * Add Items
 * End Added By Jery*/
 this.AddItem(dc, "CurPrice");    //新增 Item
 
 this.AddItem(dc, "TickVol");    //新增 Item
 }
 
 2. 在 client_Advise 中把收到的item 直接放進myQueue中
 private void client_Advise(object sender, DdeAdviseEventArgs args)
 {
 DdeClient dc = (DdeClient)sender;
 ValueItem it = new ValueItem();
 it.item = args.Item;
 it.value = args.Text;
 it.time = DateTime.Now;
 
 myQueue.Enqueue(it);
 
 }
 
 3.目前測試中所以一直讓QUEUE自動增加到我按停止連線同時Output 整個Queue
 void BtnStopConnectClick(object sender, EventArgs e)
 {
 string key = txtService.Text + "|" + txtTopic.Text;
 DdeClient client_conn = (DdeClient)ht_conn[key];
 
 this.Stop_advise(client_conn, "CurPrice");
 this.Stop_advise(client_conn, "TickVol");
 
 
 //刪除所儲存連線資訊的 key/value, 以及將該 DdeClient 連線移除。
 ht_conn.Remove(key);
 client_conn.Dispose();
 
 //將所在的連線資料列刪除掉
 dgConnection.Rows.RemoveAt(0);
 
 using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"R:\DDE.txt"))
 
 while (myQueue.Count > 0)
 {
 ValueItem it = (ValueItem) myQueue.Dequeue();
 file.WriteLine(it.item);
 file.WriteLine(it.value);
 file.WriteLine(it.time);
 }
 
 }
 
 
 | 
 |