Client/MainForm.cs
changeset 31 f19b04646b6a
parent 30 c375286d1a1c
child 32 4c416d2878dd
     1.1 --- a/Client/MainForm.cs	Fri Aug 15 10:20:01 2014 +0200
     1.2 +++ b/Client/MainForm.cs	Fri Aug 15 11:11:17 2014 +0200
     1.3 @@ -9,6 +9,7 @@
     1.4  using System.Windows.Forms;
     1.5  using System.ServiceModel;
     1.6  using System.ServiceModel.Channels;
     1.7 +using System.Diagnostics;
     1.8  
     1.9  
    1.10  namespace SharpDisplayClient
    1.11 @@ -32,7 +33,7 @@
    1.12  
    1.13          private void MainForm_Load(object sender, EventArgs e)
    1.14          {
    1.15 -            iCallback = new Callback();
    1.16 +            iCallback = new Callback(this);
    1.17              //Instance context is then managed by our client class
    1.18              InstanceContext instanceContext = new InstanceContext(iCallback);
    1.19              iClient = new Client(instanceContext);
    1.20 @@ -45,18 +46,57 @@
    1.21  
    1.22          }
    1.23  
    1.24 -        public void CloseConnection()
    1.25 +
    1.26 +       
    1.27 +        public delegate void CloseConnectionDelegate();
    1.28 +        public delegate void CloseDelegate();
    1.29 +
    1.30 +        /// <summary>
    1.31 +        /// 
    1.32 +        /// </summary>
    1.33 +        public void CloseConnectionThreadSafe()
    1.34          {
    1.35 -            if (IsClientReady())
    1.36 +            if (this.InvokeRequired)
    1.37              {
    1.38 -                //iClient.Disconnect();
    1.39 -                iClient.Close();
    1.40 +                //Not in the proper thread, invoke ourselves
    1.41 +                CloseConnectionDelegate d = new CloseConnectionDelegate(CloseConnectionThreadSafe);
    1.42 +                this.Invoke(d, new object[] { });
    1.43              }
    1.44 +            else
    1.45 +            {
    1.46 +                //We are in the proper thread
    1.47 +                if (IsClientReady())
    1.48 +                {
    1.49 +                    //iClient.Disconnect();
    1.50 +                    Trace.TraceInformation("Closing client: " + iClient.SessionId);
    1.51 +                    iClient.Close();
    1.52 +                    Trace.TraceInformation("Closed client: " + iClient.SessionId);
    1.53 +                }
    1.54  
    1.55 -            iClient = null;
    1.56 -            iCallback = null;
    1.57 +                iClient = null;
    1.58 +                iCallback = null;
    1.59 +            }
    1.60          }
    1.61  
    1.62 +        /// <summary>
    1.63 +        /// 
    1.64 +        /// </summary>
    1.65 +        public void CloseThreadSafe()
    1.66 +        {
    1.67 +            if (this.InvokeRequired)
    1.68 +            {
    1.69 +                //Not in the proper thread, invoke ourselves
    1.70 +                CloseDelegate d = new CloseDelegate(CloseThreadSafe);
    1.71 +                this.Invoke(d, new object[] { });
    1.72 +            }
    1.73 +            else
    1.74 +            {
    1.75 +                //We are in the proper thread
    1.76 +                Close();
    1.77 +            }
    1.78 +        }
    1.79 +
    1.80 +
    1.81          private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
    1.82          {
    1.83              if (IsClientReady()) //Could catch exception instead
    1.84 @@ -64,7 +104,7 @@
    1.85                  iClient.Disconnect();
    1.86              }
    1.87  
    1.88 -            CloseConnection();
    1.89 +            CloseConnectionThreadSafe();
    1.90          }
    1.91  
    1.92          public bool IsClientReady()