Category: "Example Codes"
How to put your PC in sleep or hibernate via C#
I thought that is impossible to put your PC in sleep or hibernate via C#. I found that is incorrect! Moreover, that is very simple too, just one line of command.
Putting the PC in sleep mode
Code
Application.SetSuspendState(PowerState.Suspend, false, false); |
Putting the PC in Hibernate mode
Code
Application.SetSuspendState(PowerState.Hibernate, false, false); |
That is so simple!!!
Windows Service Tutorial 1
This tutorial is to build a simple Windows Services.
1. Create a Windows Service Project, File->New->Project
2. Please add these codes into Services1.cs
Code
protected override void OnStart(string[] args) | |
{ | |
EventLog.WriteEntry("Example Start"); | |
System.Timers.Timer timer1 = new System.Timers.Timer(); | |
timer1.Enabled = true; | |
timer1.Interval = 60000; | |
timer1.Elapsed += new System.Timers.ElapsedEventHandler(timer1_Elapsed); | |
timer1.Start(); | |
} | |
| |
void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e) | |
{ | |
EventLog.WriteEntry("Timer Triggered:"+DateTime.Now.ToString()); | |
} | |
| |
protected override void OnStop() | |
{ | |
EventLog.WriteEntry("Example Stop"); | |
} |
Mutex Lock Example
I have written an example project in C#. This example will shows how to use Mutex Lock. Moreover, it will demonstrate how to create a thread with parameters. Please click here to download.
There is no ’ListItem’ in Winforms Combobox
I have been using ASP.Net for years. There is a very popup control, dropdown list. I used this control thousands of times. I know it very well. Recently, I built a winforms application. I need to use a dropdown list. ComboBox Control is the most similar with dropdown list. I found there is a different. I like to add a default option, new ListItem("Please select a type",""), into the items of the control. If the program will check whether the user selects this option, if that is the case, it will treat the input to be null. The items in ComoboBox is ObjectCollection. They are just a list of objects. You cannot use ListItem.
Well, you can create your version of ListItem objects and convert your domain objects into ListItem. Finally, you can bind the list of ListItem objects into the comobobox. In this way, Combobox will works like a drop down list.
I chose to use a simpler approach. I just created a domain object with ID 0 and insert it into the list.
Code
ResourceType empty = new ResourceType(); | |
empty.Name="---------------------------------------"; | |
resourceTypes.Insert(0,empty); | |
ddlSearchResourceTypes.DataSource=resourceTypes; |
Getting Row Count in NHibernate
Getting the row count of a table, that is easy in NHibernate. You can do it this way:
Code
ICriteria criteria = CurrentSession.CreateCriteria<Member>(); | |
criteria.Add(Expression.Eq("Code",code)); | |
return (int) criteria.List().Count; |
But this is very heavy weighted. You are retrieving a whole list of objects!!!! But you just need to know the numbers of objects! You are wasting a lot of processing power!
You can do it this way. That is only getting a row count, just a number! Therefore, a "select count(*)" query is executed.
Code
ICriteria criteria = CurrentSession.CreateCriteria<Member>(); | |
criteria.Add(Expression.Eq("Code",code)); | |
criteria.SetProjection(Projections.RowCount()); | |
return (int) criteria.UniqueResult(); |