2009-06-05

Slow sqlserver express

I got informed of something somewhat strange the other day.  Microsoft SQLServer Express runs (connects) faster if you have a query window to the database already.

Like this:

Have a freshly booted machine with only Visual studio running and your web application of choice.  The time for a refresh of you web page might take 5 seconds.

Now start Management Studio and open a query window.  Let it stay open.
Refresh your page and you are down to a second.

The figures might of course change but they are what I have on a dual core 2GHz 4MB machine for a project of size X and database of size Y.

----

I haven't checked if the same thing happens with another script editor like for instance AnySQLMaestro but guess it does.  I guess it has to do with connections - as long as there is a connection to SQLExpress, it doesn't close.  If so, it corresponds with the product's raison d'ĂȘtre, a database for applications.

----

I have noticed a correspondence between debug-start-delay and quality of code.  I say that every second wasted waiting on debug start makes the developer unwilling to test more thoroughly and leads to more bugs in the product.

----

Another trick to make web debug start faster here.

2009-04-17

Timer and GUI

One cannot use the System.Timers.Timer to update the GUI, instead one gets a cross-thread exception. The GUI can't be updated from another thread.

A nice trick to solve this is to use a method that is sensitive for if the call comes from another thread than the GUI is running on.



private delegate void SetTextDelegate(Label label, string text);

private void SetText(Label label, string text)
{
if (this.InvokeRequired)
{
IAsyncResult res = BeginInvoke(new SetTextDelegate(SetText), label, text);
EndInvoke(res);
}
label.Text = text;
}


Honor those who should.

2009-04-11

Attach to a process even faster

In an earlier article I mentioned a time saver for web projects where I recommended attaching to the process instead of restarting every time.

Today I invested time in creating a macro for attaching to the Nunit process.  It was easy.

- Instead of learning all commands just record a new macro (menu->tools->macro->record temporarymacro) and connect to the process of choice.

- Then open the macro explorer (menu->tools->menu->macro explorer) which opens a toolbox.  There is a (new) module called RecordingModule.  Open this.

A new IDE opens with something like:




Sub AttachToNUnitProcess()
' MsgBox("AttachToNUnitProcess.Start")
Try
Dim dbg2 As EnvDTE80.Debugger2 = DTE.Debugger
Dim trans As EnvDTE80.Transport = dbg2.Transports.Item("Default")
Dim dbgeng(1) As EnvDTE80.Engine
dbgeng(0) = trans.Engines.Item("Managed") ' Can be "Native".
Dim proc2 As EnvDTE80.Process2 = dbg2.GetProcesses(trans, "MDG-VILTERSTEN").Item("nunit.exe")
proc2.Attach2(dbgeng)
Catch ex As System.Exception
MsgBox(ex.Message)
End Try
' MsgBox("AttachToNUnitProcess.Slut")
End Sub

- Testrun your macro in Visualstudio just to make sure it runs properly.

- In the macro editor, create a new Module and copy the code you just created.  Rename module and method.  Save.

- Back in Visualstudio, Macro explorer, your new module should be visible.  "Should" - one might have to restart Visualstudio or the toolbox or something.  It should be runnable directly from the Macro explorer toolbox.

- Create a new menu (menu->tools->customize ...) and attach your macro.  You find it under "Categories/Macros", just drag it to your new menu or the menu bar.

Now attaching to a process is just a click or keystroke away.

Some more info is found here: http://msdn.microsoft.com/en-us/library/hdf2d1z8(VS.80).aspx.

Update: I debugged a web app for a while yesterday and Wow! - what a difference between 3 clicks and 1 click, or 3 keystrokes and 1 keystroke.  Besides being simpler it also connected faster when the GUI didn't have to render and didn't have to wait for me.  Why haven't I done this years ago?  What else is there I should have done years ago?

Update: I created a new menu and items for every macro I use.  So Fast to use!
There is something that bothers me though.  Some menues dissappear after I restart VSNet, I don't know why.

The macros are easy to start also without having created a menu for them.  Just alt-F8 and then arrow keys to find the right macro and start it with return.

Macro for attaching to a process

When debugging aspnet solutions a lot of time is wasted on restarting the debugged application.  A real time saver is to connect to the process to avoid restarting.

This is done like so:

- When a new bug is found don't stop the web browser, instead detach from the process (menu->detach->detach all).
- Update the code to correct the bug.
- Set a breakpoint.
- Connect (menu->attach to process->[find the process, it is called something like iis or aspnet]->attach) to the process.
- Reload the web browser.

