using System; using System.Threading; using System.Collections; using System.Data; using System.Xml; namespace BSS.PocketServer { public enum DataMessageAction { NewTicket, AddItem } public struct DataMessage { public DataMessageAction Action; public int TranNbr; public string Identifier; } /// <summary> /// Monitors for transactions which need to be submitted to BPA via the web service. /// Receives results and updates local data tables appropriately. /// </summary> public class DataLink { const int SLEEPINTERVAL = 250; public bool abortflag = false; public ArrayList alQueue = new ArrayList(); private Thread threadMonitor; public DataLink() { // // TODO: Add constructor logic here // ThreadStart ts = new ThreadStart(MonitorThread); threadMonitor = new Thread(ts); threadMonitor.Start(); } // // Add an item to the send queue // public void AddItem(DataMessageAction Action, int TranNbr, string Identifier) { DataMessage dm = new DataMessage(); dm.Action = Action; dm.TranNbr = TranNbr; dm.Identifier = Identifier; lock(alQueue) { alQueue.Add(dm); } } //--------------------------------------------------------------- // Thread functions //--------------------------------------------------------------- // // Primary thread loop. // Process all outstanding messages. If there are none, go to sleep. // public void MonitorThread() { do { if (alQueue.Count > 0) { DataMessage dm = (DataMessage)alQueue[0]; // ...... Processing code to call web service here // the following code simulates some processing ......... Thread.Sleep(5000); System.Windows.Forms.MessageBox.Show("Finished - Data thread"); } else { Thread.Sleep(SLEEPINTERVAL); } } while (abortflag == false); } } }Messages are sent using the following code:
// // Send message to DataLink thread // DataMessage dm; dm.Action = DataMessageAction.AddItem; dm.Identifier = iNewCount.ToString(); dm.TranNbr = m_tran_nbr; Global.oDataLink.SendMessage(dm); MessageBox.Show("Finished - main thread");