OUT-21: Added disposal of preview controls. Using, try/catch blocks. Redesign_Handshake_Dialog
authornikolaj
Wed, 14 Jun 2017 12:37:19 +0200
branchRedesign_Handshake_Dialog
changeset 1702823ee1efe16c
parent 1697 c6202440865e
child 1703 d0199974ad59
OUT-21: Added disposal of preview controls. Using, try/catch blocks.
UI/FormControlPreviewMessage.xaml.cs
     1.1 --- a/UI/FormControlPreviewMessage.xaml.cs	Mon Jun 12 18:53:33 2017 +0200
     1.2 +++ b/UI/FormControlPreviewMessage.xaml.cs	Wed Jun 14 12:37:19 2017 +0200
     1.3 @@ -87,8 +87,23 @@
     1.4              this.displayState = new State();
     1.5              this.displayState.PropertyChanged += DisplayState_PropertyChanged;
     1.6              this.DataContext = this.displayState;
     1.7 +            this.IsVisibleChanged += DisposeOfAllComponents;
     1.8          }
     1.9 -
    1.10 +        private void DisposeOfAllComponents(object sender, DependencyPropertyChangedEventArgs e)
    1.11 +        {
    1.12 +            if (!this.IsVisible) 
    1.13 +            {
    1.14 +                this.WindowsFormsHostMessageBody.Dispose();
    1.15 +                this.RichTextBoxMessageBody.Dispose();
    1.16 +                this.ButtonForward = null;
    1.17 +                this.ButtonReply = null;
    1.18 +                this.ButtonReplyAll = null;
    1.19 +                this.displayState = null;
    1.20 +                this.displayState = null;
    1.21 +                this.GridLayoutRoot = null;                                    
    1.22 +            }
    1.23 +        }
    1.24 +        
    1.25          /**************************************************************
    1.26           * 
    1.27           * Property Accessors
    1.28 @@ -120,7 +135,7 @@
    1.29           * Methods
    1.30           * 
    1.31           *************************************************************/
    1.32 -
    1.33 +     
    1.34          /// <summary>
    1.35          /// Sets the rich text box text in the UI from the display state message.
    1.36          /// </summary>
    1.37 @@ -153,6 +168,7 @@
    1.38                          5. If operation did not succeed only text without images is shown
    1.39          
    1.40                  */
    1.41 +
    1.42                  string html = this.displayState?.Message?.LongMsgFormattedHtml; // html content
    1.43                  if (string.IsNullOrEmpty(html))
    1.44                  {
    1.45 @@ -203,7 +219,7 @@
    1.46                      {
    1.47                          images.Add(img);
    1.48                      }
    1.49 -                }    
    1.50 +                }
    1.51                  if (images.Count < 1)
    1.52                  {
    1.53                      this.RichTextBoxMessageBody.Rtf = this.displayState?.Message?.LongMsgFormattedRtf;
    1.54 @@ -225,7 +241,7 @@
    1.55                          {
    1.56                              memoryStream.Position = 0;
    1.57                              byteArray = memoryStream.ToArray();
    1.58 -                        }                   
    1.59 +                        }
    1.60                          SoapHexBinary shb = new SoapHexBinary(byteArray);
    1.61                          string hexString = shb.ToString();
    1.62  
    1.63 @@ -240,126 +256,129 @@
    1.64                  }
    1.65  
    1.66                  // Copy HTML to WebBrowser class 
    1.67 -                System.Windows.Forms.WebBrowser wb = new System.Windows.Forms.WebBrowser();
    1.68 -                try
    1.69 +                using (System.Windows.Forms.WebBrowser wb = new System.Windows.Forms.WebBrowser())
    1.70                  {
    1.71 -                    wb.Navigate("about:blank");
    1.72 -                    wb.Document.Write(html);
    1.73 -                }
    1.74 -                catch
    1.75 -                {
    1.76 -                    this.RichTextBoxMessageBody.Rtf = this.displayState?.Message?.LongMsgFormattedRtf;
    1.77 -                    return;
    1.78 -                }
    1.79 -
    1.80 -                // back up data from clipboard
    1.81 -                object rtfFromClipboard = null;
    1.82 -                string textFromClipboard = null;
    1.83 -                BitmapSource imageFromClipboard = null;
    1.84 -                try
    1.85 -                {
    1.86 -                    IDataObject dataFromClipboard = Clipboard.GetDataObject();
    1.87 -                                       
    1.88 -                    if (dataFromClipboard.GetDataPresent(DataFormats.Rtf))
    1.89 +                    try
    1.90                      {
    1.91 -                        rtfFromClipboard = dataFromClipboard.GetData(DataFormats.Rtf);
    1.92 +                        wb.Navigate("about:blank");
    1.93 +                        wb.Document.Write(html);
    1.94 +                    }
    1.95 +                    catch
    1.96 +                    {
    1.97 +                        this.RichTextBoxMessageBody.Rtf = this.displayState?.Message?.LongMsgFormattedRtf;
    1.98 +                        return;
    1.99                      }
   1.100  
   1.101 -                    if (Clipboard.ContainsText())
   1.102 +                    // back up data from clipboard
   1.103 +                    object rtfFromClipboard = null;
   1.104 +                    string textFromClipboard = null;
   1.105 +                    BitmapSource imageFromClipboard = null;
   1.106 +                    try
   1.107                      {
   1.108 -                        textFromClipboard = Clipboard.GetText();
   1.109 +                        IDataObject dataFromClipboard = Clipboard.GetDataObject();
   1.110 +
   1.111 +                        if (dataFromClipboard.GetDataPresent(DataFormats.Rtf))
   1.112 +                        {
   1.113 +                            rtfFromClipboard = dataFromClipboard.GetData(DataFormats.Rtf);
   1.114 +                        }
   1.115 +
   1.116 +                        if (Clipboard.ContainsText())
   1.117 +                        {
   1.118 +                            textFromClipboard = Clipboard.GetText();
   1.119 +                        }
   1.120 +
   1.121 +                        if (Clipboard.ContainsImage())
   1.122 +                        {
   1.123 +                            imageFromClipboard = Clipboard.GetImage();
   1.124 +                        }
   1.125 +                    }
   1.126 +                    catch
   1.127 +                    {
   1.128 +                        this.RichTextBoxMessageBody.Rtf = this.displayState?.Message?.LongMsgFormattedRtf;
   1.129 +                        return;
   1.130 +                    }
   1.131 +                    // Copy from WebBrowser class to Clipboard
   1.132 +                    wb.Document.ExecCommand("SelectAll", false, null);
   1.133 +                    wb.Document.ExecCommand("Copy", false, null);
   1.134 +
   1.135 +                    string rtf= string.Empty;
   1.136 +
   1.137 +                    try
   1.138 +                    {
   1.139 +                        // Copy from Clipboard to "rtf" variable 
   1.140 +                        rtf = System.Windows.Forms.Clipboard.GetData(DataFormats.Rtf) as string;
   1.141 +
   1.142 +                        // Restore Clipboard backup
   1.143 +                        if (rtfFromClipboard != null)
   1.144 +                        {
   1.145 +                            Clipboard.SetData(DataFormats.Rtf, rtfFromClipboard);
   1.146 +                        }
   1.147 +                        else
   1.148 +                        {
   1.149 +                            if (imageFromClipboard != null)
   1.150 +                            {
   1.151 +                                Clipboard.SetImage(imageFromClipboard);
   1.152 +                            }
   1.153 +                            if (textFromClipboard != null)
   1.154 +                            {
   1.155 +                                Clipboard.SetText(textFromClipboard);
   1.156 +                            }
   1.157 +                        }
   1.158 +                    }
   1.159 +                    catch (Exception ex)
   1.160 +                    {
   1.161 +                        this.RichTextBoxMessageBody.Rtf = this.displayState?.Message?.LongMsgFormattedRtf;
   1.162 +
   1.163 +                        // If Exception was caused by another application using clipboard than GetOpenClipboardWindowText() will return information of this application. Uses user32.dll 
   1.164 +                        var msg = this.GetOpenClipboardWindowText();
   1.165 +                        Log.Error("Clipboard error: " + msg);
   1.166 +                        return;
   1.167                      }
   1.168  
   1.169 -                    if (Clipboard.ContainsImage())
   1.170 +                    try
   1.171                      {
   1.172 -                        imageFromClipboard = Clipboard.GetImage();
   1.173 +                        // Replace WMF picture headers with RtfPicturePlaceHolder  
   1.174 +                        rtf = DeleteWmfHeaders(rtf);
   1.175                      }
   1.176 -                }
   1.177 -                catch
   1.178 -                {
   1.179 -                    this.RichTextBoxMessageBody.Rtf = this.displayState?.Message?.LongMsgFormattedRtf;
   1.180 -                    return;
   1.181 -                }
   1.182 -                // Copy from WebBrowser class to Clipboard
   1.183 -                wb.Document.ExecCommand("SelectAll", false, null);
   1.184 -                wb.Document.ExecCommand("Copy", false, null);
   1.185 +                    catch (System.OutOfMemoryException ex)
   1.186 +                    {
   1.187 +                        this.RichTextBoxMessageBody.Rtf = this.displayState?.Message?.LongMsgFormattedRtf;
   1.188 +                        return;
   1.189 +                    }
   1.190 +                    catch (Exception ex)
   1.191 +                    {
   1.192 +                        this.RichTextBoxMessageBody.Rtf = this.displayState?.Message?.LongMsgFormattedRtf;
   1.193 +                        return;
   1.194 +                    }
   1.195  
   1.196 -                string rtf= string.Empty;
   1.197 -                
   1.198 -                try
   1.199 -                {
   1.200 -                    // Copy from Clipboard to "rtf" variable 
   1.201 -                    rtf = System.Windows.Forms.Clipboard.GetData(DataFormats.Rtf) as string;
   1.202 -
   1.203 -                    // Restore Clipboard backup
   1.204 -                    if (rtfFromClipboard != null)
   1.205 +                    try
   1.206                      {
   1.207 -                        Clipboard.SetData(DataFormats.Rtf, rtfFromClipboard);
   1.208 -                    }
   1.209 -                    else
   1.210 -                    {
   1.211 -                        if (imageFromClipboard != null)
   1.212 +                        //  Replace RtfPicturePlaceHolder with images hex strings
   1.213 +                        for (int i = 0; i < hexStrings.Count; i++)
   1.214                          {
   1.215 -                            Clipboard.SetImage(imageFromClipboard);
   1.216 -                        }
   1.217 -                        if (textFromClipboard != null)
   1.218 -                        {
   1.219 -                            Clipboard.SetText(textFromClipboard);
   1.220 +                            //rtf = ReplaceFirst( ref rtf, RtfPicturePlaceHolder, hexStrings[i]);
   1.221 +                            ReplaceFirst(ref rtf, RtfPicturePlaceHolder, hexStrings[i]);
   1.222 +                            hexStrings[i] = null;                           
   1.223                          }
   1.224                      }
   1.225 +                    catch (System.OutOfMemoryException ex)
   1.226 +                    {
   1.227 +                        this.RichTextBoxMessageBody.Rtf = this.displayState?.Message?.LongMsgFormattedRtf;
   1.228 +                        return;
   1.229 +                    }
   1.230 +                    catch (Exception ex)
   1.231 +                    {
   1.232 +                        this.RichTextBoxMessageBody.Rtf = this.displayState?.Message?.LongMsgFormattedRtf;
   1.233 +                        return;
   1.234 +                    }
   1.235 +                    // Assign RichTextBoxMessageBody.Rtf with rtf
   1.236 +                    if (string.IsNullOrEmpty(rtf))
   1.237 +                    {
   1.238 +                        this.RichTextBoxMessageBody.Rtf = this.displayState?.Message?.LongMsgFormattedRtf;
   1.239 +                        return;
   1.240 +                    }
   1.241 +                    this.RichTextBoxMessageBody.Rtf = rtf;
   1.242                  }
   1.243 -                catch (Exception ex)
   1.244 -                {
   1.245 -                    this.RichTextBoxMessageBody.Rtf = this.displayState?.Message?.LongMsgFormattedRtf;
   1.246 -
   1.247 -                    // If Exception was caused by another application using clipboard than GetOpenClipboardWindowText() will return information of this application. Uses user32.dll 
   1.248 -                    var msg = this.GetOpenClipboardWindowText();
   1.249 -                    Log.Error("Clipboard error: "+ msg);
   1.250 -                    return;
   1.251 -                }
   1.252 -
   1.253 -                try
   1.254 -                {
   1.255 -                    // Replace WMF picture headers with RtfPicturePlaceHolder  
   1.256 -                    rtf = DeleteWmfHeaders(rtf);
   1.257 -                }
   1.258 -                catch (System.OutOfMemoryException ex)
   1.259 -                {
   1.260 -                    this.RichTextBoxMessageBody.Rtf = this.displayState?.Message?.LongMsgFormattedRtf;
   1.261 -                    return;
   1.262 -                }
   1.263 -                catch (Exception ex)
   1.264 -                {
   1.265 -                    this.RichTextBoxMessageBody.Rtf = this.displayState?.Message?.LongMsgFormattedRtf;
   1.266 -                    return;
   1.267 -                }
   1.268 -
   1.269 -                try
   1.270 -                {
   1.271 -                    //  Replace RtfPicturePlaceHolder with images hex strings
   1.272 -                    foreach (var hexString in hexStrings)
   1.273 -                    {
   1.274 -                        rtf = ReplaceFirst(rtf, RtfPicturePlaceHolder, hexString);
   1.275 -                    }
   1.276 -                }
   1.277 -                catch (System.OutOfMemoryException ex)
   1.278 -                {
   1.279 -                    this.RichTextBoxMessageBody.Rtf = this.displayState?.Message?.LongMsgFormattedRtf;
   1.280 -                    return;
   1.281 -                }
   1.282 -                catch (Exception ex)
   1.283 -                {
   1.284 -                    this.RichTextBoxMessageBody.Rtf = this.displayState?.Message?.LongMsgFormattedRtf;
   1.285 -                    return;
   1.286 -                }
   1.287 -                // Assign RichTextBoxMessageBody.Rtf with rtf
   1.288 -                if (string.IsNullOrEmpty(rtf))
   1.289 -                {
   1.290 -                    this.RichTextBoxMessageBody.Rtf = this.displayState?.Message?.LongMsgFormattedRtf;
   1.291 -                    return;
   1.292 -                }
   1.293 -                this.RichTextBoxMessageBody.Rtf = rtf;
   1.294 -
   1.295              }
   1.296  
   1.297              return;
   1.298 @@ -408,17 +427,26 @@
   1.299              return sb.ToString();
   1.300          }
   1.301  
   1.302 -        private string ReplaceFirst(string text, string search, string replace)
   1.303 +        private void ReplaceFirst(ref string text, string search, string replace)
   1.304          {
   1.305 -            int pos = text.IndexOf(search);
   1.306 -            if (pos < 0)
   1.307 +            try
   1.308              {
   1.309 -                return text;
   1.310 +                int pos = text.IndexOf(search);
   1.311 +                if (pos < 0)
   1.312 +                {
   1.313 +                    return;
   1.314 +                }
   1.315 +                StringBuilder sb = new StringBuilder(text.Substring(0, pos));
   1.316 +                sb.Append(replace);
   1.317 +                sb.Append(text.Substring(pos + search.Length));
   1.318 +                text = null;                
   1.319 +                text = sb.ToString(); // text.Substring(0, pos) + replace + text.Substring(pos + search.Length);
   1.320 +                sb = null;                
   1.321              }
   1.322 -            StringBuilder sb = new StringBuilder(text.Substring(0, pos));
   1.323 -            sb.Append(replace);
   1.324 -            sb.Append(text.Substring(pos + search.Length));
   1.325 -            return sb.ToString(); // text.Substring(0, pos) + replace + text.Substring(pos + search.Length);
   1.326 +            catch(System.OutOfMemoryException ex)
   1.327 +            {
   1.328 +                
   1.329 +            }
   1.330          }
   1.331  
   1.332          private MemoryStream MakeMetafileStream(Bitmap image)
   1.333 @@ -930,5 +958,11 @@
   1.334                  return;
   1.335              }            
   1.336          }
   1.337 +
   1.338 +        private void RichTextBoxMessageBody_Disposed(object sender, EventArgs e)
   1.339 +        {
   1.340 +           
   1.341 +        }
   1.342 +
   1.343      }
   1.344  }