Tuesday, December 22, 2009

System Idle. Inactivity on C# Sharp.

Today is the day of the user idle time and how can we know when the system is inactive.

Well, first of all we need the user32.dll library and the function GetLastInputInfo. The GetLastInputInfo function retrieves the time of the last input event.
BOOL GetLastInputInfo(
PLASTINPUTINFO plii
);

Where plii is a pointer to
typedef struct tagLASTINPUTINFO {
UINT cbSize;
DWORD dwTime;
} LASTINPUTINFO, *PLASTINPUTINFO;


Now, we can use this information to program in C#.
[DllImport("user32.dll", SetLastError = true)]
static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);

internal struct LASTINPUTINFO
{
public uint cbSize;
public uint dwTime;
}

We create an Timer in our Form and set the tick function:
private void Timer_Tick(object sender, EventArgs e)
{
// Get the system uptime
int SystemUptime = Environment.TickCount;
// The tick at which the last input was recorded
int LastInputTicks = 0;
// Ticks since last imput
int IdleTicks = 0;

// Set the struct data
LASTINPUTINFO LastInputInfo = new LASTINPUTINFO();
LastInputInfo.cbSize = (uint)Marshal.SizeOf(LastInputInfo);
LastInputInfo.dwTime = 0;

if (GetLastInputInfo(ref LastInputInfo))
{
// Get the number of ticks
LastInputTicks = (int)LastInputInfo.dwTime;
// Number of idle ticks between SystemUpdate and LastInputTicks
IdleTicks = SystemUptime - LastInputTicks;
}
}

where all this variables are in miliseconds. Now, you can do whatever you want with the variable IdleTicks.

Greetings

Sunday, December 20, 2009

The best podcast in the world

Hello again. For over 6 months, 2 times per week I heard a technological channel, broadcast from Honolulu, which is impressive, entertaining and keeps you abreast of all technology in minutes... but What am I talking about?

See for yourself in http://www.geeknewscentral.com

Calculating distances between two latitude-longitude points

Well, today I decided to make a little program for my mobile phone and I have to calculate the distance between two points (latitude, longitude) ... How can I do?

Haversine formula
The haversine formula is an equation important in navigation, giving great-circle distances between two points on a sphere from their longitudes and latitudes. These names follow from the fact that they are customarily written in terms of the haversine function, given by haversin(θ) = sin^2(θ/2).

For any two points p1=(lat1,long1), p2=(lat2,long2) on a sphere (like earth)
haversin(d/R) = haversin(lat1-lat2) + cos(lat1)cos(lat2)haversin(long2-long1)

where
R = earth’s radius (6,371km)
d = distance between the two points along a great circle of the sphere.

Now we can calculate our "d" doing:
R = 6371km
Δlat = lat2− lat1
Δlong = long2− long1
a = sin^2(Δlat/2) + cos(lat1).cos(lat2).sin^2(Δlong/2)
c = 2*atan2(sqrt(a), sqrt(1−a))
d = R*c

The Haversine formula is useful for numerical calculation even at small distance, but if it isn't necessary to use such precision we can use the spherical law of cosines.
d = R * acos(sin(lat1).sin(lat2)+cos(lat1).cos(lat2).cos(long2−long1))

easier and faster to calculate.

For example:

var R = 6371;
var d = R * Math.acos(Math.sin(lat1)*Math.sin(lat2) +
Math.cos(lat1)*Math.cos(lat2) *
Math.cos(lon2-lon1));

Thursday, December 17, 2009

Virus battery could 'power cars'

Woow!, That's something that I didn't know and it's amazing.

Viruses have been used to help build batteries that may one day power cars and all types of electronic devices.

You will find the complete article here.

Wednesday, December 16, 2009

Congress approves NASA budget

The US Congress has passed a spending bill that would fully fund NASA for 2010 while also requiring the agency to stick to its current exploration plan. An omnibus spending bill for a variety of federal agencies, including NASA, was announced last week after House and Senate negotiators reconciled their versions of individual spending bills; the House passed the final version Thursday and the Senate on Sunday. The bill provides $18.7 billion for NASA for fiscal year 2010, approximately the same amount requested earlier this year by the White House. The bill does include a provision that requires NASA to continue work on the current Constellation program using the funding provided in the bill: and cancellations, new programs, or other changes require the approval of Congress in a subsequent appropriation.

More information in Space Today

Monday, December 14, 2009

Small example of LINQ in C# to sort lists of structures

Imagine we have a list of things and we want to display them in a certain order.
Suppose we have a structure named Attribute that contains the Name and Surname variables. Now we create a list of structures named AttributeList and we want to sorted by Name, then we can do:

var sorted = (from att in AttributeList orderby att.Name ascending select att);

foreach (Attribute att in sorted)
Console.WriteLine(att.Name);


And the same for the case of order by Surname.

I hope this is helpful.

Thursday, December 10, 2009

How calculate days between two dates using javaScript

Yesterday, I needed to calculate the days between two different dates so I decided to do it using javaScript. Here is my example:


<script language="javascript">
function daysbetweenDates()
{
var iY=document.getElementById("iY").value;
var iM=document.getElementById("iM").value;
var iD=document.getElementById("iD").value;

var fY=document.getElementById("fY").value;
var fM=document.getElementById("fM").value;
var fD=document.getElementById("fD").value;

start = new Date(iY,iM,iD);
end = new Date(fY,fM,fD);
var t2 = end.getTime();
var t1 = start.getTime();
diff = t2 - t1;
result = Math.floor(diff/(1000*60*60*24));

alert(result + " days online");
}
</script>

Start (YYY/MM/DD):<br>
<input type="text" id="iY">
<input type="text" id="iM">
<input type="text" id="iD">

End (YYY/MM/DD):<br>
<input type="text" id="fY">
<input type="text" id="fM">
<input type="text" id="fD"><br>

<a href="#" onclick="daysbetweenDates();">Calculate</a>

Monday, December 7, 2009

Let's begin!

My first day here, my first post, my first blog and... I hope many more. Here you will find information on technology, mathematics, programming and whatever comes up.