Thứ Sáu, 28 tháng 1, 2011

8 Cách khắc phục lỗi không truy cập vào được Facebook

Vào những ngày cuối năm 2010, người dùng Internet Việt Nam không thể truy cập vào được Facebook. Bạn đã thử nhiều cách, trong đó có thiết lập DNS mới (Google DNS, Open DNS, Norton DNS,…) cho máy tính nhưng không hiệu quả. Bài viết sau sẽ hướng dẫn các bạn hướng giải quyết vấn đề này qua 8 cách được trình bày dưới đây với thao tác thực hiện dễ dàng và hiệu quả.
1 / Truy cập thông qua địa chỉ phụ Facebook Lisp4:
- Đây là một cách đơn giản và hoạt động rất tốt. Sau khi các ISP chặn Google DNS và những cách khác để truy cập vào Facebook, chúng ta có thể tiếp tục sử dụng mạng xã hội Facebook tại địa chỉ: http://www.lisp4.facebook.com.
- Nếu bạn đang sử dụng Windows, bạn vẫn có thể truy cập Facebook tại địa chỉ mặc định của nó bằng cách thực hiện các bước đơn giản:
Chuyển đến C: \ Windows \ System32 \ drivers \ etc. Mở file hosts bằng Notepad để chỉnh sửa, thêm 3 dòng này ở phía dưới và lưu lại.
153.16.15.71 facebook.com
153.16.15.71 www.facebook.com
153.16.15.71 apps.facebook.com

Bây giờ, hãy làm mới trình duyệt và bạn có thể truy cập Facebook bình thường.
2 / Sử dụng Proxy / Socks:
Đây là một cách rất phổ biến để truy cập bất kỳ trang web mà bạn muốn mà không có bất kỳ hạn chế. Tất cả bạn cần là tìm một proxy tốt để sử dụng. Nhưng khi sử dụng proxy, tốc độ của bạn có thể giảm do thời gian đáp ứng của máy chủ proxy. Bạn cũng có thể tìm mua một proxy / socks chất lượng tốt để sử dụng. Nếu không, chỉ cần tìm proxy miễn phí từ trang web chia sẻ như sockslist.net, aliveproxy.com,… Tất cả proxy hoặc socks có cùng định dạng:
123.123.123.123:1234, với con số trước ":" là địa chỉ IP (123.123.123.123), và sau đó là cổng (1234). Để sử dụng nó, bạn cần cấu hình trình duyệt của bạn như sau:
- Firefox: Tools > Options > Advanced > Networks > Settings > Manual Proxy Configurations.
- Internet Explorer: Tools > Internet Settings > Connections > Lan Settings > Use a Proxy Server for you LAN.
- Chrome: Options > Under the Hood > Change Proxy Settings > Connections > Lan Settings > Use a Proxy Server for you LAN.
- Opera: Tools > Preferences > Advanced > Networks > Proxy Servers.
Sau đó điền IP và port và áp dụng. Để loại bỏ nó, chỉ cần xóa các dòng thông tin đã nhập để trở về trạng thái mặc định.

