sl@18: using System; sl@18: using System.Collections.Generic; sl@18: using System.ComponentModel; sl@18: using System.Data; sl@18: using System.Drawing; sl@18: using System.Linq; sl@18: using System.Text; sl@18: using System.Threading.Tasks; sl@18: using System.Windows.Forms; sl@18: using System.ServiceModel; sl@18: using System.ServiceModel.Channels; sl@31: using System.Diagnostics; sl@20: sl@18: sl@18: namespace SharpDisplayClient sl@18: { sl@18: public partial class MainForm : Form sl@18: { sl@26: Client iClient; sl@26: Callback iCallback; sl@18: sl@18: public MainForm() sl@18: { sl@18: InitializeComponent(); sl@18: } sl@18: sl@18: private void buttonSetText_Click(object sender, EventArgs e) sl@18: { sl@19: //iClient.SetText(0,"Top"); sl@19: //iClient.SetText(1, "Bottom"); sl@33: iClient.SetTexts(new string[] { textBoxTop.Text, textBoxBottom.Text }); sl@18: } sl@18: sl@18: private void MainForm_Load(object sender, EventArgs e) sl@18: { sl@31: iCallback = new Callback(this); sl@25: //Instance context is then managed by our client class sl@26: InstanceContext instanceContext = new InstanceContext(iCallback); sl@26: iClient = new Client(instanceContext); sl@18: sl@29: //Connect using unique name sl@32: //string name = DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss.fff tt"); sl@32: string name = "Client-" + (iClient.ClientCount() - 1); sl@32: iClient.SetName(name); sl@30: //Text = Text + ": " + name; sl@32: Text = "[[" + name + "]] " + iClient.SessionId; sl@18: sl@33: // sl@33: textBoxTop.Text = iClient.Name; sl@33: textBoxBottom.Text = iClient.SessionId; sl@33: sl@18: } sl@21: sl@31: sl@31: sl@31: public delegate void CloseConnectionDelegate(); sl@31: public delegate void CloseDelegate(); sl@31: sl@31: /// sl@31: /// sl@31: /// sl@31: public void CloseConnectionThreadSafe() sl@21: { sl@31: if (this.InvokeRequired) sl@29: { sl@31: //Not in the proper thread, invoke ourselves sl@31: CloseConnectionDelegate d = new CloseConnectionDelegate(CloseConnectionThreadSafe); sl@31: this.Invoke(d, new object[] { }); sl@29: } sl@31: else sl@31: { sl@31: //We are in the proper thread sl@31: if (IsClientReady()) sl@31: { sl@31: Trace.TraceInformation("Closing client: " + iClient.SessionId); sl@31: iClient.Close(); sl@31: Trace.TraceInformation("Closed client: " + iClient.SessionId); sl@31: } sl@29: sl@31: iClient = null; sl@31: iCallback = null; sl@31: } sl@26: } sl@26: sl@31: /// sl@31: /// sl@31: /// sl@31: public void CloseThreadSafe() sl@31: { sl@31: if (this.InvokeRequired) sl@31: { sl@31: //Not in the proper thread, invoke ourselves sl@31: CloseDelegate d = new CloseDelegate(CloseThreadSafe); sl@31: this.Invoke(d, new object[] { }); sl@31: } sl@31: else sl@31: { sl@31: //We are in the proper thread sl@31: Close(); sl@31: } sl@31: } sl@31: sl@31: sl@26: private void MainForm_FormClosing(object sender, FormClosingEventArgs e) sl@26: { sl@31: CloseConnectionThreadSafe(); sl@29: } sl@29: sl@29: public bool IsClientReady() sl@29: { sl@29: return (iClient != null && iClient.State == CommunicationState.Opened); sl@21: } sl@18: } sl@18: }