Monday, August 08, 2005

BeginInvoke Method

BeginInvoke Method

BeginInvoke is a method of Control, and it executes a delegate asynchronously on the thread that the control's underlying handle was created on.

Sunday, August 07, 2005

How virtual Memory works?

The virtual memory system allows only part of the memory required by the process to be resident in physical memory. Say, if a process requires 500 mb memory, but it may only have 20% memory sitting in physical memory, the other 80% may actually stay on the disk.

Demand Paging

1>When a process requests a block of memory, it check the internal page table for this process to determine whether the reference was a valid or invalid memory access.
2>If the reference is invalid, we terminate the process. If it was valid, but we have not yet brought in that page, we now page it in.
3>We find a free frame by taking one from the free-frame list.
4>We schedule a disk operation to read the desired page into the new allocated frame.
5>When the disk operation is complete, we modify the internal table kept with the process and the page table to indicate that the page is now in memory.
6>We restart the instruction that was interrupted by the illegal address trap.

Paging

1>Paging allows the logically continuous memory to be scattered through the physical memory out of order.
2>Suppose the logical address space is 2**m, and the page size is 2**n, then the high order m-n bits are designated as page number, while the lower n bits are designated as offset after the particular physical page has been found.
3>The TLB is the quick look up to speed up the page table translation, it will quickly find the entry for the logical address. If the TLB is missing, then we need to look up the page table to find the right entry.
4>If the logical address space is very large, and the page size is relatively small, it will lead to a very large page table. We have to use a couple of techniques such as "multi level page table", "inverted page table" to reduce the size.

Finalizer and Disposer

Finalizer is an implict cleanup, which relies on the GC to clean up the resources, while the disposer is explicit cleanup, which allows to clean up the resources explictly. The .Net supports both patterns, so it is very important to distinguish them. In general, explicit cleanup should be used favorably over the implict cleanup, since the resources are generally more precious than memory, we shouldn't really rely on the GC to hold it for long time and clean it after an unspecified time.

Annotation (Brian Grunkemeyer): There are two different concepts that are somewhat intertwined around object tear-down. The first is the end of the lifetime of a resource (such as a Win32 file handle), and the second is the end of the lifetime of the object holding the resource (such as an instance of FileStream). Unmanaged C++ provided destructors which ran deterministically when an object left scope, or when the programmer called delete on a pointer to an object. This would end the resource’s lifetime, and at least in the case of delete, end the lifetime of the object holding onto the resource. The CLR’s finalization support only allows you to run code at the end of the lifetime of the object holding a resource. Relying on finalization as the sole mechanism for cleaning up resources extends the resource’s lifetime to be equal to the lifetime of the object holding the resource, which can lead to problems if you need exclusive access to that resource or there are a finite number of them, and can hurt performance. Hence, witness the Dispose pattern for managed code, allowing you to define a method to explicitly mimic the determinism & eagerness of destructors in C++. This relegates finalization to a backstop against users of a type who do not call Dispose, which is a good thing considering the additional restrictions on finalizers.

In the Dispose(bool disposing) method, if the diposing passed in is true, it means that we can explicitely use other reference types that refer to other objects knowing for sure that those other objects have not been finalized or disposed yet. If it's false, we cannot refer to other resources since those objects may have already been freed.

When we inherit a class which has already correctly implemented "IDispose" pattern, we need only override the Dispose (bool disposing) class, we don't need to override the Finalize and Dipose class.

Saturday, August 06, 2005

Internal Fragmentation and External Fragmentation.

In the memory allocation model, if the memory is partitioned into the fixed size blocks[M bytes a block], then when a process(N bytes) is allocated into the memory, it will fit the first N/M blocks, and occupy the part of the last block, and this is called "internal fragmentation", since the fragment is internal to a block.

The other model is called "external fragmentation", the memory is NOT partitioned into the fixed size. It will maintain a free list of "available" memory blocks and select the best fit for the waiting process. The fragment is external to any occupied blocks so it is called external fragmentation.

