Tag Archives: C#

Thread Timer

24 Aug

There are three timer controls in the .NET Framework:

  • A Windows-based timer, which is always in the Visual Studio Toolbox (System.Windows.Forms.Timer)
  • A server-based timer, which you can add to the Visual Studio Toolbox (System.Timers.Timer)
  • A thread timer, which is available programmatically (System.Threading.Timer)

If you are using Windows Forms and probably updating a UI, use System.Windows.Forms.Timer.

For server-based timer functionality, you might consider using System.Timers.Timer, which is also more accurate and has additional features.

The third one, System.Threading.Timer, uses a separate thread for it’s operation. If you need to do an asynchronous job periodically, it may be useful.

Using  System.Threading.Timer:

System.Threading.Timer timer;
timer = new System.Threading.Timer(new TimerCallback(DoSomething), null, 0, 1000);

DoSomething() executes at specified intervals(1000 ms here). Note that DoSomething() does not execute on the thread that created the timer; it executes on a ThreadPool thread supplied by the system.

private void DoSomething(object obj)
//it executes every second

To enable/disable timer, use Change() method:

timer.Change(Timeout.Infinite, Timeout.Infinite); //disable
timer.Change(0, 1000); //enable

Simple Exception Logger

21 Apr

The following class is probably the world simplest text logger.
There are several ways to open a file and write texts into it but I used File.AppendAllText() method because by using this method, it is guaranteed that the file handle will be closed, even if exceptions are raised.

public static class Logger
	public static void Log(string message)
		File.AppendAllText("C:\\MyLogFile.txt", DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToShortTimeString() + ":\t" + message + Environment.NewLine);

Using the Logger class:
By handling Application.ThreadException event in Program.cs , you can log every un-handled exception into a text file for further examination.

static void Main(string[] args)
	Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException); //it must be before Application.Run

static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)

Monitoring access to your shared files on network

2 Mar

ShareMonitor snapshot

I have written a program (named ShareMonitor) that allows you to monitor any access to your network shared files.  When a user remotely opens shared files on your computer, application detects it and displays information such as who opened it, when it was opened, duration, etc. about that file. All details will be logged and can be saved to .CSV or .XML files for further analysis.
You can also disconnect any active connection whenever you want.

For more details and source code, visit ShareMonitor’s Code Project page.

Download ShareMonitor (from Softpedia)


Since it was published, many articles have been written by experts to introduce and recommend ShareMonitor. Here you can find some of them:

Multiple Cell Selection in DataGridView

9 Sep

DataGridView has a facility to select multiple cells (or a range of cells) using mouse dragging, Cltrl+Click or Shift+Click. This feature is similar to cell selection in spreadsheet applications such as Microsoft Excel.

In order to enable this feature in DataGridView, use ‘SelectionMode’ property as follows:

DataGridViewCell.SelectionMode = DataGridViewSelectionMode.CellSelect;

To read/write data from/to selected cells simply write:

foreach (DataGridViewCell cell in dataGridView1.SelectedCells)
	//read cell data

	//change cell data
	dataGridView1.Rows[cell.RowIndex].Cells[cell.ColumnIndex].Value = 1;


16 May

There are several ways to sort data in C# but one of the easiest ways is using BindingSource sort capability.
Depending on what object (underlying list) is linked to the BindingSource as its DataSource and whether it has implemented IBindingList or IBindingListView interfaces or not, you can use this feature.

To find out whether a BindingSource with current DataSource supports sorting, use ‘SupportsSorting’ property:

if (bindingSource1.SupportsSorting)
	bindingSource1.Sort = "Code, Name";

Similarly, you can benefit from BindingSource.Filter .

Using SelectionChangeCommitted event of ComboBox

12 May

If you want to capture changes in ComboBox you can use ‘SelectedIndexChanged’ or ‘SelectedValueChanged’ events. However, if you want to get changes only when the user changes the selection, use ‘SelectionChangeCommitted’ event instead.

SelectionChangeCommitted occurs only when the ComboBox selection changes by user (via keyboard or mouse) and it is not raised when the selection changes programmatically.

private void comboBox1_SelectionChangeCommitted(object sender, EventArgs e)
    if (comboBox1.SelectedIndex == 1)
        //do something...

String Replicate

3 Feb

The simplest way to replicate (repeat) a string in C# is using string class constructor:

string s = new string('#', 4); //result: ####

If you want to replicate a sub-string (not only a char), use this:

string.Concat(System.Collections.ArrayList.Repeat("#0",4).ToArray()); //result: #0#0#0#0

DataBound ComboBox Problem

3 Feb

When you want to use a data bound ComboBox in .NET (C#, VB, etc), you should set a few properties such as DataSource, DisplayMember and ValueMember.
It works fine unless you want to set SelectedValue for the first time. It won’t be set!

I found that BindingContext property must be created before initializing SelectedValue:

ComboBox1.BindingContext = new BindingContext();
ComboBox1.DataSource = MyDataSet.DefaultView
ComboBox1.DisplayMember = "Title";
ComboBox1.ValueMember = "Code";
ComboBox1.SelectedValue = 1;

Merry Christmas!

26 Dec

Long time no update…

I couldn’t start reading the book until last week.
It’s an interesting book and helps you to switch to C# in the shortest time possible.
For me, with software designing and programming background, many concepts are similar but of course there are a few new features such as Generics, Attributes, etc. that weren’t in Delphi.
I have been reading two chapters a day and I’m currently reading the 12th chapter.

My first post!

7 Sep

Hello everybody!

After 7 years of professional software development in VB and Delphi, I have finally decided to switch to C# .Net. There are several reasons for this decision which I’m not going to explain them.

I’m trying to utilize my previous Delphi experience and learn new programming technics as much as I can.

This blog may be useful for beginners at first and gradually for professionals later when I got improved. Your feedback would be a good motivation for me to continue.

By the way, I’m going to start with “Pro C# 2008 and .NET 3.5 Platform” by Andrew Troelsen.

%d bloggers like this: