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.
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@30
    31
            iClient.SetTexts(new string[] { iClient.Name, iClient.SessionId });
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@29
    42
            string name = DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss.fff tt");
sl@29
    43
            iClient.Connect(name);
sl@30
    44
            //Text = Text + ": " + name;
sl@30
    45
            Text = Text + ": " + iClient.SessionId;
sl@18
    46
sl@18
    47
        }
sl@21
    48
sl@31
    49
sl@31
    50
       
sl@31
    51
        public delegate void CloseConnectionDelegate();
sl@31
    52
        public delegate void CloseDelegate();
sl@31
    53
sl@31
    54
        /// <summary>
sl@31
    55
        /// 
sl@31
    56
        /// </summary>
sl@31
    57
        public void CloseConnectionThreadSafe()
sl@21
    58
        {
sl@31
    59
            if (this.InvokeRequired)
sl@29
    60
            {
sl@31
    61
                //Not in the proper thread, invoke ourselves
sl@31
    62
                CloseConnectionDelegate d = new CloseConnectionDelegate(CloseConnectionThreadSafe);
sl@31
    63
                this.Invoke(d, new object[] { });
sl@29
    64
            }
sl@31
    65
            else
sl@31
    66
            {
sl@31
    67
                //We are in the proper thread
sl@31
    68
                if (IsClientReady())
sl@31
    69
                {
sl@31
    70
                    //iClient.Disconnect();
sl@31
    71
                    Trace.TraceInformation("Closing client: " + iClient.SessionId);
sl@31
    72
                    iClient.Close();
sl@31
    73
                    Trace.TraceInformation("Closed client: " + iClient.SessionId);
sl@31
    74
                }
sl@29
    75
sl@31
    76
                iClient = null;
sl@31
    77
                iCallback = null;
sl@31
    78
            }
sl@26
    79
        }
sl@26
    80
sl@31
    81
        /// <summary>
sl@31
    82
        /// 
sl@31
    83
        /// </summary>
sl@31
    84
        public void CloseThreadSafe()
sl@31
    85
        {
sl@31
    86
            if (this.InvokeRequired)
sl@31
    87
            {
sl@31
    88
                //Not in the proper thread, invoke ourselves
sl@31
    89
                CloseDelegate d = new CloseDelegate(CloseThreadSafe);
sl@31
    90
                this.Invoke(d, new object[] { });
sl@31
    91
            }
sl@31
    92
            else
sl@31
    93
            {
sl@31
    94
                //We are in the proper thread
sl@31
    95
                Close();
sl@31
    96
            }
sl@31
    97
        }
sl@31
    98
sl@31
    99
sl@26
   100
        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
sl@26
   101
        {
sl@29
   102
            if (IsClientReady()) //Could catch exception instead
sl@26
   103
            {
sl@26
   104
                iClient.Disconnect();
sl@26
   105
            }
sl@29
   106
sl@31
   107
            CloseConnectionThreadSafe();
sl@29
   108
        }
sl@29
   109
sl@29
   110
        public bool IsClientReady()
sl@29
   111
        {
sl@29
   112
            return (iClient != null && iClient.State == CommunicationState.Opened);
sl@21
   113
        }
sl@18
   114
    }
sl@18
   115
}