Cấu hình Proxy / Socks trong Firefox
Ví dụ: Bạn truy cập vào trang http://sockslist.net để lấy proxy, chọn một quốc gia không bị chặn Facebook (Hoa Kỳ) > nhấn liên kết check. Trong trang mới, bạn sao chép dãy số dưới khung Proxy list to check > các bước tiếp theo bạn làm theo hướng dẫn ở trên.
3 / Sử dụng phần mềm Fake IP:
Một số phần mềm được sử dụng để ẩn danh và che giấu địa chỉ IP như Auto Hide IP, Real Hide IP, Hide IP NG, Invisible Browsing,… giúp bạn truy cập vào được Facebook nhưng có thể làm giảm tốc độ lướt nhưng không đáng kể. Bạn có thể tìm kiếm và tải các phần mềm này trên Google và các diễn đàn tin học.
4 / Sử dụng UltraSurf:
Đây là cách dễ nhất và nhanh nhất để ẩn IP và truy cập vào bất kỳ trang web bạn muốn. Tất cả lưu lượng truy cập sẽ được đưa qua máy chủ UltraReach. Vì vậy, tốc độ hầu như không có thay đổi. Sau khi tải UltraSurf, bạn chỉ cần chạy nó, và cửa sổ Internet Explorer sẽ mở ra để sử dụng. Bạn cũng có thể sử dụng tiện ích UltraSurf với trình duyệt Firefox.
Lưu ý: Để sử dụng trong Firefox, bạn phải tải add-on UltraSurf ngay liên kết bên dưới của trang chủ, cài đặt và kích hoạt nó trong trình duyệt Firefox. Cụ thể: kích dòng chữ WJ Enabled bên dưới góc phải cửa sổ trình duyệt.
UltraSurf hoàn toàn miễn phí và không cần phải cài đặt.
5 / Sử dụng OpenDNS:
Dịch vụ này quá nổi tiếng và quen thuộc với người dùng, bạn có thể xem chi tiết thêm về Open DNS tại http://vi.wikipedia.org/wiki/OpenDNS. Ở đây, tôi chỉ hướng dẫn lại nếu bạn đã quên các bước thiết lập. Trước tiên, bạn cần phải thay đổi cấu hình mạng cho máy mình:
- Với Windows XP:
Vào Control Panel > Network and Internet Connections > Network Connections, kích chuột phải vào Local Area Network và chọn Properties. Trong danh sách kết nối này sử dụng các mục sau đây, chọn Internet Protocol (TCP / IP) và kích Properties.
Trong cửa sổ mới, bạn nhấn chuột Use the following DNS server addresses, và ở Preferred DNS Server, điền 208.67.222.222 và tại Alternate DNS Server điền 208.67.220.220. Sau đó, nhấn OK > OK để kết thúc.
- Với Windows Vista hoặc Windows 7, vào Control Panel > Network and Internet > Network and Sharing Center > View Status > Properties và các cấu hình như XP.
Tìm thêm thông tin tại trang chủ: opendns.com
6 / Sử dụng Proxy an toàn:
Cách an toàn nhất và phổ biến nhất để truy cập vào Facebook khi bị chặn là sử dụng một Proxy an toàn. Có nhiều loại dịch vụ bảo mật Proxy cho phép truy cập Facebook, và những cái tôi thích nhất là các dịch vụ mà không cần yêu cầu cài đặt phần mềm và cấu hình máy tính. Có một vài trang web cung cấp dịch vụ proxy an toàn, một trong những website tốt nhất và được khuyến cáo sử dụng là https://www.access-any-website.com/ (hay https://www.proxy-anywhere.com/). Dịch vụ này sử dụng giao thức mã hóa https:// với khả năng bảo mật cao cấp, tuy nhiên, đây là một dịch vụ trả phí và chỉ cho phép bạn dùng thử trong khoảng thời 10 phút cho mỗi lần đăng nhập.
Với cách này, bạn có thể tận dụng khi mình đang cần thực hiện gấp một tin nhắn quan trọng với bạn bè trên Facebook tại nơi công cộng thiếu an toàn như café Internet,…
7 / Sử dụng Website proxy miễn phí:
Có rất nhiều trang web giúp bạn lướt web thông qua proxy. Ba trong số các trang web tốt nhất là HideMyAss (http://www.hidemyass.com/), Defilter (https://defilter.co.uk/), và Blockedsiteaccess (http://blockedsiteaccess.com/),… Tất cả bạn cần làm là nhập URL của trang web cần truy cập trong hộp văn bản và nhấn Enter.
8 / Sử dụng mạng riêng ảo: Có thể đây là giải pháp cuối cùng, nếu tất cả 7 cách trên đều “bất lực”.
Hotspot Shield tạo ra một mạng riêng ảo (VPN) giữa máy tính của bạn và AnchorFree Internet gateway. Cho phép người dùng Internet truy cập Internet thông qua một máy chủ trung gian (được quản lý bởi máy chủ của Hotspot Shield), do đó, cho phép họ tránh được các cơ chế lọc nội dung (cho nên, tránh được các kiểm duyệt), để đảm bảo an ninh cho thông tin trực tuyến giữa máy tính của người dùng và các máy chủ Hotspot Shield, và để đảm bảo tính ẩn danh của truy cập tới tài nguyên Internet.

Hotspot Shield là một sản phẩm thương mại (miễn phí), tương thích tốt với các hệ điều hành Windows , Mac OS X, đặc biệt có hỗ trợ tiếng Việt. Bạn tải bản cài đặt online tại đây và cài đặt vào máy bình thường.
Chương trình sẽ tự khởi chạy và thường trú dưới khay hệ thống, từ biểu tượng Hotspot Shield bạn có thể kích hoạt / vô hiệu hóa tính năng bảo vệ của chương trình thông qua lệnh “Kết nối / Bật” (Connect / ON – biểu tượng hiển thị màu xanh ) và “Ngắt kết nối / Tắt” (Disconnect / OFF - sẽ có màu đỏ) trên menu ngữ cảnh.
Theo XHTT

Thứ Hai, 17 tháng 1, 2011

Delegate in C#–Part 1

First words: tình cờ lượn lờ trên code project thì phát hiện thấy một series 4 bài viết về delegate trong C#, đọc dòng đầu tiên thấy người ta nói quá đúng (đại loại là ngại sử dụng delegate đấy) nên quyết định ngồi đọc. Đọc một chặp mình quyết định dịch ra lại cho nhớ. Tuy nhiên mình vẫn chưa có xin phép bác tác giả bài này để dịch lại thành tiếng Việt (tại làm biến đăng kí acc ^^), hi vọng bác ấy không biết :D. Cái này mình dịch lại thôi, cho nên đại từ nhân xưng “Tôi” ở đây là của bác ấy ấy, không phải em, hì hì. Vậy là xong phần giới thiệu, sau đây sẽ là nội dung chính.
Xem thêm các bài viết khác:
http://lequangkhai.wordpress.com/2010/12/06/delegate-in-cpart-2/
http://lequangkhai.wordpress.com/2010/12/27/delegate-in-cpart-3/
http://lequangkhai.wordpress.com/2011/01/01/delegate-in-c-part-4/

Introduction

Có rất nhiều lập trình viên thường tìm cách tránh sử dụng delegate trong công việc của mình. Tất cả chúng ta đều biết rằng chúng ta có thể phát triển bất cứ thứ gì chúng ta muốn bằng rất nhiều các công cụ cũng như các phương thức lập trình rất quen thuộc. Vậy tại sao chúng ta lại phải mất thời gian tìm hiểu sử dụng delegate trong khi có nhiều cách khác cũng có thể thực hiện được chức năng tương tự.
Theo tôi thì đây chỉ đơn giản là lời ngụy biện mà thôi – nó chỉ ra một thực tế rằng đôi khi nhiều (hoặc thậm chí là rất nhiều) lập trình viên không hiểu rõ ràng về delegate.

Challenge

Có hàng ngàn các chủ đề, tutorials và blog giải thích về delegates. Đó quả thực là một thách thức không nhỏ cho những ai đang cố gắng giải thích lại một cái gì đó đã được giải thích rất nhiều lần. Tuy vậy, tôi vẫn quyết định viết một vài bài viết với mục tiêu giúp cho programmer có thể hiểu hơn về nó, nhờ vậy bỏ đi cảm giác ngại ngùng khi sử dụng delegate trong các dự án của họ.

The Essence of Delegates

Trước hết, chúng ta sẽ bắt đầu từ nghĩa tiếng anh của từ Delegate. Theo từ điển của Google, delegate được giải thích như sau:
Delegate: a person sent or authorized to represent others, in particular an elected. Representative sent to a conference.”
Ở đây, tôi muốn nhấn mạnh cụm từ “sent or authorized to represent others”. (tạm hiểu là được gởi gắm hoặc có quyền đại diện cho một người khác). So, delegate trong ngôn ngữ lập trình C# là một thực thể có quyền đại diện cho một đối tượng khác nào đó. Nghĩa là chúng ta đang có một thứ gì đó và nó cần được mô tả lại (hoặc đại diện). Vậy thì một câu hỏi được đặt ra đó là: What is “thứ gì đó”?
Trong trường hợp của chúng ta thì nó chính là các phương thức (method), hoặc hàm (function). Các method được đề cập ở đây có thể nằm ở bất kì đâu. Nó có thể thuộc sở hữu của một đối tượng, hoặc chỉ đơn giản là một static method. Nếu một method cần phải trình diễn từ xa (remote representation), nó sẽ phải dùng delegate. Delegate có thể gọi một phương thức dễ dàng mà không cần quan tâm đến khoảng cách xa hay gần giữa nó và method. Vậy thì tại sao Microsoft lại dùng cái tên delegate để đặt tên cho loại thực thể này? Đơn giản bởi vì chức năng chủ yếu của nó là gọi và thực thi bất kì phương thức nào từ xa, tương tự với định nghĩa tiếng Anh của nó. Ngoài ra, có thể hiểu một cách đại khái rằng nó giúp chúng ta thực hiện việc truyền method như một tham số. Chúng ta đã quá quen thuộc với việc truyền một đối tượng vào hàm với vai trò là tham số, nhưng còn đối với method thì sao? Liệu chúng ta có được phép truyền method/function vào hàm như một parameter? Câu trả lời là KHÔNG. Nhưng (và đây chính là sức mạnh thần kì của delegate), đại diện hợp pháp của nó (ở đây là delegate) thì có thể. Khá là dông dài phải không? Cho đến thời điểm này thì chúng ta đã biết rằng một delegate đại diện cho một method (function). Bây giờ chúng ta sẽ đi vào phân tích cách sử dụng delegate, step by step.
Step 1: Declare a delegate
Public delegate void RemoteOperation();
Microsoft tạo ra delegate theo một cách “rất hướng đối tượng”. Khác với con trỏ hàm trong C++, C# sẽ tạo ra một class khi compiler bắt gặp đoạn code trên. Bạn đừng quá ngạc nhiên. Khi bạn khai báo một delegate thì một class đang được tạo ra bởi compiler mà bạn không biết đó thôi. Tên của class được tạo ra sẽ là RemoteOperation. Tùy thuộc vào compiler version mà nó có thể kế thừa từ class MulticastDelegate (với các phiên bản mới nhất) hoặc từ class Delegate. Class MulticastDelegate kế thừa từ class Delegate. Class này cũng có các contructor, điều này có nghĩa là bạn cần phải khởi tạo nó bằng từ khóa new(). Giữa Delegate và MulticastDelegate cũng có những mối quan hệ nhất định. Nếu bạn là một lập trình viên chuyên nghiệp về OOP, hẳn bạn đã từng nghe qua về programming patterns lần đầu tiên được giới thiệu bởi Gang of Four (GOF). MulticastDelegate -> Delegate là một ví dụ hoàn hảo của Structural Composite Pattern (thiệt tình là chưa có biết cái GOF với cái Structural Composite là gì ^^). Nói chung là khái niệm về nó thì rất đơn giản, Delegate class giới thiệu component. MulticastDelegate giới thiệu composite. MulticastDelegate có cài đặt một mảng có kiểu Delegate dùng để lưu trữ các đối tượng delegate (private array). Bạn có thể thêm hoặc bớt các đối tượng của MulticastDelegate từ mảng này theo ý thích. Base class Delegate có một số các properties và methods. Trong đó nó có một method gọi là Invoke(), method này có các dấu hiệu đặc trưng của delegate (đó là kiểu trả về, danh sách các tham số truyền vào, các thành phần này gọi chung là signature). Signature này được lấy từ câu lệnh khai báo delegate của bạn. Trong ví dụ của chúng ta, phương thức Invoke không nhận bất cứ tham số nào và có kiểu trả về là kiểu void. Base class delegate có 2 property đặc biệt quan trọng:
  1. Target – dùng để lưu trữ tham chiếu đến đối tượng mà Method được cài đặt
  2. Method – lưu trữ MethodInfo của đối tượng này
Chúng ta sẽ đi sâu vào các properties này sau.

Note: nếu chúng ta khai báo delegate khác (nhìn bên dưới) thì Invoke method cũng phải có kiểu trả về là kiểu int và nhận 2 tham số, một kiểu integer và một kiểu string. Nói cách khác, Invoke method bắc chước định nghĩa delegate của bạn.
public delegate int DifferentSignatureDelegate (int id , string name );

Bây giờ chúng ta sẽ chuẩn bị một method mà chúng ta muốn delegate mô tả lại. Đây chỉ đơn giản là một static function với tên là DoNothing(), function này không có tham số và return void:
public static void DoNothing()
{
Console.WriteLine( "Nothing in nothing out, no action taken");
} 
Step 2: Instantiate the delegate
RemoteOperation operationVoid = new RemoteOperation(DoNothing);
Lúc này, compiler sẽ tạo ra một instance hoặc class RemoteOperation. Như các bạn thấy, chúng ta sử dụng từ khóa new và một tham số truyền vào contructor của nó, tham số này chính là tên của function, không phải là một string. Bạn còn nhớ Invoke method chứ? Trước khi bắt đầu tạo ra instance, compiler sẽ kiểm tra xem signature của hàm DoNothing có giống với signature của Invoke method của class RemoteOperation hay không. Nếu sai, compiler sẽ trả về một lỗi với nội dung là không tìm thấy cái hàm “somename” nào trùng với delegate “delegatename”. Còn nếu đúng, một đối tượng delegate thuộc kiểu RemoteOperation được tạo ra. Sau đó, compiler tạo một thể hiện mới của class Delegate. Thể hiện này sẽ set value của Target property thành null bởi vì delegate đang là đại diện của một static function. Nếu function mà delegate đại diện không phải là static thì lúc này, chúng ta sẽ đưa vào đối tượng delegate này một hàm thuộc thể hiện của object sở hữu hàm đó. Property còn lại là Method sẽ có giá trị là Void DoNothing(). Sau đó, nó sẽ thêm thể hiện của class Delegate mà ta nói ở trên vào mảng Delegates được cài đặt bên trong đối tượng kiểu RemoteOperation


Hình ảnh này không thực sự là một UML diagram, nó chỉ mô tả những gì xảy ra bên trong khi chúng ta thực hiện một delegate. Đối tượng vừa mới tạo ra sẽ có đầy đủ các thông tin cần thiết để mô tả lại chính xác công việc của function DoNothing(), đó là:
  1. Có signature của function
  2. Biết chính xác function này được đặt ở đâu
  3. Nó có thể gọi function này được.
    Và, bây giờ chúng ta có thể sử dụng đối tượng này làm tham số để truyền vào hàm.
    Step 3: Execute the Delegate
    Việc thực thi delegate (gọi hàm mà nó đại diện) rất đơn giản:
    operatorVoid();
    Câu lệnh này cũng tương đương như việc gọi Invoke method của đối tượng operatorVoid:
    operatorVoid.Invoke();
    Lúc này, chương trình sẽ tìm hàm được đưa vào delegate và thực thi nó, bất kể dù nó nằm ở đâu. Một câu hỏi đặt ra là: một delegate có thể làm đại diện cho nhiều hàm/phương thức khác? Có trả lời là có, hiển nhiên!! Một đối tượng delegate có thể đại diện cho bao nhiêu methods (functions) tùy ý. Chỉ có duy nhất một yêu cầu đó là tất cả các function này đều phải có signature giống tuyệt đối với delegate. Và như chúng ta đã biết, việc này thực hiện được là nhờ mảng lưu trữ các đối tượng có kiểu là Delagate. Bạn có thể xem mảng này trong lúc debug nhờ vào phương thức GetInvocationList(). Bây giờ, chúng ta có thêm một class nữa:
    public class SomeClass
    {
    public void AnotherFunction() {}
    } 
    Sau đó chúng ta sẽ tạo ra một thể hiện của class này:
    SomeClass o1 = new SomeClass (); 
    Thêm tham chiếu đến method AnotherFunction() vào delegate:
    operationVoid += new RemoteOperation(o1.AnotherFunction); 
    Hình ảnh số 2 dưới đây sẽ mô tả những gì có bên trong đối tượng operatorVoid.
Khi bạn gọi method Invoke, tất cả các function trong delegate sẽ được gọi thực thi theo trình tự mà bạn add vào. Bạn cũng có thể remove một function ra khỏi delegate:
operationVoid -= new RemoteOperation(o1.AnotherFunction);

Example

Lí thuyết như vậy là đủ rồi ha, bây giờ chúng ta sẽ code thử vài dòng để demo những gì chúng ta vừa được biết ở trên. Ý tưởng của đoạn code này là dựa trên remote control của TV để bật, tắt.
Trước hết chúng ta khai báo một public delegate:
public delegate void RemoteOperation();
Class Television có 2 method dùng để bật và tắt TV:
public delegate void RemoteOperation()   public class Television
{
private string model;
public string Model { get {return model; } }
// Constructor
public Television (string model)
{
this.model = model;
}
// Turn TV on
public void TurnOn()
{
Console.Writeline(this.model + " is on");
}
// Turn TV off
public void TurnOff()
{
Console.WriteLine(this.model + " is off");
}
} 
Class TVOwner có 2 readonly property: TV và Name
public class TVOwner
{
private Television myTV;
private string name;
public string Name
{
get { return name; }
}
public Television TV
{
get { return myTV; }
}
// Constructor
public TVOwner (string name, string tvmodel)
{
this.name = name;
myTV = new Television(tvmode);
}
}
Class Friend có một biến private thuộc kiểu RemoteOperation – tên của delegate đã được khai báo từ từ trước. Ngoài ra còn có 2 methods:
  1. SetRemoteOperation
  2. RemoveRemoteOperation
Các method này nhận một trong các tham số có kiểu RemoteOperation và add tham số này vào delegate (biến private). Người sử dụng TV có thể gọi trực tiếp phương thức TurnOn và TurnOff, nhưng họ cũng có thể gọi các phương thức này thông qua điều khiển từ xa. Hơn nữa, họ cũng có thể chuyển remote control sang cho người khác sử dụng, và những người này tất nhiên cũng có thể bật/tắt TV bằng remote control. Đây chính là công dụng của delegate.
public class Friend
{
private RemoteOperation remote; // variable of delegate type
private string name;
// Constructor
public Friend (string name)
{
this.name = name;
}   // Set quyen dieu khien cho thiet bi
public void SetRemoteOperation (RemoteOperation remote, string deviceName)
{
this.remote += remote;
Console.WriteLine("Remote operation for " + deviceName + " set");
}   // Remove quyen dieu khien cua thiet bi
public void RemoveRemoteOperation (RemoteOperation remote, string deviceName)
{
this.remote -= remote;
Console.WriteLine("RemoteOperation " + deviceName + " removed");
}   // Thuc thi lenh dieu khien tu xa
public void UserRemote()
{
if (remote != null)
{
Console.WriteLine(name + " clicked remote '" +
remote.GetType().FullName + " "
+ Remote.Method.Name + "'");
remote();
}
}
}   
Đoạn chương trình chính:
/// 
/// Execution program class
///
public class Program
{
///
/// Main procedure
///
///
static void Main( string[] args)
{
//create tv owner:
TvOwner owner = new TvOwner( "Ed", "Samsung");   //create tv owner:
TvOwner nowner = new TvOwner( "Nick", "Sony");   //create friend:
Friend friend = new Friend( "Alex");   //instantiate delegate for turning tv on:
RemoteOperation setTvOn = new RemoteOperation(owner.TV.TurnOn);   //instantiate delegate for turning tv on:
RemoteOperation aaa = new RemoteOperation(nowner.TV.TurnOn);   //instantiate delegate for turnting tv off:
RemoteOperation setTvOff = new RemoteOperation(owner.TV.TurnOff);   //pass function turnOnTv to the friend:
friend.SetRemoteOperation(setTvOn, owner.TV.Model);   //pass function turnOnTv to the friend:
friend.SetRemoteOperation(aaa, nowner.TV.Model);   //friend is using the remote to turn TV
friend.UserRemote();   //remove remote operation from friend:
friend.RemoveRemoteOperation(setTvOn, owner.TV.Model);   //pass function turnOffTv to the friend:
friend.SetRemoteOperation(setTvOff, owner.TV.Model);   //friend is using the remote to turn TV
friend.UserRemote();   //instantiate delegate for static function:
RemoteOperation operationVoid = new RemoteOperation(DoNothing);   friend.RemoveRemoteOperation(setTvOff, "none");   friend.SetRemoteOperation(operationVoid, "none");   friend.UserRemote();   //finish:
Console.Read();
}   public static void DoNothing()
{
Console.WriteLine( "Nothing in nothing out, no action");
}
}
Kết quả khi chạy đoạn chương trình trên:

Tóm tắt

Dễ thấy là một delegate khi thể hiện được instantiated sẽ có kiểu MulticastDelegate. Nhiệm vụ của đối tượng này là để giữ tham chiếu đến một hoặc nhiều phương thức có cùng signature. Signature này được set trong lúc khai báo delegate. Bản thân bên trong mỗi delegate có một mảng lưu trữ các tham chiếu đến mỗi method được add vào delegate, và khi delegate thực thi methode Invoke(), tất cả các method được delegate tham chiếu tới sẽ được gọi thực thi ngay lập tức. Delegate cũng là một đối tượng, do đó chúng ta cũng có thể xem nó như các loại đối tượng khác, nghĩa là chúng ta có thể đưa nó vào hàm với vai trò tham số.
Bạn có thể xem bài viết nguyên mẫu tại: http://www.codeproject.com/KB/cs/delegatespart1.aspx
[To be continued...]

Thứ Tư, 12 tháng 1, 2011

Hướng dẫn cách đổi chế độ phím từ Media -> Function key (Dell laptop)

Cách 1:
Trong Windows 7, bạn vào: Control Panel -> Hardware and Sound -> Adjust commonly used mobility settings -> Fn Key Behavior

Cách 2:
Để thể hiện đẳng cấp, hãy vào BIOS chỉnh :d

Bấm F2 hoặc F12 khi vừa khởi động laptop để vào chế độ BIOS

Thứ Sáu, 31 tháng 12, 2010

Windows XP Setup Simulator 1.0 free download

Windows XP Setup Simulator is a program that teaches you how to install Windows XP, without having to worry you will mess up your hardware or software. It just simulates installing Windows XP as if it was the real setup. It reproduces every step exactly as it is in the real setup, and it does not restart your computer. This program can also be used as a guide for learning how to install Windows 2003, because the steps are very similar. The package also includes an e-book that explains every step of the Windows XP Setup and some other Windows XP Basics. Follow the link and free download Windows XP Setup Simulator now.
 http://files3.download3000.com/download/97b4dd2995f69973844915709c00036f/56/29048/winxp_simulator.exe

Thứ Tư, 29 tháng 12, 2010

GDI+ Tutorial for Beginners

GDI+ is next evolution of GDI. Using GDI objects in earlier versions of Visual Studio was a pain. In Visual Studio .NET, Microsoft has taken care of most of the GDI problems and have made it easy to use.
GDI+ resides in System.Drawing.dll assembly. All GDI+ classes are reside in the System.Drawing, System.Text, System.Printing, System.Internal , System.Imaging, System.Drawing2D and System.Design namespaces.
The first class we must discuss is the Graphics class. After the Graphics class, I will discuss other useful GDI+ classes and structures such as Pen, Brush, and Rectangle. The final part of this tutorial are some examples in C#.
The Graphics Class
The Graphics class encapsulates GDI+ drawing surfaces. Before drawing any object (for example circle, or rectangle) we have to create a surface using Graphics class. Generally we use Paint event of a Form to get the reference of the graphics. Another way is to override OnPaint method.
Here is how you get a reference of the Graphics object:
private void form1_Paint(object sender, PaintEventArgs e) { Graphics g = e.Graphics;}
OR:  
protected override void OnPaint(PaintEventArgs e) {Graphics g = e.Graphics;}
Once you have the Graphics reference, you can call any of this class's members to draw various objects. Here are some of Graphics class's methods:

DrawArc Draws an arc from the specified ellipse.
DrawBezier Draws a cubic bezier curve.
DrawBeziers Draws a series of cubic Bezier curves.
DrawClosedCurve Draws a closed curve defined by an array of points.
DrawCurve Draws a curve defined by an array of points.
DrawEllipse Draws an ellipse.
DrawImage Draws an image.
DrawLine Draws a line.
DrawPath Draws the lines and curves defined by a GraphicsPath.
DrawPie Draws the outline of a pie section.
DrawPolygon Draws the outline of a polygon.
DrawRectangle Draws the outline of a rectangle.
DrawString Draws a string.
FillEllipse Fills the interior of an ellipse defined by a bounding rectangle.
FillPath Fills the interior of a path.
FillPie Fills the interior of a pie section.
FillPolygon Fills the interior of a polygon defined by an array of points.
FillRectangle Fills the interior of a rectangle with a Brush.
FillRectangles Fills the interiors of a series of rectangles with a Brush.
FillRegion Fills the interior of a Region.
In .NET, GDI+ functionality resides in the System.Drawing.dll. Before you start using GDI+ classes, you must add reference to the System.Drawing.dll and import System.Drawing namespace. If you are using Visual Studio .NET for your development, you can add reference to the GDI+ library using following:
Creating this project is simple. Create a Windows application and add reference to the System.Drawing.dll using Project->Add Reference menu item. See Figure 1.

Figure 1. Adding reference to System.Drawing.dll
After that add these two lines.
using System.Drawing;
using System.Drawing.Drawing2D;

Note: If you create a Windows application using VS.NET, you only need write only one line.
using System.Drawing.Drawing2D; After that add a Form_Paint event handler using the Properties Window. See Figure 2.

Figure 2. Adding Form_Paint event handler.
Note: You can also add Form paint event handler manually described above.
Graphics Objects
After creating a Graphics object, you can use it draw lines, fill shapes, draw text and so on. The major objects are:

Brush Used to fill enclosed surfaces with patterns,colors, or bitmaps.
Pen Used to draw lines and polygons, including rectangles, arcs, and pies
Font Used to describe the font to be used to render text
Color Used to describe the color used to render a particular object. In GDI+ color can be alpha blended
The Pen Class
A pen draws a line of specified width and style. You always use Pen constructor to create a pen. The constructor initializes a new instance of the Pen class. You can initialize it with a color or brush.
Initializes a new instance of the Pen class with the specified color.
public Pen(Color);
Initializes a new instance of the Pen class with the specified Brush.
public Pen(Brush);
Initializes a new instance of the Pen class with the specified Brush and width.
public Pen(Brush, float);
Initializes a new instance of the Pen class with the specified Color and Width.
public Pen(Color, float);
Here is one example:
Pen pn = new Pen( Color.Blue ); or Pen pn = new Pen( Color.Blue, 100 ); Some of its most commonly used properties are:

Alignment Gets or sets the alignment for objects drawn with this Pen.
Brush Gets or sets the Brush that determines attributes of this Pen.
Color Gets or sets the color of this Pen.
Width Gets or sets the width of this Pen.
The Color Structure
A Color structure represents an ARGB color. Here are ARGB properties of it:

A Gets the alpha component value for this Color.
B Gets the blue component value for this Color.
G Gets the green component value for this Color.
R Gets the red component value for this Color.
You can call the Color members. Each color name (say Blue) is a member of the Color structure. Here is how to use a Color structure:
Pen pn = new Pen( Color.Blue ); The Font Class
The Font class defines a particular format for text such as font type, size, and style attributes. You use font constructor to create a font.
Initializes a new instance of the Font class with the specified attributes.
public Font(string, float); Initializes a new instance of the Font class from the specified existing Font and FontStyle.
public Font(Font, FontStyle);
Where FontStyle is an enumeration and here are its members:

Member Name Description
Bold Bold text.
Italic Italic text.
Regular Normal text.
Strikeout Text with a line through the middle.
Underline Underlined text.
Here is one example:
Graphics g ; Font font = new Font("Times New Roman", 26);
Some of its most commonly used properties are:

Bold Gets a value indicating whether this Font is bold.
FontFamily Gets the FontFamily of this Font.
Height Gets the height of this Font.
Italic Gets a value indicating whether this Font is Italic.
Name Gets the face name of this Font.
Size Gets the size of this Font.
SizeInPoints Gets the size, in points, of this Font.
Strikeout Gets a value indicating whether this Font is strikeout (has a line through it).
Style Gets style information for this Font.
Underline Gets a value indicating whether this Font is underlined.
Unit Gets the unit of measure for this Font.
The Brush Class
The Brush class  is an abstract base class and cannot be instantiated. We always use its derived classes to instantiate a brush object, such as SolidBrush, TextureBrush, RectangleGradientBrush, and LinearGradientBrush.
Here is one example:
LinearGradientBrush lBrush = new LinearGradientBrush(rect, Color.Red, Color.Yellow, LinearGradientMode.BackwardDiagonal);
OR
Brush brsh = new SolidBrush(Color.Red), 40, 40, 140, 140);
The SolidBrush class defines  a brush made up of a single color. Brushes are used to fill graphics shapes such as rectangles, ellipses, pies, polygons, and paths.
The TextureBrush encapsulates a Brush that uses an fills the interior of a shape with an image.
The LinearGradiantBrush encapsulates both two-color gradients and custom multi-color gradients.
The Rectangle Structure
public Rectangle(Point, Size); or public Rectangle(int, int, int, int);
The Rectangle structure is used to draw a rectangle on WinForms. Besides its constructor, the Rectangle structure has following members:

