Client/MainForm.cs
author sl
Fri, 15 Aug 2014 11:11:17 +0200
changeset 31 f19b04646b6a
parent 30 c375286d1a1c
child 32 4c416d2878dd
permissions -rw-r--r--
Fixing our client issue with static MainForm overwritten when using multiple clients.
That was basically our issue with broadcast not working the way it should.
     1 using System;
     2 using System.Collections.Generic;
     3 using System.ComponentModel;
     4 using System.Data;
     5 using System.Drawing;
     6 using System.Linq;
     7 using System.Text;
     8 using System.Threading.Tasks;
     9 using System.Windows.Forms;
    10 using System.ServiceModel;
    11 using System.ServiceModel.Channels;
    12 using System.Diagnostics;
    13 
    14 
    15 namespace SharpDisplayClient
    16 {
    17     public partial class MainForm : Form
    18     {
    19         Client iClient;
    20         Callback iCallback;
    21 
    22         public MainForm()
    23         {
    24             InitializeComponent();
    25         }
    26 
    27         private void buttonSetText_Click(object sender, EventArgs e)
    28         {
    29             //iClient.SetText(0,"Top");
    30             //iClient.SetText(1, "Bottom");
    31             iClient.SetTexts(new string[] { iClient.Name, iClient.SessionId });
    32         }
    33 
    34         private void MainForm_Load(object sender, EventArgs e)
    35         {
    36             iCallback = new Callback(this);
    37             //Instance context is then managed by our client class
    38             InstanceContext instanceContext = new InstanceContext(iCallback);
    39             iClient = new Client(instanceContext);
    40 
    41             //Connect using unique name
    42             string name = DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss.fff tt");
    43             iClient.Connect(name);
    44             //Text = Text + ": " + name;
    45             Text = Text + ": " + iClient.SessionId;
    46 
    47         }
    48 
    49 
    50        
    51         public delegate void CloseConnectionDelegate();
    52         public delegate void CloseDelegate();
    53 
    54         /// <summary>
    55         /// 
    56         /// </summary>
    57         public void CloseConnectionThreadSafe()
    58         {
    59             if (this.InvokeRequired)
    60             {
    61                 //Not in the proper thread, invoke ourselves
    62                 CloseConnectionDelegate d = new CloseConnectionDelegate(CloseConnectionThreadSafe);
    63                 this.Invoke(d, new object[] { });
    64             }
    65             else
    66             {
    67                 //We are in the proper thread
    68                 if (IsClientReady())
    69                 {
    70                     //iClient.Disconnect();
    71                     Trace.TraceInformation("Closing client: " + iClient.SessionId);
    72                     iClient.Close();
    73                     Trace.TraceInformation("Closed client: " + iClient.SessionId);
    74                 }
    75 
    76                 iClient = null;
    77                 iCallback = null;
    78             }
    79         }
    80 
    81         /// <summary>
    82         /// 
    83         /// </summary>
    84         public void CloseThreadSafe()
    85         {
    86             if (this.InvokeRequired)
    87             {
    88                 //Not in the proper thread, invoke ourselves
    89                 CloseDelegate d = new CloseDelegate(CloseThreadSafe);
    90                 this.Invoke(d, new object[] { });
    91             }
    92             else
    93             {
    94                 //We are in the proper thread
    95                 Close();
    96             }
    97         }
    98 
    99 
   100         private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
   101         {
   102             if (IsClientReady()) //Could catch exception instead
   103             {
   104                 iClient.Disconnect();
   105             }
   106 
   107             CloseConnectionThreadSafe();
   108         }
   109 
   110         public bool IsClientReady()
   111         {
   112             return (iClient != null && iClient.State == CommunicationState.Opened);
   113         }
   114     }
   115 }