LINQPad Crash

Tuesday, March 2nd, 2010

I found myself using LINQPad more often than creating console applications, so much so I dicided to make the small but worth while investment in the optional “Autocompletion” (Intelisense-like) component. The licence is great because I can have it installed on all three of my PCs without having to buy extra licences.

I was figuring out the limits of the Math.Pow function a few days ago on the laptop when the LINQPad upgrade message appeared, not sure what happened next because LINQPad crashed with the following exception.

System Specification:

  • Windows 7 Home Premium x64
  • .NET v2.0.50727 (+3.0 & 3.5)
  • .NET v4.0.20506
  • VisualStudio 2010 Beta1
System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Form.ShowDialog(IWin32Window owner)
   at LINQPad.Program.ProcessException(Exception ex)
   at LINQPad.Program.Start(String[] args)
   at LINQPad.ProgramStarter.Run(String[] args)
   at LINQPad.Loader.Main(String[] args)

If anyone has any theories as to how this can be fixed I would be very apprecitive if you could post in the comments.

So far I have tried:

  • Reinstalling from the latest (2.10.1) from the LINQPad website.
  • Restarted the computer.
  • Removing LINQPad through Add/Remove Programs.
  • Remove LINQPAD manually.
  • Rename %APPDATA%\LINQPad.
  • Looked for Native Images in C:\Windows\assembly – None there

It seems to me that LINQPad throws some exception, which it’s built in exception handler tries to handle then fails, this probably means that the above stack trace is probably not indicative of what is causing the problem. Not that I think it will make a difference but I am going to try upgrading to Visual Studio 2010 RC tomorrow then at least I wll be able to use LINQPad for .NET 4.

Preventing the PictureBox control from locking files

Sunday, February 28th, 2010

One of our more regular contributors to EVEMon posted on our forums showing that the application was incapable of updating cached files (specifically images), after a bit testing I discovered the following Exception was being thrown when trying to overwrite the file in question:

System.IO.IOException: The process cannot access the file 'path\filename' because it is being used by another process.
   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.File.InternalCopy(String sourceFileName, String destFileName, Boolean overwrite)
   at System.IO.File.Copy(String sourceFileName, String destFileName, Boolean overwrite)
   at EVEMon.Common.FileHelper.OverwriteOrWarnTheUser(String srcFileName, String destFileName) in EVEMon.Common\FileHelper.cs:line 108
   at EVEMon.Common.FileHelper.OverwriteOrWarnTheUser(String destFileName, Func`2 writeContentFunc) in EVEMon.Common\FileHelper.cs:line 82
   at EVEMon.Common.Controls.CharacterPortrait.SavePortraitToCache(Image newImage) in EVEMon.Common\Controls\CharacterPortrait.cs:line 248

After a bit of searching around I discovered a post on StackOverflow identifying that System.Drawing.Bitmap(string filename) would lock the filename until the Bitmap was disposed of. The post presented a solution but no code, A bit of further searching confirmed my expectation that Image.FromFile(string filename) was subject to the same locking behaviour:

The file remains locked until the Image is disposed.

A bit more searching identified another post on StackOverflow which gave me the basic syntax and structure for the code I was going to need to implement this in EVEMon. The final code looks like this:

189
190
191
192
193
194
195
196
MemoryStream stream = new MemoryStream();
 
byte[] imageBytes = File.ReadAllBytes(cacheFileName);
stream.Write(imageBytes, 0, imageBytes.Length);
stream.Position = 0;
 
var image = Image.FromStream(stream);
return image;

It appears that GDI+ will lock any image that is loaded into a control in WinForms and WPF, several comments on StackOverflow and byte.com suggested that even disposing of the control and the FileStream was not a reliable way of being able to write to the file so the above method is seems to be be the best solution all round.

Controling Code Outlining with the Keyboard

Tuesday, November 3rd, 2009

Code outlining is a feature of supported by Visual Studio and many other editors, MSDN has some good documentation for VS2005, VS2008 and VS2010. If I were asked to explain this as briefly as possible, I would probably say:

Code Outlining is the logical partitioning of code in such a way that the user interface, or editor, is able to selectively hide the body of the content (such as a class, struct, enum or method) whilst leaving the signature or some identifying comment visible.

You can see this in action in Visual Studio 2008 with the following Screenshot:

CodeOutliningVS2008

I accidentally turned off Code Outlining today by hitting some keyboard shortcut that I didn’t know how to reverse, this lead me to discover several useful keyboard shortcuts for managing the display of your code from the keyboard.

As it turns out I managed to hit Ctrl-M followed by Ctrl-P (or just P in fact) which maps to Edit.StopOutlining, by default it seems that the Visual C# 2005 mapping scheme doesn’t provide a shortcut to enable Automatic Outlining so instead you can access the command through Edit Menu -> Outlining -> Start Automatic Outlining.

Enabled again, I get to play with code outlining from the keyboard:

  • To toggle (collapse an expanded block or expand a collapsed block) the closest outlined element use Ctrl-M followed by Ctrl-M.
  • To toggle everything use Ctrl-M followed by Ctrl-L (I find little use for this)
  • To collapse to definitions use Ctrl-M followed by Ctrl-O

The last one is the most useful when used in conjunction with Regions as after colapsing to definitions you will get something similar to this:

ColapseToDefinitionsVS2008

You might have noticed in the first screenshot that CodeRush Xpress adds a coloured line between the beginning and end of blocks of code, this is a nice feature if you have long blocks of code, which of course you shouldn’t have.

CodeRushXpressBlockLines

There we go, an errant key stroke can lead to learning and blogging, who would have thought it?

Adjusting Selections in CodeRush Xpress

Friday, October 16th, 2009

I found this function totally accidentally when I knocked my mouse into a key when something interesting popped up on Twitter. I happened to have a variable selected like so:

Variable Selected in Visual Studio

The key that I knocked was the Number Pad “+” key, and it expanded the selection like this:

VariableSelectedPlus1

As any self respecting Systems Administrator would do I wondered if doing exactly the same thing repeatedly would have equal or compound effects.

VariableSelectedAgainAgain

Strangely enough it worked, the selection will continue to expand selecting increasingly larger sections of code. The reverse works as well if you press the Number Pad “-” key the selection will be reduced.

WhatHappened

As I was looking for documentation on this function; I found that you could use the CamelCase select function to start selections off using Ctrl+Shift+Left (or Ctrl+Shift+Right) this will take your selection from the current cursor position to the next upper case character to the left (or right). This can be coupled with the Number Pad “+” to expand your selection from a variable to an expression, to a line then a block. Neat!

Each of these discoveries was announced with a handy little popup in the bottom right hand corner of the viewport telling me what happened, and if relevant what function was suppressed.

I am going to make a concerted effort to spend my lunch break watching some more videos on DevExpress’ training site. I got 10 minutes today and watched what appeared to be footage from a launch event or a conference which was interesting but didn’t really teach me anything specifically.

Find Files in CodeRush Xpress

Monday, October 12th, 2009

As I mentioned in my previous entry I have started using DevExpress’s CodeRush Xpress. It is a free cut down version of CodeRush that I heard about in an episode of .NET Rocks. I have wanted add something to my Visual Studio development experience and I can’t justify the cost of either Resharper or CodeRush at the moment. There has been quite a bit of discussion about Resharper vs. CodeRush and in my experience most people I have spoken to love one and hate the other.

I hope to be able to write a series of posts about some of the features found in CodeRush Xpress which I hope will clarify their purpose and use in my mind and maybe help someone find the function they are looking for.

QuickFileNavigationAbouFirst off “Quick File Navigation”, this is a search function for locating a file by file name, I am finding it increasingly useful when looking through patches as it allows me to very quickly jump to a file name. Particularly as I am trying to move my projects to a one class per file so if I can remember the class name I can find the code file very quickly.

The “Quick File Navigation” dialog is accessible through the keyboard short cut Ctrl+Alt+F. Typing will filter the list box by the name of the file matching anywhere in the filename including the extension.

An additional feature for those who like me use CamelCase in their file names; if you enter your filter terms in capital letters it will search for capitalized words, in order within file names. Thus entering “AW” into the search box will also bring up the AboutWindow.cs in the above solution.

Combining the above with the Ctrl-G keyboard shortcut in Visual Studio 2008 to go to a specific line we can do the following to go to line 162 in ShipLoadoutSelectWindow.cs:

Ctrl+Alt+F  →  S,L,S,W  →  Ctrl+G →  1,6,2

DevExpress have a great training video on their tv.devexpress.com sub site.

Ctrl+Alt+F → S,L,S,W → Ctrl+G → 1,6,2