Client/MainForm.cs
author sl
Thu, 14 Aug 2014 18:37:23 +0200
changeset 29 c4e03315035c
parent 26 a6fb2b2f73b0
child 30 c375286d1a1c
permissions -rw-r--r--
Client/Server duplex is still a mess in C#.
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@20
    12
sl@18
    13
sl@18
    14
namespace SharpDisplayClient
sl@18
    15
{
sl@18
    16
    public partial class MainForm : Form
sl@18
    17
    {
sl@26
    18
        Client iClient;
sl@26
    19
        Callback iCallback;
sl@18
    20
sl@18
    21
        public MainForm()
sl@18
    22
        {
sl@18
    23
            InitializeComponent();
sl@18
    24
        }
sl@18
    25
sl@18
    26
        private void buttonSetText_Click(object sender, EventArgs e)
sl@18
    27
        {
sl@19
    28
            //iClient.SetText(0,"Top");
sl@19
    29
            //iClient.SetText(1, "Bottom");
sl@26
    30
            iClient.SetTexts(new string[] { "Top", "Bottom" });
sl@18
    31
        }
sl@18
    32
sl@18
    33
        private void MainForm_Load(object sender, EventArgs e)
sl@18
    34
        {
sl@26
    35
            iCallback = new Callback();
sl@25
    36
            //Instance context is then managed by our client class
sl@26
    37
            InstanceContext instanceContext = new InstanceContext(iCallback);
sl@26
    38
            iClient = new Client(instanceContext);
sl@18
    39
sl@29
    40
            //Connect using unique name
sl@29
    41
            string name = DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss.fff tt");
sl@29
    42
            iClient.Connect(name);
sl@29
    43
            Text = Text + ": " + name;
sl@18
    44
sl@18
    45
        }
sl@21
    46
sl@21
    47
        public void CloseConnection()
sl@21
    48
        {
sl@29
    49
            if (IsClientReady())
sl@29
    50
            {
sl@29
    51
                //iClient.Disconnect();
sl@29
    52
                iClient.Close();
sl@29
    53
            }
sl@29
    54
sl@26
    55
            iClient = null;
sl@26
    56
            iCallback = null;
sl@26
    57
        }
sl@26
    58
sl@26
    59
        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
sl@26
    60
        {
sl@29
    61
            if (IsClientReady()) //Could catch exception instead
sl@26
    62
            {
sl@26
    63
                iClient.Disconnect();
sl@26
    64
            }
sl@29
    65
sl@29
    66
            CloseConnection();
sl@29
    67
        }
sl@29
    68
sl@29
    69
        public bool IsClientReady()
sl@29
    70
        {
sl@29
    71
            return (iClient != null && iClient.State == CommunicationState.Opened);
sl@21
    72
        }
sl@18
    73
    }
sl@18
    74
}