Tuesday, April 27, 2010

How to write callbacks (delegate) in C# and .NET

Copy from:
http://www.myelin.co.nz/notes/callbacks/cs-delegates.html

http://msdn.microsoft.com/en-us/library/Bb985784

http://msdn.microsoft.com/en-us/magazine/cc301810.aspx

If you're used to function pointers in C, a delegate is basically a pair of pointers rolled into one:
  • A pointer to an object (optional)
  • A pointer to a method of that object
That means a single delegate passes all the information needed to locate a function in your program, whether it's a static method or associated with an object.
You define them like this in C#:
public delegate void FooCallbackType( int a, int b, int c );
When you want to use them, you make delegate out of the function you want to call:
class CMyClass
{
 public void FunctionToCall( int a, int b, int c )
 {
  // This is the callback
 }

 public static void Foo()
 {
  FooCallbackType myDelegate = new FooCallbackType(
   this.FunctionToCall );
  // Now you can pass that to the function
  // that needs to call you back.
 }
}
If you want to make a delegate to point to a static method, it just looks the same:
class CMyClassWithStaticCallback
{
 public static void StaticFunctionToCall( int a, int b, int c )
 {
  // This is the callback
 }

 public static void Foo()
 {
  FooCallbackType myDelegate = new FooCallbackType(
   CMyClass.StaticFunctionToCall );
 }
}
All in all, they do the same thing as interface-based callbacks in C++, but cause a bit less trouble because you don't need to worry about naming your functions or making helper objects, and you can make delegates out of any method. They're more flexible.

Monday, April 26, 2010

Solution for WinCE Emulator 5.0 Save State Bug

Solution for WinCE Emulator 5.0 Save State Bug

WinCE 5.0 has a bug that it can't load the saved state.

Solution from http://wincetips.blogspot.com/ :


Connecting the CE 5.0 emulator to VS2005

Title: Connecting the CE 5.0 emulator to VS2005

From: Lewis Williams

Email (Optional): lewisw#tesco.net

Added: 3/10/2007 5:32:12 AM

Message: Connecting the CE 5.0 Emulator to VS2005

1. This uses the network method and saves the emulator state. It does not use activesync, communications ports or a null modem cable.

Start the emulator using a shortcut command that is something like this:

"C:\Program Files\Windows CE 5.0 Emulator\Emulator_500.exe" nk.cem
/video 640x480x16
/Ethernet virtualswitch
/sharedfolder "C:\CE5SharedFolder"

The shared folder appears on the emulator as \My Device\Storage Card. Using the shared folder, copy the following files to the \My Device\Windows\ folder on the emulator. These files are located on the host at \Program Files\Common Files\Microsoft Shared\CoreCon\1.0\Target\wce400\x86, or similar

Clientshutdown.exe
ConmanClient2.exe
CMaccept.exe
eDbgTL.dll
TcpConnectionA.dll

2. Select Emulator -> Start Menu -> run -> \Windows\conmanclient2.exe.

3. Get the IP address of the emulator by double-clicking on the T networking symbol bottom left. If it has no ip address try installing Microsoft Loopback Adapter on the host, check for Virtual Machine Network Services, or other host networking hacks. (This is the difficult bit).

4. To check that the emulator is responding, on the host type Ping at a DOS prompt.

5. To get "Save State" working on the emulator, shut down the emulator using the "Save State" option. Then navigate to Host -> My Documents -> My Virtual Machines

6. The saved state is in the folder that is named with a curly brackets string similar to {06A8A448-EB8B-4E0B-8A88-451412A10C66} say, and known as a GUID. Attempt to rename this folder so that you can highlight and copy the GUID string itself (not the folder).

7. Then add an option, which is similar to /vmid {06A8A448-EB8B-4E0B-8A88-451412A10C66}, to the emulator shortcut command above.

8. The shortcut should now start the emulator from its saved state. It is a good idea to back up the saved state folder.

9. On the host select Visual Studio 2005 -> Tools -> Options -> Device Tools -> Devices

10. Then select Windows CE 5.0 Device -> Properties -> Configure

11. In the "Configure TCP/IP Transport" dialog box, select "Use specific IP address", and then type the emulator IP address you found above.

12. Close the dialog boxes.

13. Select Emulator -> Start -> run -> \Windows\cMaccept.exe and connect to the emulator from VS2005 within three minutes.

14. Run your application from Start Debugging in VS2005 and VS2005 should deploy the two cab files nectcfv2.wce5.x86.cab and system_SR_enu.cab first (this may take some time), and then your application.

15. Close your application in the emulator (I've had trouble using the Stop button on the host).

16. Shut down the emulator using the "Save State" option.

17. You may need to re-run cMaccept each time you restart the emulator or VS2005, but the cab files should not need to deploy again, and the emulator ip address should remain the same.

18. To avoid cMaccept navigate host -> programs -> Microsoft Visual Studio 2005 -> Visual Studio Remote Tools -> Remote Registry Editor

19. In the "Select a Windows Device" dialog box that appears highlight the "Windows CE 5.0 Device" option

20. In the emulator run cMaccept and then immediately click OK in the Remote Registry Editor

21. Highlight Windows CE 5.0 -> HKLM -> System

22. Right click in the right hand pane and select New DWORD value.

23. In the name field type (exactly and without the quotes) "CoreConOverrideSecurity" and set its value to 1

24. Close the editor. Shut down the emulator with Save State.

Monday, April 12, 2010

Microsoft NumericUpDown Control Bug - It does not validate max/min/decimal value

Detail:

If you define:
1. max = 10000
2. min = 0
3. decimalplaces=4

If user input 99.00004 and leave focus, the GUI will update the displayed value to 99.0000.

However, if you use numericUpDown.Value property to get the user typed value, you will get 99.00004 instead of 99.0000.

If your program prompts the message saying that user input was wrong, then the user will be confused as the GUI value was correct!!!

The bug was recorded by MS in 2007:

http://support.microsoft.com/?scid=kb%3Ben-us%3B814347&x=7&y=2


The bug was still not fixed in .NET Framework 3.5 (2009):
http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/f45c16a5-8ddd-41e5-a0b8-5bec938ba8d7/

Monday, April 05, 2010

微軟的免費午餐 - Microsoft Chart Control for .NET framework 3.5

Sample and Tutorials
http://terudio.blogspot.com/2009/02/microsoft-chart-control-for-not-net.html

http://www.dotblogs.com.tw/chou/archive/2009/10/03/10902.aspx

http://www.dotblogs.com.tw/jeff-yeh/archive/2009/09/24/10760.aspx

Download:
Microsoft Chart Controls Add-on for Microsoft Visual Studio 2008
http://www.microsoft.com/downloads/details.aspx?FamilyId=1D69CE13-E1E5-4315-825C-F14D33A303E9&displaylang=en

Microsoft Chart Controls for Microsoft .NET Framework 3.5
http://www.microsoft.com/downloads/details.aspx?displaylang=zh-tw&FamilyID=130f7986-bf49-4fe5-9ca8-910ae6ea442c

Microsoft Chart Controls for Microsoft .NET Framework 3.5 語言套件
http://www.microsoft.com/downloads/details.aspx?displaylang=zh-tw&FamilyID=581ff4e3-749f-4454-a5e3-de4c463143bd

*Note: Microsoft Chart Controls will be included in .NET Framework 4.0 and Visual Studio 2010