Home

error message "handle is invalid" using program mode.

I have tried sample code that runs fine in visual studio and command line compilation but I get the above error when using linqpad. The samples in linqpad run fine, why can't I paste sample code from msdn?

here is an example:

// This example demonstrates the
// Console.CursorLeft and
// Console.CursorTop properties, and the
// Console.SetCursorPosition and
// Console.Clear methods.
using System;

class Sample
{
protected static int origRow;
protected static int origCol;

protected static void WriteAt(string s, int x, int y)
{
try
{
Console.SetCursorPosition(origCol+x, origRow+y);
Console.Write(s);
}
catch (ArgumentOutOfRangeException e)
{
Console.Clear();
Console.WriteLine(e.Message);
}
}

public static void Main()
{
// Clear the screen, then save the top and left coordinates.
Console.Clear();
origRow = Console.CursorTop;
origCol = Console.CursorLeft;

// Draw the left side of a 5x5 rectangle, from top to bottom.
WriteAt("+", 0, 0);
WriteAt("|", 0, 1);
WriteAt("|", 0, 2);
WriteAt("|", 0, 3);
WriteAt("+", 0, 4);

// Draw the bottom side, from left to right.
WriteAt("-", 1, 4); // shortcut: WriteAt("---", 1, 4)
WriteAt("-", 2, 4); // ...
WriteAt("-", 3, 4); // ...
WriteAt("+", 4, 4);

// Draw the right side, from bottom to top.
WriteAt("|", 4, 3);
WriteAt("|", 4, 2);
WriteAt("|", 4, 1);
WriteAt("+", 4, 0);

// Draw the top side, from right to left.
WriteAt("-", 3, 0); // shortcut: WriteAt("---", 1, 0)
WriteAt("-", 2, 0); // ...
WriteAt("-", 1, 0); // ...
//
WriteAt("All done!", 0, 6);
Console.WriteLine();
}
}
/*
This example produces the following results:

+---+
| |
| |
| |
+---+

All done!

*/

Comments

  • You are trying to access the console in a windows GUI application which won't work.

    Try the following instead:
    - Set the query type to C# Program
    - Paste the code into a LINQPad query
    - Remove the 'using System;' directive (see this line is marked invalid)
    - Save the file to disk
    - Run the file with lprun.exe in a command line prompt window (lprun.exe script.linq)

    Result:
    image
Sign In or Register to comment.