Showing posts with label NET. Show all posts
Showing posts with label NET. Show all posts

Wednesday, October 29, 2008

Visual Studio 2010 and .NET Framework 4.0 CTP released

Almost one month after Microsoft unveiled the press release, Visual Studio 2010 and .NET Framework 4.0 CTP can be downloaded from here: Microsoft Pre-release Software Visual Studio 2010 and .NET Framework 4.0 Community Technology Preview(CTP), and it is available in VHD format.

Here're some overview:
  • Democratizing Application Lifecycle Management
  • Enabling emerging trends
  • Inspiring developer delight
  • Riding the next generation platform wave.
Pick it up fast, and you could be the next MVP!
(Following image is external link)

Wednesday, October 22, 2008

10 Developer-must-have apps

While so many bloggers have suggested the usable tools to their audience, how could i lag? Although Scott Hanselman have covered most, and it's a mountain high to climb, let me introduce my favourite top 10 developer-must-have apps:

1) Notepad++ (Language: neutral)
when you need it?
a) View Unix files in Windows platform, especially during Java development.

when you open a come-with-JDK Java file using notepad

when you open it using Notepad++, it's well-formatted

b) Need to probe the _ViewState of ASP .NET web page to see how stateless HTTP retains value after post-back, using base64 decode. (you can try it here)

A normal ASP .NET with its HTML source

Copy the content to Notepad++

Decode it to base64, you can see your state retained

c) Determine Byte-Order-Mark of a file, whether it's saved in following formats: UTF-8, UTF-16, UTF-16 (LE), UTF-32 (BE), UTF-32 (LE), UTF-7, UTF-1, UTF-EBCDIC, SCSU, BOCU-1. And it's exactly .Net method GetPreamble trying to do.

Did you see extra three bytes ahead your file content?

d) it's lightweight, unlike Visual Studio IDE, although Noah Coad might beg to differ.
e) Hey! "All I've mentioned above, other editors have the same features as well". Well, simply I like its UI also.

2) Unlocker (Language: neutral)
when you need it?
a) When you want to delete a file which is being viewed/locked by hidden host. That's nothing more annoyed than this.

b) When a file get locked by your app, especially using class File.IO to write it without closing the stream properly.
c) When you try to delete an old ASP .NET version 1.1 web projects in "Inetpub" that's being locked by IIS.

3) Dependency Walker (Language: native)
when you need it?
a) When you need to determine a EXE is a native exe or .NET managed exe. Why, because you can decompile the source accordingly later :)

A .NET exe

A VB6 dll

b) Of course, find the exe/dll dependencies, as the name implies. When you were in the pre-NET era with only Visual C++/Visual Basic 6 & Package Deployment Wizard, there's no such thing called MSI technology, which can help you to determine most of the dependencies for you.

4) Process Explorer (Language: neutral)
when you need it?
a) When you need to find out what process/apps/service is running secretly. You'll never know your employer have planted a window service for software inventory control on your laptop, until you run it.

b) Check the thread state of a stopped thread, probably due to Timeout of Three-Way Handshake in FTP uploading/downloading, when you suppose to have a multi-threaded app run without stopping FTP upload/download.


5) Microsoft Network Monitor (Language: neutral)

when you need it?
a) When you have written a FTP client app running well on uploading files, but FTP server complaint it never receive. You can show this to prove.


b) When you need to sniff network traffic, especially on FTP since all the commands are transferred thru' Cleartext.

Wow, you can see user-name and password!

6) Reflector (Language: .NET)
when you need it?
a) When you need to re-generate the source that lost and you do not have any source control previously, with the aid from plug-in, FileDisassembler


b) When you need to find the difference between the exe/dll you deployed, with the another aid from plug-in, Diff

7) DebugView (Language: native/.NET)
when you need it?
a) When your app only with issue happens intermittently, you need your app to run in verbose mode using Debug.Print without additional tracer running side-by-side, that might affect the performance, or introduce new bug/maintenance issue. This works well for native app too, using Win32.

8) 7Z (Language: neutral)
when you need it?
a) It supports most of the commercial format, yet without asking you to purchase every time it loads, eg. WinZip, Winrar. Furthermore, it able to compress more to save your disk-space.