Wednesday, August 03, 2005

What is delegate in C#

Delegate is a class, NOT a method. It is a class inherited from System.Delegate or System.MulticastDelegate, which has two properties called "Target" and "Method". The target is the class instance on which the current delegate invokes the instance method, if it is null, it means it's a "Static" calling. MethodInvoker or EventHander are two special delegates treated in a fast manner by Invoke and BeginInvoke. MethodInvoker takes no parameters and returns no value, and EventHandler takes two parameters and returns no value.

Notifications

Notifications

It takes me a while to figure out why the legend control is not updated correctly until I found this sentence.

The applications that rely solely on WM_VSCROLL (and WM_HSCROLL) for scroll position data have a practical maximum position value of 65,535.


The GetScrollInfo function enables applications to use 32-bit scroll positions. Although the messages that indicate scroll-bar position, WM_HSCROLL and WM_VSCROLL, provide only 16 bits of position data, the functions SetScrollInfo and GetScrollInfo provide 32 bits of scroll-bar position data. Thus, an application can call GetScrollInfo while processing either the WM_HSCROLL or WM_VSCROLL messages to obtain 32-bit scroll-bar position data.

To get the 32-bit position of the scroll box (thumb) during a SB_THUMBTRACK message in a WM_HSCROLL or WM_VSCROLL message, call GetScrollInfo with the SIF_TRACKPOS value in the fMask member of the SCROLLINFO structure. The function returns the tracking position of the scroll box in the nTrackPos member of the SCROLLINFO structure. This allows you to get the position of the scroll box as the user moves it. The following sample code illustrates the technique.

SCROLLINFO si;
case WM_HSCROLL:
switch(LOWORD(wparam)) {
case SB_THUMBTRACK:
// Initialize SCROLLINFO structure

ZeroMemory(&si, sizeof(SCROLLINFO));
si.cbSize = sizeof(SCROLLINFO);
si.fMask = SIF_TRACKPOS;

// Call GetScrollInfo to get current tracking
// position in si.nTrackPos

if (!GetScrollInfo(hwnd, SB_HORZ, &si) )
return 1; // GetScrollInfo failed
break;
.
.
.
}

Wednesday, July 27, 2005

Test the E9-1-1 Server.

It has been pretty stressful for me to write the testing code for the E9-1-1 server these days. Multithreading programming is always challenging, it's hard to debug, the code is flowing all around, and it's easy to make mistakes with those locks, monitors, wait handles. But it's also enjoyful to dig into those codes.

After finishing the test codes, the server part also needs extensively refactoring, the I/O completion port should be used to replace the polling, the so called "virtual multithreading" should be replaced by true multi-threading.

Tuesday, July 26, 2005

Bing will go to Hospitality and Tourism Management Program - College of Charleston

Hospitality and Tourism Management Program - College of Charleston

This blog is a little bit late, but still, I am very proud of him when I heard that he will head to College of Charleston to become a faculty.

Bing inspired me a lot in my life. When I was still at Nanjing University, I saw him get very high score in GRE, go abroad successfully, all of those things have a very good impact on my life.

After coming to states, we have choosing different roads. He is finishing his PH.D. and then go to Cornell , firstly in the CS master program, then transfer to a PostDoc in the Info Lab. I know his road is not smooth, but he goes through this step by step.

I hope he will do well in the future as a faculty, which he could be even 7 years ago in Nanjing University.

Wednesday, July 20, 2005

中文博客.

写写中文试试看.

keyboard shortcut assignment in textbox

keyboard shortcut assignment in textbox
All 2 messages in topic - view as tree


