Client/MainForm.cs
author StephaneLenclud
Mon, 18 Aug 2014 23:43:44 +0200
changeset 35 f3893924a6eb
parent 32 4c416d2878dd
child 43 86aad774b532
permissions -rw-r--r--
Tried using Control instead of Label as base class for our Marquee.
Sticking to Label for now.
     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[] { textBoxTop.Text, textBoxBottom.Text });
    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             string name = "Client-" + (iClient.ClientCount() - 1);
    44             iClient.SetName(name);
    45             //Text = Text + ": " + name;
    46             Text = "[[" + name + "]]  " + iClient.SessionId;
    47 
    48             //
    49             textBoxTop.Text = iClient.Name;
    50             textBoxBottom.Text = iClient.SessionId;
    51 
    52         }
    53 
    54 
    55        
    56         public delegate void CloseConnectionDelegate();
    57         public delegate void CloseDelegate();
    58 
    59         /// <summary>
    60         /// 
    61         /// </summary>
    62         public void CloseConnectionThreadSafe()
    63         {
    64             if (this.InvokeRequired)
    65             {
    66                 //Not in the proper thread, invoke ourselves
    67                 CloseConnectionDelegate d = new CloseConnectionDelegate(CloseConnectionThreadSafe);
    68                 this.Invoke(d, new object[] { });
    69             }
    70             else
    71             {
    72                 //We are in the proper thread
    73                 if (IsClientReady())
    74                 {
    75                     Trace.TraceInformation("Closing client: " + iClient.SessionId);
    76                     iClient.Close();
    77                     Trace.TraceInformation("Closed client: " + iClient.SessionId);
    78                 }
    79 
    80                 iClient = null;
    81                 iCallback = null;
    82             }
    83         }
    84 
    85         /// <summary>
    86         /// 
    87         /// </summary>
    88         public void CloseThreadSafe()
    89         {
    90             if (this.InvokeRequired)
    91             {
    92                 //Not in the proper thread, invoke ourselves
    93                 CloseDelegate d = new CloseDelegate(CloseThreadSafe);
    94                 this.Invoke(d, new object[] { });
    95             }
    96             else
    97             {
    98                 //We are in the proper thread
    99                 Close();
   100             }
   101         }
   102 
   103 
   104         private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
   105         {
   106             CloseConnectionThreadSafe();
   107         }
   108 
   109         public bool IsClientReady()
   110         {
   111             return (iClient != null && iClient.State == CommunicationState.Opened);
   112         }
   113     }
   114 }