Bottom Gets the y-coordinate of the lower-right corner of the rectangular region defined by this Rectangle.
Height Gets or sets the width of the rectangular region defined by this Rectangle.
IsEmpty Tests whether this Rectangle has a Width or a Height of 0.
Left Gets the x-coordinate of the upper-left corner of the rectangular region defined by this Rectangle.
Location Gets or sets the coordinates of the upper-left corner of the rectangular region represented by this Rectangle.
Right Gets the x-coordinate of the lower-right corner of the rectangular region defined by this Rectangle.
Size Gets or sets the size of this Rectangle.
Top Gets the y-coordinate of the upper-left corner of the rectangular region defined by this Rectangle.
Width Gets or sets the width of the rectangular region defined by this Rectangle.
X Gets or sets the x-coordinate of the upper-left corner of the rectangular region defined by this Rectangle.
Y Gets or sets the y-coordinate of the upper-left corner of the rectangular region defined by this Rectangle.
Its constructor initializes a new instance of the Rectangle class. Here is the definition:
public Rectangle(Point, Size); or public Rectangle(int, int, int, int);
The Point Structure
This structure is similar to the POINT structure in C++. It represents an ordered pair of x and y coordinates that define a point in a two-dimensional plane. The member x represents the x coordinates and y represents the y coordinates of the plane.
Here is how to instantiate a point structure:
Point pt1 = new Point( 30, 30); Point pt2 = new Point( 110, 100);
Some sample Examples:
Drawing a rectangle
You can override OnPaint event of your form to draw an rectangle. The LinearGradientBrush encapsulates a brush and linear gradient.
protected override void OnPaint(PaintEventArgs pe)
{
Graphics g = pe.Graphics ;
Rectangle rect = new Rectangle(50, 30, 100, 100);
LinearGradientBrush lBrush = new LinearGradientBrush(rect, Color.Red, Color.Yellow, LinearGradientMode.BackwardDiagonal); g.FillRectangle(lBrush, rect); }