b) When you need to find out how the zip file is created. Either SharpZipLib or Windows built-in zipping tool; compression method is either DEFLATE, Implode, or Store. You can get the Host OS and CRC as well.

this zip file is compressed using SharpZipLib with DEFLATE method

this zip file is compressed using Store method

this zip file is compressed using Windows built-in zipping tool

9) Microsoft Virtual PC (Language: neutral)
when you need it?
a) When you wish to be beta tester yet you do not want to jeopardize your existing system.
b) When you found virus, and you need to cultivate it in order to trace the originator, so that... counter-hack!
c) When you need to test your web app in different OS. MS has prepared it for you to use it until January, 2009. Flavours available: Windows XP SP3 with IE6 VHD file, Windows XP SP2 with IE7 VHD file, Windows XP SP3 with IE8 Beta 2 VHD file, and Vista Image with IE7 VHD file. (see: Internet Explorer Application Compatibility VPC Image)
d) When you wish to play around with different *-nix OS.


10) Windiff (Language: neutral)
a) Basically for file compare and directory compare. I have used it since development in Visual C++ 6.0. Though it might not user-friendly, eg, cannot compare in separate panes, cannot compare Unicode/UTF-8 files. But it comes with Visual C++ 6.0 and have been with me so long, I feel hard to say goodbye to it.

Sunday, August 31, 2008

Probing thread state in .NET application

In multi-threaded application, some threads could be running fine, others might have problem. While trouble-shooting bug in a multi-threaded application, someone pointed me out that there are 2 set of thread id and thread state can be probed:
1) ThreadState Enumeration in System.Diagnostics (in system.dll)
2) ThreadState Enumeration in System.Threading (in mscorlib.dll)

While the enumerate members can be found in MSDN easily, the associate integer value is a bit tricky. And using Reflector, its values are:


and


While it's easy to use "Thread.CurrentThread" to check the state of the current thread, most of the time we need to monitor all the threads. Furthermore you will get a whole different set of thread id, if using property "Thread.CurrentThread.ManagedThreadId". Thus, classes like "ProcessThreadCollection":

For Each threCurr As ProcessThread In Process.GetCurrentProcess().Threads
Console.WriteLine("ID : " + CStr(threCurr.Id) + " ; State : " + CStr(threCurr.ThreadState))
Next

will be useful to enumerate all the threads in your application, especially you can simply use any other third party tool like Process-Explorer to probe all the thread easily, in real-time:

Tuesday, July 15, 2008

How to capture the output from Console.WriteLine

Sometimes, windows/console application needs to capture the output from console.writeline for debugging purpose. It's useful if the application can run in verbose mode, so that we can know at which point the system crashes. Maybe you might think we can implement ConsoleTraceListener Class in .Net 2.0.

While doing some tutorials on Linq on .Net 3.0, I found the sample have another simpler way to achieve that. Here are the steps:

1) Create a windows application


2) Design a simple GUI. Button represents you business logic and multiline textbox means the trace information.


3) Set the StreamWriter to the Console output, re-capture it with TextWriter will do.

Imports System.IO

Public Class Form1
Private ReadOnly _OutputStreamWriter As StreamWriter = New StreamWriter(New MemoryStream())

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' Capture console output
Dim writer As StreamWriter = _OutputStreamWriter
Dim oldConsoleOut As TextWriter = Console.Out
Console.SetOut(writer)
Dim stream As MemoryStream = CType(writer.BaseStream, MemoryStream)
stream.SetLength(0)

DoSomething
' set to result pane
writer.Flush()
Console.SetOut(oldConsoleOut)
TextBox1.Text += writer.Encoding.GetString(stream.ToArray())

End Sub

Private Sub DoSomething()
Console.WriteLine("Hello world!")
End Sub
End Class

4) Here's the final result:

Wednesday, September 26, 2007

My first .Net app : C# Analog Clock

Finally, my first try on a simple .NET app is done. I created a analog clock widget using C#. I think it's quite challenging for a beginner because it will require simple background of Math, image processing, and 3D graphics programming.

