2009年11月26日 星期四

C# Timestamp

1.Define a class, HiResDateTime

public class HiResDateTime
{
private static DateTime _startTime;
private static System.Diagnostics.Stopwatch _stopWatch = null;
private static TimeSpan _maxIdle = TimeSpan.FromSeconds(10);

public static DateTime UtcNow
{
get
{
if ((_stopWatch == null) ||
(_startTime.Add(_maxIdle) < DateTime.UtcNow))
{
Reset();
}
return _startTime.AddTicks(_stopWatch.Elapsed.Ticks);
}
}

private static void Reset()
{
_startTime = DateTime.UtcNow;
_stopWatch = System.Diagnostics.Stopwatch.StartNew();
}
}


2.How to use:

Console.WriteLine("{0}", HiResDateTime.UtcNow.ToString("yyyyMMddHHmmssffff"));

2009年11月21日 星期六

WRONG HOLE with DJ Lubel, Taryn Southern and Scott Baio



I took her on a date things seemed so bright
I knew I would not need my youporn tonight
We go to her place and we fooled around
We throw all our clothes to the ground

We begin as she turns out the lights
I start but feel something so very extra tight
I hear her cry and I see her frown
I look at the condom it is all brown

Last night I stuck it in the wrong hole
I'm so sorry from the bottom of my soul
Cause I stuck it in the wrong hole

Try some preparation H it'll make you feel better
In my defense those holes are so close together
Oh baby baby don't feel defiled
It's a common accident during doggy style

It was so dark I couldn't see so good
I had no idea where I put my wood
I want to make things better want to make it alright
If you want you can put on a strap on and give it back to me all night ( I'd rather if she didn't)

Last night I stuck it in the wrong hole
I'm so sorry from the bottom of my soul

I never ever want to make you feel hurting
I guess that's why God made that hole not for inserting
Tell me how you feel baby please don't pause
Now I know how they feel in that HBO show OZ
Maybe take some advit your pain it will fix
From the way you are walking you can compete in the special olympics
If this was Alabama we would be on trial
That's how my mom took my temperature when I was a child

I've got a confession and I think you wont mind
I kinda liked when you put it in my behind
I don't know baby I'm no Sodomite
Can't we just try it again tonight?

Alright!

Every night I stick it in the wrong hole
It's so much fun and we don't need no birth control
When we stick it in the wrong hole.

I stuck in your ass.

2009年11月13日 星期五

C# Thread Implementation

I will demonstrate 3 types of thread implementations in C#. They are ThreadPool.QueueUserWorkItem(), Thread, and Win32 thread.

1. ThreadPool.QueueUserWorkItem

class Program
{
static void Main(string[] args)
{
string message = "Hi";
ThreadPool.QueueUserWorkItem(DoSomething, message);
}

//The method to be used in ThreadPool.QueueUserWorkItem, must has one parameter
//with object
static void DoSomething(object stateInfo)
{
Console.WriteLine("DoSomething: stateInfo={0}", stateInfo.ToString());
}
}


2. Thread
First, create a Daemon class. This class has a constructor with values. Besides, it has a delegate callback function.

public class Daemon
{
private string title;
private int val;

private DelegateCallbackFunc callback;

public Daemon(string _title, int _val, DelegateCallbackFunc _callback)
{
title = _title;
val = _val;
callback = _callback;
}

public void DoSomething()
{
try
{
Console.WriteLine("DoSomething: _title={0}, _val={1}", title, val);
title = title + " Dude!";
val = val + 1;

//Callback
if (callback != null)
callback(title, val);
}
catch (ThreadAbortException ex)
{

}
finally
{

}

}
}

//Declare a delegate callback method
public delegate void DelegateCallbackFunc(string _title, int _val);

Second, create and start the thread.

class Program
{
static void Main(string[] args)
{
Daemon d = new Daemon("Hi", 0, new DelegateCallbackFunc(CallbackFunc));
Thread t = new Thread(new ThreadStart(d.DoSomething));
t.Start();
}

public static void CallbackFunc(string _title, int _val)
{
Console.WriteLine("CallbackFunc: _title={0}, _val={1}", _title, _val);
}
}

3. Use win32 thread
First, create a class with extern declaration.

public class Win32
{
[DllImport("kernel32.dll", SetLastError = true)]
public static extern int CreateThread(ref SECURITY_ATTRIBUTES lpThreadAttributes,
int dwStackSize, Delegate lpStartAddress, ref object lpParameter,
int dwCreationFlags, ref int lpThreadId);

[StructLayout(LayoutKind.Sequential)]
public struct SECURITY_ATTRIBUTES
{
public int nLength;
public IntPtr lpSecurityDescriptor;
public int bInheritHandle;
}
}

public delegate void ExecuteFunc();

Second, use this Win32 class in main program.

class Program
{
static void Main(string[] args)
{
Daemon d = new Daemon("Hi", 0, new DelegateCallbackFunc(CallbackFunc));
ExecuteFunc ef = new ExecuteFunc(d.DoSomething);

Win32.SECURITY_ATTRIBUTES sa = new Win32.SECURITY_ATTRIBUTES();
object lpParameter = new object();
int lpThreadId = 0;
Win32.CreateThread(ref sa, 0, ef, ref lpParameter, 0, ref lpThreadId);
}
}


Source code is here.

2009年9月16日 星期三

C# byte[] string Convert

byte[] → string

byte[] byteArr = new byte[]{1,0};
string s = System.Text.Encoding.ASCII.GetString(byteArr);

string → byte[]

System.Text.Encoding.ASCII.GetBytes(s);

2009年8月29日 星期六

2009/08/29 汐止←→五分山氣象站

去程:汐止→汐平公路→平溪→106縣道(十分、74.5公里處五分山氣象站入口)→五分山氣象站

回程:五分山氣象站→106縣道(十分、平溪)→汐平公路→汐止

總里程:74.99KM
總騎乘時間:5:28:44

行程圖片




2009年8月18日 星期二

Java HashMap VS C# Dictionary



Java Code:

HashMap< String , String > hm = new HashMap< String , String >();
hm.put("A", "A--Content");
hm.put("B", "B--Content");
//Get all value
for(Iterator< String > it = hm.keySet().iterator(); it.hasNext();){
String key = it.next();
System.out.println("Key: "+ key + ", value: "+hm.get(key));
}
//Get specific value
System.out.println(hm.get("A"));


C# Code:

Dictionary< string , string > dict = new Dictionary< string , string >();
try
{
dict.Add("A", "A--Content");
dict.Add("B", "B--Content");
//Get all value
foreach (KeyValuePair< string , string > kvp in dict)
{
Console.WriteLine("key: {0}, value: {1}", kvp.Key, kvp.Value);
}
//Get specific value
string val = "";
dict.TryGetValue("A", out val);
Console.WriteLine("value="+val);
//or
Console.WriteLine("value=" + dict["A"]);
}
catch (Exception)
{
//The method "Add" will throw exception if key already existed!!!
Console.WriteLine("Can't insert same key");
}

2009年8月2日 星期日

8/2 汐止 ←→ 冷水坑

去程:汐止→河濱自行車專用道(汐止→東湖→南港→松山→內湖→大直)→大直美麗華→劍南路至善路三段71巷平菁街(平等國小→台北奧萬大)→菁山路菁山路101巷→冷水坑

回程:原路折返

心得:冷水坑真的很不好騎,連續的超陡坡,不過從至善路三段71巷開始至終點冷水坑,不落地完成。


總騎乘距離:78.53 KM
總騎乘時間:5:45:26