The output of the above code looks like Figure 3.

Figure 3. Drawing a rectangle.
Drawing an Arc
DrawArc function draws an arc. This function takes four arguments.
First is the Pen. You create a pen by using the Pen class. The Pen constructor takes at least one argument, the color or the brush of the pen. Second argument width of the pen or brush is optional.
Pen pn = new Pen( Color.Blue ); or Pen pn = new Pen( Color.Blue, 100 ); The second argument is a rectangle. You can create a rectangle by using Rectangle structure. The Rectangle constructor takes four int type arguments and they are left and right corners of the rectangle.
Rectangle rect = new Rectangle(50, 50, 200, 100); protected override void OnPaint(PaintEventArgs pe)
{
Graphics g = pe.Graphics ;
Pen pn = new Pen( Color.Blue );
Rectangle rect = new Rectangle(50, 50, 200, 100);g.DrawArc( pn, rect, 12, 84 );}

The output looks like this:

Drawing a Line
DrawLine function of the Graphics class draws a line. It takes three parameters, a pen, and two Point class parameters, starting and ending points. Point class  constructor takes x, y arguments.
protected override void OnPaint(PaintEventArgs pe)
{
Graphics g = pe.Graphics ;
Pen pn = new Pen( Color.Blue );
// Rectangle rect = new Rectangle(50, 50, 200, 100); Point pt1 = new Point( 30, 30);
Point pt2 = new Point( 110, 100); g.DrawLine( pn, pt1, pt2 ); }

