Posts

Showing posts from May, 2009

C# - UI Cross Thread

If you use a UI thread to perform lengthy process, you will experience UI "hang" and you will not be able to do anything else (even closing the UI)

To fix that, you will have to start another thread to perform these lengthy process.



//doLengthyProcess is a method that perform your process
Thread newThread = new Thread(new ThreadStart(doLengthyProcess));

// Start the thread
newThread.Start();


But now, if you update UI component at this newThread, you will encounter Croess Thread Exception. The way to fix cross thread issue is delegate. Some steps are

1. You have to create a UI method that update your UI components.
2. Create a Delegate callback to perform UI thread callback
3. Check for InvokeRequire of your UI component
4. If invoke required, use delegate callback to wake up UI thread to perform UI action


//delegate to clear textfield
public delegate void ClearText();

//method to clear text. It check for Cross Thread too
private void clearText() {
if (myText.Invo…

C# - read from file

I am amazed by the simplicity to read all file bytes in C#... WoooHoo


byte[] dataBytes = File.ReadAllBytes("My_FILE_NAME");

C# - XML Serialization

Image
C# XML Serialization can be done by

1. Create a serializable class
2. Use a XmlSerializer to serialize and deserialize
3. Use TextWriter and FileStream to write and read from file respectively.

Below are very sample code

A Serializable class - It is a C# class with [Serializable] tag



Write serializable object to xml file



Read xml file and deserialize into object



The resulting xml is


Autorun Program From Removable Media

Autorun, as the name imply, allow program to autorun from removable storage (USB drive, CD-ROM...). This will give you simple guide on how to create a autorun removable storage

To create a autorun removable media, do the following step

1. Create a file call autorun.inf in the root folder of the removable media.

2. Open autorun.inf with any text editor program

3. At the top of the file, put [autorun]. [autorun] denote that this is a autorun file

4. In general, the autorun file setting is in name=value pair. (ie, icon=myicon.ico). The following give you some example of the name value pair

icon=xxx.ico -> allow you to set the icon of your autorun removable media

label=xxxx -> allow you to set the name of this removable media

open=xxxx.xxx -> allow you to define the program with you want to be open when media is inserted

action=xxxx -> allow you to set the action name in the action prompt when you insert your media

shell\xxxx\command -> allow you to set XXXX to the context menu when …

HTTP Persistent Connection

HTTP/1.1 offers HTTP persistent connection. This persistent connection provides a huge improvement in HTTP performance. HTTP persistent connection allows the same TCP connection to send/receive data over the same TCP socket without opening a new socket. If you have notice, in HTTP/1.0, it does not provide HTTP persistent connection. As a result, each HTTP call opens a new connection. So, what the issue?The issue is TCP termination. Whenever a TCP connection terminate, the TCP socket will goes into a Time-Wait state. TCP Time-Wait state make the device to wait for a period of time equal to double the maximum segment life (MSL) time. This gives enough time to ensure the ACK it sent to the client was received. And, the damaging effect is Port Availability, that is, you cannot use that port for 2*MSL time.In Window and Linux environment, the default TCP Time-Wait is 2 minutes. This means: After a TCP connection is closed, the port is not available for the next 4 minutes. You may ask, what…

Use named Mutex to restrict multiple program startup

Something new I had learn from C# today, and that is, the use of Mutex.

I guess most people know what is mutex. For clarity, below is the Mutex definition from Microsoft

When two or more threads need to access a shared resource at the same time, the system needs a synchronization mechanism to ensure that only one thread at a time uses the resource. Mutex is a synchronization primitive that grants exclusive access to the shared resource to only one thread. If a thread acquires a mutex, the second thread that wants to acquire that mutex is suspended until the first thread releases the mutex.

But, there is 2 types of Mutex, local and system-wide. Local Mutex is a unnamed mutex that only avaiable for the created process. System-wide Mutex is a named mutex that available for the whole operation system.

Definition from Microsoft

Mutexes are of two types: local mutexes, which are unnamed, and named system mutexes. A local mutex exists only within your process. It can be used by any thread in your…