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