Monday, April 28, 2008

LINQ by example

http://msdn2.microsoft.com/en-us/vcsharp/aa336746.aspx

Wednesday, March 19, 2008

MSMQ Multicast with .net

msmq 3.0 and above supports multicast with PGM

1. Create a non transactional queue and set the multicast address

2. Create a publisher
MessageQueue msq = new MessageQueue("formatname:MULTICAST=234.1.1.1:1234");
msq.Formatter = new BinaryMessageFormatter();
msq.Send("hello world");
msq.Close();

3. Create a consumer
MessageQueue q = new MessageQueue(@".\private$\myqueue");
q.Formatter = new BinaryMessageFormatter();
Message m = q.Receive();
Console.WriteLine(m.Body.ToString());
Console.ReadLine();
q.Close();

Tuesday, February 12, 2008

Simple .net BST

using System;
using System.Collections.Generic;
using System.Text;

namespace BinarySearchTree
{
internal class Node where T: IComparable
{
private T _data;
private Node leftNode = null;
private Node rightNode = null;

internal Node(T data)
{
_data = data;
}

internal Node LeftNode
{
get
{
return leftNode;
}
set
{
leftNode = value;
}
}

internal Node RightNode
{
get
{
return rightNode;
}
set
{
rightNode = value;
}
}

internal int CompareTo(T val)
{
return _data.CompareTo(val);
}
}
}


----------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Text;

namespace BinarySearchTree
{
public class BinarySearchTree where T : IComparable
{
private Node rootNode = null;

public BinarySearchTree()
{
}

public void Insert(T val)
{
rootNode = Insert(rootNode, val);
}


private Node Insert(Node node, T val)
{
if (node == null)
{
node = new Node(val);
return node;
}

if (node.CompareTo(val) >= 0)
{
node.LeftNode = Insert(node.LeftNode, val);
}
else
{
node.RightNode = Insert(node.RightNode, val);
}

return node;
}
}
}

----------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Text;

namespace BinarySearchTree
{
class Program
{
static void Main(string[] args)
{
BinarySearchTree bns = new BinarySearchTree();

bns.Insert(3);
bns.Insert(2);
bns.Insert(10);
bns.Insert(1);
bns.Insert(4);
bns.Insert(11);
}
}
}

Tuesday, February 5, 2008

Simple Pointers

int cmp(const int i, const int j)
{
if (i == j)
{
return 1;
}

return 0;
}

void search(const int val, const int *p, int (*compare)(const int i, const int j))
{
int ret = 0;
for (int i = 0; i < 10; i++)
{
ret = compare(val, p[i]);
if (ret == 1)
{
printf("value found");
return;
}
}

printf("value not found");
}

int _tmain(int argc, _TCHAR* argv[])
{
int num[] = {100, 1,2,3,4,15,6,7,8,9,10};
int *p = num;
int **pp = &p;

printf("*p = %d\n", *p);
printf("p[0] = %d\n", p[0]);
printf("p[1] = %d\n", p[1]);
printf("*p++ = %d\n", *p++);
printf("*p = %d\n", *p);
printf("*p-- = %d\n", *p--);
printf("*p = %d\n", *p);
printf("*++p = %d\n", *++p);
printf("*--p = %d\n", *--p);
printf("*(p++) = %d\n", *(p++));
printf("*p = %d\n", *p--);
printf("*p+5 = %d\n", *p+5);
printf("*(p+5) = %d\n", *(p+5));

printf("**pp = %d\n", **pp);

search(2, num, cmp);


getc(stdin);
return 0;
}


OUTPUT:
*p = 100
p[0] = 100
p[1] = 1
*p++ = 100
*p = 1
*p-- = 1
*p = 100
*++p = 1
*--p = 100
*(p++) = 100
*p = 1
*p+5 = 105
*(p+5) = 15
**pp = 100
value found

Windows Threading

include windows.h
HANDLE CreateThread(LPSECURITY_ATTRIBUTES secAttr,
SIZE_T stackSize,
LPTHREAD_START_ROUTINE threadFunc,
LPVOID param,
DWORD flags,
LPDWORD threadID);

Thread can be destroyed by calling CloseHandle(HANDLE );
or BOOL TerminateThread(HANDLE thread, DWORD status); //no cleanup
VOID ExitThread(DWORD status); //cleanup stacks

The Visual C++ alternatives to CreateThread( ) and ExitThread( ) are _beginthreadex( ) and _endthreadex( ). Both require the header file .

DWORD SuspendThread(HANDLE hThread);
DWORD ResumeThread(HANDLE hThread);
DWORD GetPriorityClass(HANDLE hApp);
BOOL SetPriorityClass(HANDLE hApp, DWORD priority);
HANDLE GetCurrentThread(void); //get the main thread

Windows Synchronization:
- Semaphore
- Mutex //global object can be used by multi process
- Event Object
- Waitable Timer
- Critical section

MUTEX:
HANDLE CreateMutex(LPSECURITY_ATTRIBUTES secAttr, BOOL acquire, LPCSTR name);
name = NULL => localized sync
A mutex handle is automatically closed when the main process ends. You can explicitly close a mutex handle when it is no longer needed by calling CloseHandle( ).
DWORD WaitForSingleObject(HANDLE hObject, DWORD howLong);
BOOL ReleaseMutex(HANDLE hMutex);

if(WaitForSingleObject(hMutex, 10000)==WAIT_TIMEOUT) {
// handle time-out error
}

// access the resource

ReleaseMutex(hMutex);















Friday, January 25, 2008

ASP .Net Ajax for VS 2005

This section discuss the basic steps to add ajax to your existing asp .net page for VS 2005. Three basic steps to follow:

- Download and install ASP .net AJAX extension
go to http://www.asp.net/ajax/downloads/

- In the toolbox of VS2005 you should have the AJAX Extensions added.
What's important to note is the ScriptManager, and UpdatePanel. The rest is more advance topics you can find reference from http://www.asp.net/ajax

- So let's see how to simple ajax behavior to an existing web asp .net page.
1. Copy the web.config from the ajax download to your own web.config
2. Add Scriptmanager to the page
3. Add the UpdatePanel to the page section you want postback with the
ContentTemplate section.

That's all to do to add ajax capbility to section of your page. Note that the page lifecycle is still called, but during the page rendering phase only the required updatepanel section is transfered.

Wednesday, October 24, 2007

Trace system calls

A nice way to trace a system call is to use the strace command.

ex: strace ls

And: strace -c ls
to get some statistics. Really nice debugging tool for filesystem writer.