I'm building a form to allow the user to assign a keyboard shortcut.
Basically I have a textbox that I want to work like the "Press shortcut
key(s)" in the Visual Studio keyboard options, the user does the key
combination and it shows up in the textbox. It looks like it works on the
KeyDown event. The KeyDown event has an argument of KeyEventArgs. The docs
say "You can use constants from Keys to extract information from the KeyData
property. Use the bitwise AND operator to compare data returned by KeyData
with constants in Keys to obtain information about which keys the user
pressed. To determine whether a specific modifier key was pressed, use the
Control, Shift, and Alt properties." Unfortunately there is no example and
I don't know how to do bitwise compares in c#. Can someone help me out
here?

thanks
Paul




first, set Form1.KeyPreview value to "true"

then, on Form's KeyDown Event

private void Form1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs
e)
{
//Note: "T" is a sample designated shortcut key for textbox
if (e.Modifiers.ToString().ToUpper() == "ALT" &
e.KeyCode.ToString().ToUpper() == "T")
this.textBox1.Select();

}

gani

How Static Method works.

[Question:]
What if two users call the static method at the same time? Since the
method is static, is any data within this method (parameters passed in and
local variables declared within the method) also considered static and shared for all users of the class?

[Answer:]
If the method is not accessing other static variables,
and is only working with references/values that are stored on the stack (in
other words, declared in the function), then you should have nothng to worry
about. The only thing you have to worry about at this point is other
resources you might be accessing (for example, the same file, if you are
working with files).

Tuesday, July 19, 2005

Can you find YY on this page?



This picture is supposed to taken by a professional photographer, well, can you tell how professional it is. If you cannot, then you have the same idea as mine, the so-called professional photographer is totally bullshit.

Monday, July 18, 2005

The priorty date issue.

[Email from my attorney:
Jianwei

You are going to create a serious problem for yourself if you continue to try and use multiple avenues to push this along. The DOL and USCIS are going to start to push back. Up to now, I think your calls and actions have been just a question of impatience, but you are about to cross the line to a problem. I have seen cases where the agencies get calls from several sources, dig in their heels, and do nothing just to spite the individual because they feel there is too much pressure.

Please, I am serious. If you want me to work on this, you are going to have to back off and be patient. If you are convinced that you need to call dozens of people and push hard, I will be happy to submit a withdrawal letter, and you can handle it on your own. However, I can't do both. You need to make a decision whether you are going to handle your case on your own, or whether you want me to continue to work on it.

I called John today and left a voice mail. I will talk to him when he calls, and see what I can do to find out where in the process the information is stuck, if in fact it is stuck at all. But if you are going to make calls through Congressional offices, have the lawyer call, have the manager call, and check back a few days later for all, you are going to sabotage the process. I know that the government agencies will ignore the file if they think that is what is going on.
]

Well, I feel a little bit frustrating. I think Rob did a pretty good job on my case, and I don't have any issues with him. After all, the whole thing is not his responsbilty. But it still frustrates me, I have been waiting for this for the last three years, and every step seems to have a lot of pains. I adimit it is an issue because I have limited ability. If I am truley outstanding, I may not have to go through this painful process.

Life is life, you just have to live with it. If you spend more time improve yourself, there will be less pain ahead.

Sunday, July 17, 2005

Columbus, Ohio: Dentist Reviews, Dentist Ratings, Dentist Recommendations and Dentist Help.

Columbus, Ohio: Dentist Reviews, Dentist Ratings, Dentist Recommendations and Dentist Help.

Well, I was suggested by my dentist to get my wisdom teeth out. Since xiaoyi had very bad experiences when she did it two year ago, and I am a little bit worried. I don't want to lie on the bed for two weeks after doing the surgery.

This seems a pretty good place to see other people's reviews about a particular dentist. Unfortunately, it is not very complete.

Tactus Touch Typing Keyboard - Keyboard Posture - Correct Posture

Tactus Touch Typing Keyboard - Keyboard Posture - Correct Posture

I feel a little bit hurt on my back of arms, and I think it is caused by incorrect posture of typing posture. It is very important to keep it right.

Friday, July 15, 2005

A regular expression help from C# newsgroup