The output looks like this:

Drawing an Ellipse
An ellipse( or a circle)  can be drawn by using DrawEllipse method. This method takes only two parameters, Pen and rectangle.
protected override void OnPaint(PaintEventArgs pe)
{
Graphics g = pe.Graphics ;
Pen pn = new Pen( Color.Blue, 100 );
Rectangle rect = new Rectangle(50, 50, 200, 100); g.DrawEllipse( pn, rect ); }

The output looks like this:

The FillPath
Drawing bazier curves is little more complex than other objects.
protected override void OnPaint(PaintEventArgs pe)
{
Graphics g = pe.Graphics;
g.FillRectangle(new SolidBrush(Color.White), ClientRectangle);
GraphicsPath path = new GraphicsPath(new Point[] {
new Point(40, 140), new Point(275, 200),
new Point(105, 225), new Point(190, 300),
new Point(50, 350), new Point(20, 180), },
new byte[] {
(byte)PathPointType.Start,
(byte)PathPointType.Bezier,
(byte)PathPointType.Bezier,
(byte)PathPointType.Bezier,
(byte)PathPointType.Line,
(byte)PathPointType.Line,
});
PathGradientBrush pgb = new PathGradientBrush(path);
pgb.SurroundColors = new Color[] { Color.Green,Color.Yellow,Color.Red, Color.Blue,Color.Orange, Color.White, };g.FillPath(pgb, path);}