With the mouse hit-test, anti-aliasing graphics and flicker-free double buffering, you couldn't even tell the difference between it and Sidebar gadget. Yet it is in C#, and it reduces the pain to deal with Win32 API directly.


To draw the hour-hand, minute-hand and second-hand needs a little trick.

1) Set the origin of form to center, from left-top (so that following rotation can be taken place)
2) Save the current state
3) Rotate the new graphics objects
4) Draw the second-hand (or any hands) at new origin, new orientation
5) Restore the saved state
6) Rotate the new graphics objects
7) Draw the minute-hand (or any hands left) at new origin, new orientation
8) Reload the identity / Reset the origin and orientation (Restore the saved state seems failed)
9) Rotate the new graphics objects
10) Draw the hour-hand (or any hands left) at new origin, new orientation

Voila!
You can get the source here or get the exe here!

#p/s: thanks for the Fasticon for its free icon provided.

Tuesday, September 11, 2007

SQL CE database upgrade

This blog will talk about SQL CE database upgrade.
Actually, SQL CE database upgrade is briefly discussed in BOL (Book On-Line). And this blog is just to fill the missing link.

Normally, You can create a brand new application to use .NET CF 2.0 & SQL CE 3.0. Then what's the big deal about the SQL CE database upgrade, you might ask. Well,What you actually face in real-life situation is you are .NET CF 1.0 & SQL CE 2.0 application developer. Due to performance issue (SqlCeResultSet Class) or new functionality that only available in SQL CE 3.0, you need to meet the new requirement yet need to maintain the records that already keep in existing SQL CE 2.0 database. You do not want to create a new SQL CE 3.0 database and import back all the records, ain't you? Then, this is where SQL CE database upgrade comes in.

How will one know the database file (*.sdf) is not workable in SQL CE 3.0 ? Simple, open it from MSSQL Management Studio (Sorry, you need full version to run it). If you see this message, clearly you need to upgrade your database.

Now I will show you how to upgrade it within the emulator environment, specifically for OS PPC 2003. It can be done in actual device in the similar way. First, copy all the cabinet files of SQL CE run-time components for SQL CE 2.0 and 3.0 into you emulator, using Windows CE Remote File Viewer. The default location of the SQL CE 2.0 run-time components is C:\Program Files\Microsoft Visual Studio 8\SmartDevices\SDK\SQL Server\Mobile\v2.0\[platform]\[processor] & the default location of the SQL CE 3.0 run-time components is C:\Program Files\Microsoft Visual Studio 8\SmartDevices\SDK\SQL Server\Mobile\v3.0\[platform]\[processor]. You also need a upgrade.exe. This files is located in the same folder with SQL CE 3.0 run-time components.

Then you need to download the Pocket Shell in Windows Mobile Developer Power Toys. It is the PPC version of you desktop command prompt. The default location is in C:\Program Files\Windows Mobile Developer Power Toys\PPC_Command_Shell\arm. Copy the console.dll to emulator's \WINDOWS directory and copy the shell to emulator root folder for less programming later.

So now you should have 5 cabinet files(sqlce20.dev.ppc.wce4.armv4.cab, sqlce20.ppc.wce4.armv4.cab, sqlce30.dev.ENU.ppc.wce4.armv4.cab, sqlce30.dev.ppc.wce4.armv4.cab, sqlce30.rep.ppc.wce4.armv4.cab), 3 executable files (upgrade.exe, cmd.exe & shell.exe from Power Toy) and one SQL CE 2.0 database (oldDB.sdf). Install all the 5 cabinet files & click on the cmd.exe to run it. You will be prompt to enter command.

Type something like: upgrade.exe /s oldDB.sdf /d newDB.sdf

viola! (You need to wait a while if your database is huge)

Try to open it with desktop MSSQL:


If you have missed any cabinet file during installation (eg. you did not install "sqlce30.rep.ppc.wce4.armv4.cab"), you will get this error message:

Try to export the upgrade.log file to analyze:

In this case, you will get:

Connecting to source databaseCreating destination databaseUnable to instantiate SQL CE 3.0 provider
HRESULT: 0x80040154Database upgrade failed