.NET Framework and Windows Forms (WinForms) Hints, Tips & Tricks, Performance Tuning Cory Smith, MVP – Visual Basic Senior Developer, Scott Studios, LLC / dMarc Broadcasting http://www.addressof.com December 15, 2004 Ft. Worth .NET Users Group Agenda Quick tips for all types of development Assembly Debug or Release? Managing Memory Consumption Load image from a stream Load image from a URI Generate a unique ID Retrieve a list of printers Retrieve more information from P/Invoke error messages Windows Forms specific tips Managing event handlers Internationalization and Localization XP Visual Styles Converting values Using BitConverter Using ASCIIEncoding.ASCII Using Structures What about ASCII and Char (Unicode)? Exploring performance tuning String Comparison and Manipulation Converting between value types Digging deeper… Using IL An even easier way to dig deeper Encryption DPAPI – Pro’s & Con’s An easy alternative Open Discussion Determine if the executing assembly was built as debug or release programmatically? Sub Main() If Reflection.Assembly.GetExecutingAssembly.GetCustomAttributes( _ GetType(DebuggableAttribute), False ).Length > 0 Then Console.WriteLine ("Assembly compiled with as 'Debug' mode.") Else Console.WriteLine ("Assembly compiled with as 'Release' mode.") End If Console.ReadLine() End Sub My customer is complaining my application uses too much memory? What can I do? Is it really using that much memory? Demo of the problem What can you do? How can I force Windows to reset the reported memory usage for my application? Code a possible solution Warnings / Disclaimer How do I load an image from a stream? Dim image As Image = New Bitmap(stream) How do I load an image from a URI? Private Function ImageFromUri(ByVal uri As String) As Image Dim request As HttpWebRequest = _ DirectCast(WebRequest.Create(uri), HttpWebRequest) Dim response As HttpWebResponse = _ DirectCast(request.GetResponse, HttpWebResponse) Dim ws As Stream = response.GetResponseStream Dim br As BinaryReader = New BinaryReader(ws) Dim bytes() As Byte = br.ReadBytes(CInt(response.ContentLength)) Return New Bitmap(New IO.MemoryStream(bytes)) End Function What’s the easiest way to generate a unique identifier? Dim guid As String = System.Guid.NewGuid.ToString How do I retrieve a list of installed printers? Imports System.Drawing.Printing.PrinterSettings For Each printerName As String In InstalledPrinters Console.WriteLine(printerName) Next How can I get more information from a Win32 style error when using P/Invoke? Imports System.ComponentModel Imports System.Runtime.InteropServices Throw New Win32Exception(Marshal.GetLastWin32Error) How do I stop events from happening when I update data on my form programmatically? You can dynamically add and remove event handlers at runtime. Demo I need to support multiple languages, how do I do this? Visual Studio .NET 2003 supports multiple languages (Internationalization and Localization) in the box. Walkthrough (from scratch) Gotcha’s… Visual Studio 2005 improvements How do I enable Visual Styles support when running under Windows XP? Modify your application so it uses Sub Main() to start. Public Shared Sub Main() Application.EnableVisualStyles() Application.DoEvents() ' To work around a bug. Application.Run(New Form1) End Sub Modify all controls that support the FlatStyle property, setting the value to System. Set all TabPage instances BackColor property to White. Code Demo – Possible problems. How do I truly determine of Visual Styles are enabled when running Windows XP? Why is this important? Demo References - Websites AddressOf – Articles, Code Samples http://www.addressof.com GotDotNet – Code Samples, Discussions http://www.gotdotnet.com Code Project – Articles, Code Samples, Discussions http://www.codeproject.com Fort Worth .NET Users Group http://www.fwdnug.com Open Discussion Questions?