The output looks like this:

Drawing Text and Strings
You can override OnPaint event of your form to draw an rectangle. The LinearGradientBrush encapsulates a brush and linear gradient.
protected override void OnPaint(PaintEventArgs pe)
{
Font fnt = new Font("Verdana", 16);
Graphics g = pe.Graphics;
g.DrawString("GDI+ World", fnt, new SolidBrush(Color.Red), 14,10);}

The output looks like this:

References:
MSDN and .NET samples.

Thứ Hai, 27 tháng 12, 2010

NUNIT unit test software for use

NUNIT Use

1: Software and Installation

Free, open source unit testing software.

Installation Just run the installer, you can press all the default settings.

NUNIT: www.nunit.org

NUNIT ADDIN: http://sourceforge.net/projects/nunitaddin/

2: DLL Test

1: Create a new Class Library PROJECT,

2: Add reference to DLL to be tested,

3: Add a reference NUnit.Framework.dll

4: In the beginning of the file by adding: using NUnit.Framework

5: Test the class to be added before: [TestFixture]

6: Test methods to be added before: [Test]

7: Start NUNIT-GUI, new projects, new Class Library PROJECT will join the Executive.

3: Attribute Description

1: TestFixture: before each class to be tested must be added to the Attribute.

【Note】 When the need for a public test class's default constructor (or does not define any constructor)

