dotnet thoughts 

a dotnet developer's technical blog

Serialization in .Net – I

Serialization is the process of saving an object onto a storage medium (such as a file, or a memory buffer) or to transmit it across a network connection link, either in binary form, or in some human-readable text format such as XML. The series of bytes or the format can be used to re-create an object that is identical in its internal state to the original object (actually a clone).

A class can serialize / Deserialize using [Serializable] attribute to the class.

[Serializable]
class MyClass
{
//Class implementation..
}

To serialize an object, in .Net framework the BinaryFormatter class can be used.

System.IO.Stream sw = System.IO.File.OpenWrite("C:\\Sample.bin");
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter _Formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
Class1 objClass = new Class1();
objClass.Username = "Username1";
_Formatter.Serialize(sw,objClass);
sw.Flush();
sw.Close();

This will serialize the objClass in to the C:\Sample.bin file.

Anonymous Methods in C#

C# supports delegates for invoking one or multiple methods, in some scenarios, where you don’t have to use the delegate for multiple targets, C# 2.0 supports anonymous methods.

class MySampleClass
{
delegate void message();
public void ()
{
message msg = delegate()
{
MessageBox.Show("Hello World");
};
msg(); //This will invoke the Hello World Message.
}
}

Stopwatch Class

The stop watch class provides a set of methods and properties that you can use to measure elapsed time.

Stopwatch sw = new Stopwatch();
sw.Start();
//Do some long process.
sw.stop();
Console.WriteLine(sw.ElapsedMilliseconds.ToString())

To check stopwatch is running or not can use the IsRunning() property. It can also retun the Elapsed time in timestamp type.

For more details on stop watch class, use MSDN.
URL :Stopwatch class on MSDN

Improving performance, while adding items to listbox and combo – II

In my previous post, using Begin Upadate and End Update method, the performance improved very much. You can also boost up the Databinding performance, via setting the value member property before setting the DataSource property.

Here is the code with two scenarios.

Dim sw As New Stopwatch
sw.Start()
ListBox1.DisplayMember = "MyText"
ListBox1.DataSource = MyTable
ListBox1.ValueMember = "MyValue"
sw.Stop()
MessageBox.Show(sw.ElapsedMilliseconds)
In this scenario, I am setting the Datasource property first, and after I am setting the Value member property, it worked on machine and return 73ms.
And

Dim sw As New Stopwatch
sw.Start()
ListBox1.DisplayMember = "MyText"
ListBox1.ValueMember = "MyValue"
ListBox1.DataSource = MyTable
sw.Stop()
MessageBox.Show(sw.ElapsedMilliseconds)this returns nearly 65ms.
This will work with Datagrid also.

Improving performance, while adding items to listbox and combo

If you are adding large amount of items to list or combo using Add method, you can improve performance by calling BeginUpdate and EndUpdate methods.

Here is the code, in this two listboxs one using Begin – Edit update method calls and other one no using it. The first method fills the list box with lesser time than the second listbox.

Dim sw As New Stopwatch
sw.Start()
ListBox1.BeginUpdate()
For i As Integer = 0 To 1000
ListBox1.Items.Add("Item " & i)
Next
ListBox1.EndUpdate()
sw.Stop()
MessageBox.Show(sw.ElapsedMilliseconds)

Code for first listbox, it shows nearly 74 ms in my machine.

Dim sw As New Stopwatch
sw.Start()
For i As Integer = 0 To 1000
ListBox1.Items.Add("Item " & i)
Next
sw.Stop()MessageBox.Show(sw.ElapsedMilliseconds)

And this one shows nearly 195ms.If your using larger amount of data, using BeginUpdate and EndUpdate methods you can boostup the performance.

Older Posts »