: I am trying to parse out the apratment number in a regular expression :
:
: If I use
:
: Regex regex = new
: Regex(@"[...](?\bAPT|#|UNIT\b)[...]",
: RegexOptions.ExplicitCapture);
:
: I will be able to parse out "100 main ST # C)
:
: But if I move "#" position in regular expression, I won't be able to
: parse out the same address anymore.
:
: Regex regex = new
: Regex(@"[...](?\bAPT|UNIT|#\b)[...]",
: RegexOptions.ExplicitCapture);

The latter fails to match because there's no word boundary (\b) between
an octothorpe and a space. Remember, a word boundard occurs between a
\w and a \W or vice versa, but '#' and ' ' both match \W.

A start in the right direction is (line breaks inserted):

(?.+)
(?\b(APT|UNIT)\b|#(?=\s+))
(?.+)

Hope this helps,
Greg

Thursday, July 14, 2005

The powerful unit testing.

The whole idea of unit testing is to ensure that you won't break already-working codes when you refactoring. Address Parsing is a very good example, there are a lot of different situations, and it's very easily that you break some codes when you modify the regular expression rules.

The better way is to put everything into an XML file, and run the parsing through the XML file every time when modifying something. That way, you will always ensure that something already working well won't be broken when regular expression rules are added, modified, or delete.

Another way to learn from JTS, the great library.

Wednesday, July 13, 2005

How JTS cropping works.

JTS is very cool, the algorithm is complex but really powerful. All the intersection/union/difference/symmetry difference operations in all kinds of geometry types could be done in a very generic algorithm.

How the cropping works, here is what I understood:

1> The first step is to get all the intersection points between two geometries. The algorithm used here is the sweep-line algorithm.

2> Then it will need to create the split edges based on those intersection points. The split edge is the edge between two intersection points on the same geometry.

3> For each split edge, we have two directed edge end generated from it. One is starting from the beginning, and the other one is starting from the end. For the labelling, if it starts from the beginning, it will use the parent edge label, if it starts from the end, it will filp the parent edge label.

4> Based on those directed edges, we can create a planar graph, and all the vertices in the graph are made from the starting point of the directed edge end.

5> We need compute the labels (topological relationship) of the directed edge and vertice relative to the two different geometries.
a> Each edge end in the edge end map has a label.
b> The edge end star map has its own label, which will be used to update the label of the node.
c> Get the starting edge in the edge end star map, loop through each edge in the edge end map, and compute the labelling location.
d> We also need to merge the label from one directed edge with its symmentric directed edge.
e> Merge the label of the node with the label of the edge end star map.


6> We can determine whether each edge is in result or not based on the topological relationship with two geometries. For each different operation like intersection / union / difference, the criteria to determine whether it is in result is different.

7> We sort each vertice in the planar graph firstly by X coordinate, then by Y coordinate. By looping each vertice in the graph, we will determine how the edges in each vertice link together.

8>For the polygon building, we must determine which edge will actually qualify for the result polygon. There are generally three conditions which will make it qualify for the polygon edge:
8-a. The label of the edge has to show that it is an area edge to both geometries.
8-b. The edge is not an interior area edge. The interior area ege means that the label is an area label for both geometries and for each geometry, both sides are interior.
8-c. The right sides of the edge to both geometries should be inside.


9>We build the maximal edge ring and minimum edge ring based on linking results.
The maximal edge rings will be built firstly, and then we loop through each node. If there are more than one outgoing directed edge which is in result, we must consider the minimal edge ring too. Because if we have one more outgoing edge in one particular vertice, it means that we cannot simply link the edge to form a polygon, we must do another step to form the minimum edge ring, which is based on the maximum edge ring.

10> The actual geometries are built based on the linking results. If there are only maximum edge rings, then build polygons based on that. If there are minimum edge rings, then we need build polygon based on it instead of maximum edge rings.

11> We will collect the geometries in the following order: polygons firstly, lines second, and points last. The lines could be generated by an input geometry of line, or
two touching polygons.
GISResearch