2: Test: Test the class of each test function to be added before the Attribute.

【Note】 a Test attribute of the method must be public and must return void, and no incoming parameters. If you do not meet these requirements, the Test Runner GUI is not listed among this method, and when in the implementation of the Unit Test will not run this method.

3: TestFixtureSetUp and TestFixtureTearDown Introduction

The two mainly used in TestFixture inside, its role is to provide a set of functions to perform any test run before (TestFixtureSetUP) and after the implementation of the final test (TestFixtureTearDown). Each TestFixture only one TestFixtureSetUp methods and TestFixtureTearDown method. If more than one TestFixtureSetUp and TestFixtureTearDown method, you can compile but not run. Note that one can have a TestFixtureSetUp TestFixture and a SetUp, can also have a TestFixtureTearDown and a TearDown method.

TestFixtureSetUp and TestFixtureTearDown be used in difficult to use SetUp and TearDown methods.

General use SetUp and TearDown attributes.

4: SetUp and Teardown Attributes Description

When writing Unit Tests, sometimes you will need to implement each test method before (or after) to make some preparatory or remedial work. Of course, you can write a private of the method, and then each test method in the beginning or very end of a call to this particular method. Or, you can use the we have to introduce the SetUp and Teardown Attributes to achieve the same purpose. As these two Attributes of the name means, the method will have Setup Attribute in the TextFixture Each test method is executed before the execution was Test Runner, while Teardown Attribute of method in each test method will be executed was performed after Test Runner. In general, Setup Attribute and Teardown Attribute must be used to prepare some of the objects (objects), such as database connection, and so on.