There are some caveats.  Like when using Webform the viewstate is tightly connected to the controls on the form so it is not always possible to connect.

But for the most times this is a great time saver.

2009-03-22

truefalse

Personally I write xml comments on all my methods even though I am a fan of method names  enough describing that comments really are unnecessary.  By descriptive method names I mean that others also understand them.

In a project I stumbled upon this method:

private void ShowHideControl( bool truefalse ){...

Since I had the source code it didn't take much effort to find out what it did.

But a question remains - how do I present the problem for the original programmer without being rude?

Avoid code ownership

I heard a thing the other day I haven't heard for a long time.  "That is not my code"

I believe no code and all code in your current project is Yours.  There is nothing like finding a bug and leaving it be.  If you can't correct it, flag it.  Either in a bug tracking system, to the person currently working with that part or to wherever your project stores possible riscs.

This my code-your code mentality mentioned above lead to the bug being forgotten.
That is not considered good.

LINQ examples

Linq is sooo good.

The next time you loop a list or array to find a value or two; consider spending 15 minutes to learn some Linq; it is well invested.  The syntax is somewhat like SQL but more straight forward.  For instance the keywords are written "the normal" way.  First what you have (from), then the filtering (where) and finally what you want (select).

var query =
    from c in myCustomerList
    where c.Age < 42
    select c;

If you are looping two lists and compare them to get a new list, you should really look into Linq.  It will save you lots of time.

I found an old site at MSDN with some good examples here: http://msdn.microsoft.com/en-us/vcsharp/aa336746.aspx

2009-03-20

Time estimation and business objects, CRUDSLM

A small rule I have while estimating time or looking into a system is to run CRUDSLM against all business objects and possibly all database tables.

I make sure that every business object has one tick in each C, R, U and D.  This is the normal case and by doing this I guard myself against forgetting something.  There are times when things aren't updated and even not deleted but then I know this and can argue for it.
It is a fast exercise and I usually run it only once against every business object and once against every database table.

For time estimation I extend the CRUD with SLM where the letters are Search, List and Manipulate respectively.  Many times, especially with the main business objects, they are Listed and Searched.  Then for reports, imports and exports they have to be Manipulated too.

I have many times found out that Delete is missing.

A tip for writing Xml comments in Csharp

There is something called xml comments in the dotnet world.

It looks something like

/// <summary>
///
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public User FindUser( string name )
{...


The trick here is to write the comments on the very first line. Like:

/// <summary>This method finds and returns a User by its name.
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public User FindUser( string name )
{...


When the code is folded it then looks like

/// <summary>This method finds and returns a User by its name. ...
public User FindUser( string name )...


Visual Studio "suggests" writing the comments on line two, like

/// <summary>
/// This method finds and returns a User by its name.
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public User FindUser( string name )
{...


but then after folding it looks like

/// <summary> ...
public User FindUser( string name )...


(this is not a problem in vbnet)

2009-03-01

Visual Studio suddenly refuses to show forms in the designer

Short story:

Totally wierd.  One day VSNet of my colleague refused to open a form.

The solution was to run "C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\devenv.exe" /ResetSkipPkgs

Long story:

VSNet 2008 suddenly refused to open a form in a VBNet project with the message

---------------------------
Microsoft Visual Studio
---------------------------
There is no editor available for 'C:\Documents and Settings\myusername\Local Settings\Application Data\Temporary Projects\WindowsApplication1\Form1.vb'.
Make sure the application for the file type (.vb) is installed.
---------------------------
OK  
---------------------------

(How to copy error message dialogues.)

Googling didn't give much.

Repair did nothing.  It stopped after a "...has encountered a problem..." with Ok as only choice.
Uninstallation din't work; the same error as above.
Uninstalled through a tool found here.  Reinstalled VSNet2008 and Service Pack 1.  The "total uninstallation" was a fraud, the old projects still populated the MRU list :-(  And the problem remained.

My colleague created a new winform project in C# where the problem was the same but the error message different.

---------------------------
Microsoft Visual Studio
---------------------------
Project 'WindowsFormsApplication1' could not be opened because the Microsoft Visual C# 2008 compiler could not be created. QueryService for '{74946829-37A0-11D2-A273-00C04F8EF4FF}' failed.
---------------------------
OK  
---------------------------

This gave us more Google fodder to find more switches here.

After running "C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\devenv.exe" /ResetSkipPkgs the sky was sunny again.

This took the better part of a day.

Why didn't uninstall all remove everything.  Who do Microsoft think they are?  Adobe?