5: Attribute Description

TestFixture: before each class to be tested must be added to the Attribute.

Test: Each test class to be added before the function test of the Attribute.

TestFixtureSetUp: in a test class only once, and run all the test cases before the implementation of the

SetUp: before running each test case execution

ADD: Test Case 1

TearDown: After running each test case execution

SetUp: before running each test case execution

Sub: test case 2

TearDown: After running each test case execution

TestFixtureTearDown: in a test class only once, and after the implementation of all the test cases run

ExpectedException: detection of an exception

[Test]

[ExpectedException (typeof (InvalidOperationException))]

[Test, ExpectedException (typeof (InvalidOperationException))]

Ignore: temporarily ignore some tests

Ignore ("Not ready to test this yet");

4: NUNIT various assertions

Assertions are used to help you determine if a test function is working correctly, usually a test method will be more than assertion, when an assertion fails, the test method will be terminated. Call use Assert or Assertion assertion function.

1: AreEquals (expected, actual [, string message])

Expected is the expected value of the code being tested, actual is the actual value of the tested code, message is an optional message, the value of the two inconsistent reporting errors. Expected and actual can be an object.

For floating-point comparison, using AreEquals (expected, actual, tolerance [, string message]), which, tolerance, said precision that only a relatively small number of 0.01 after the two.

2: IsNull / IsNotNull (object [, string message])

Determine an object is empty / not empty.

3: AreSame (expected, actual [, string message])

Expected and actual judge whether the same object.

4: IsTrue (bool condition [, string message]) / IsFalse

Determine whether the condition is true / false.

5: Fail ([string message])

To test immediately be invalidated.

5: Exception Handling

NUNIT framework can catch any exception, and it reports an error, they do not require user involvement

6: Test preparation

NUNIT use of assertions to be tested to judge the industry code is to return the correct result, the process of preparing test cases, we often need to have a correct value as the basis and testing the code to return the value for comparison. In some cases, may be easier to get to the correct value, but sometimes to get the correct value is not easy. Such as taking data from the database, difficult to set a correct value, in this case, it is best to use some already being tested is correct or commercial module program to bring these tested procedures for the return value or commercial module as the correct value, as a comparison reference.

Thứ Sáu, 24 tháng 12, 2010

"Copy/Paste functionality has been disabled." - lý do!

"Copy/Paste functionality has been disabled." là cái thông báo mà khá nhiều bạn bị dính, lỗi ở đâu?
Virus? hay registry lỗi? máy mình, và máy nhà trường bị cái này, lên mạng tìm xem sao...
Hóa ra là: Sử dụng đĩa JAVA hay bất kì đĩa nào của Aptech đều bị xuất hiện lỗi như thế này:cutepig32: cách khắc phục thì trên mạng đã có bác đưa ra:


Chính vì thế mà mình ko thể khoanh tay đứng nhìn được, đành phải ra tay làm cái AptechKit này. Nó gồm có 2 chức năng chính:
1. Suspend START.EXE - Không cho CD Aptech ngốn tài nguyên CPU nữa.
2. Allow copy/paste - Không bị cái Copy/paste functionality has been disabled đáng ghét nữa.

Nhược điểm của AptechKit 1.0:
- Chỉ copy/paste được text thôi, còn các thành phần khác như bitmap, files,... thì ko copy được. Mình sẽ khắc phục cái này trong phiên bản tiếp.
Mã:
DOWNLOAD:
http://www.mediafire.com/?atymrzadmnt
hoặc: http://files.myopera.com/hieubm/files/AptechKit.rar
---Lưu ý: Để chạy được AptechKit, bạn cần cài Visual C++ 2008 Redistributable Package trước.

Bài đăng phổ biến