The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 1,861 other subscribers

Archive for the ‘.NET’ Category

C# Code fragments of the week

Posted by jpluimers on 2012/11/06

Boy, was I astonished to see the code fragments below in production apps. It is far more Daily WTF than Coding Horror.

However, it did make debugging the production problem at hand a lot worse than it should be.

First a few short pieces:

        private void method(Word.Application objNewDoc, string stringWithCsvSeparatedDotFileNames)
        {
            char c = char.Parse(",");
            string[] wordAddIns = stringWithCsvSeparatedDotFileNames.ToString().Split(c);
        }

It took me almost a minute to understand what happened here. Especially because the names of parameters actually were pretty meaningless.

                foreach (string sFilename in attachments)
                {
                    Word.Application mailDocument = new Word.Application();

                    string[] filePath = sFilename.Split('\\');

                    string tempDirectory = GetTempDirectoryFromConfigFile();
                    object fileName = tempDirectory + filePath[filePath.Length - 1];

                    File.Copy(sFile, (string)fileName, true);
                    // some code that actually does something with the attachment
                }

It took me more than a few minutes to realize that:

  1. The tempDirectory needs to end with a backslash
  2. mailDocument (not a document, see below), will stay alive when File.Copy(…) throws an exception.
        internal virtual bool Method(/* parameters */, Word.Application objDoc)
        {
            // lots of pre-initialized empty string variables that are used at the very end of the method

            Word.Application objNewDoc;
            if (objDoc != null)
            {
                objNewDoc = objDoc;
            }
            else
            {
                objNewDoc = new Word.Application();
            }

            // lots of Object variables for COM, including:
            Object missing = Missing.Value;
            Object bFalse = false;

            try
            {
                // lots of code that does not use objNewDoc
            }
            catch (IOException IOex)
            {
                objNewDoc.Quit(ref bFalse, ref missing, ref missing);
                objNewDoc = null;
                throw IOex;
            }
            catch (Exception ex)
            {
                objNewDoc.Quit(ref bFalse, ref missing, ref missing);
                objNewDoc = null;
                throw new Exception("Some error message.", ex);
            }
            finally
            {
                // empty finally block
            }

            try
            {
                // actual coder that does use objNewDoc
            }
            catch (Exception ex)
            {
                objNewDoc.Quit(ref bFalse, ref missing, ref missing);
                objNewDoc = null;
                throw new Exception("Some error message.", ex);
            }
            return true;
        }

I rewrote the whole piece into separate methods.

Luckily the person who wrote this got promoted away from programming a few years ago.

–jeroen

Posted in .NET, C#, C# 1.0, C# 2.0, C# 3.0, C# 4.0, C# 5.0, Development, Software Development | Leave a Comment »

How to view build log in VS2010?

Posted by jpluimers on 2012/11/01

Sometimes you search for Visual Studio functionality that you use every couple of years to pin down something nasty: The HTML build log output.

And after searching for a long time, then nasty surprise is: the feature got removed.

Q: (by Andrew MacDonald)

With earlier versions of Visual C++, you could view the build log by ctrl+clicking the link in the output window or opening it directly from the intermediate folder. With VS2010 Beta 1 this doesn’t seem to exist. Have I missed it? There is a .log file written to the solution folder, but it just contains the same things as the output window, and doesn’t show the command lines used. I need this to debug why something isn’t building correctly.

A: (by Brian Tyler)

The old HTML log output option is no longer available – we use the command MSBuild logging instead. What you need to do is go to Tools->Options->Projects and Solutions->Build and Run. At the bottom, change the logging level from Normal to Detailed for either the output window or log file.

This will generate a considerable amount of information about the overall build process – so what I do is then just click in the output window and search for cl.exe, or whatever the name of the tool is you are looking for.

–jeroen

via: How to view build log in VS2010?.

Posted in .NET, Development, Software Development, Visual Studio 2005, Visual Studio 2008, Visual Studio 2010, Visual Studio and tools | Leave a Comment »

.net – WinForms Load vs. Shown events – Stack Overflow

Posted by jpluimers on 2012/10/31

The order of events and what you can do in events is very important in Windows applications.

This includes the WinForms applications – still popular for business applications – the first .NET framework that supported building Windows applications.

WinForms has two important event/method combo’s:

In descendants, you override the methods. In the form designer, you use the events.

Both the methods and events rely on Windows messages to get fired. This means they depends on which message loop is active. And this limits in what you can do during them.

One of the things you should not do in Load or Show is perform a MessageBox, ShowDialog or any other form of message pumping (like in COM).

Hans Passant explains it this way:

Avoid using MessageBox.Show() to debug this [ed: debug Shown/Load behaviour]. It pumps a message loop, disturbing the normal flow of events. The Load event is triggered by Windows sending the WM_SHOWWINDOW message, just before the window becomes visible. There is no Windows notification for “your window is now fully shown”, so the WF designers came up with a trick to generate the Shown event. They use Control.BeginInvoke(), ensuring the OnShown() method gets called as soon as the program goes idle again and re-enters the message loop.

This trick has lots of other uses, particularly when you have to delay the execution of code started by an event. However, in your case it falls apart because you use MessageBox.Show(). Its message loop dispatches the delegate registered with BeginInvoke(), causing the Shown event to run before the window is shown.

Krishnan Sriram explains that if you use proper debug logging (see what Hans wrote), you get this order of events:

  1. Form – Client Size Changed : 8/14/2010 10:40:28 AM
  2. Form – Control Added – button1 : 8/14/2010 10:40:29 AM
  3. Form – Constructor : 8/14/2010 10:40:29 AM
  4. Form – Handle Created : 8/14/2010 10:40:29 AM
  5. Form – Invalidated : 8/14/2010 10:40:29 AM
  6. Form – Form Load event : 8/14/2010 10:40:29 AM
  7. Form – Loaded : 8/14/2010 10:40:29 AM
  8. Form – Create Control : 8/14/2010 10:40:29 AM
  9. Form – OnActivated : 8/14/2010 10:40:29 AM
  10. Form – Shown : 8/14/2010 10:40:29 AM
  11. Form – OnPaint : 8/14/2010 10:40:29 AM
  12. Form – Invalidated : 8/14/2010 10:40:29 AM
  13. Form – OnPaint : 8/14/2010 10:40:29 AM

Finally, Ahmed Said indicates that there can be form size differences in the Load and Shown state:

The Shown event occured after the load event, the main difference is not in the visibility but in state of the form (width,hieght,..etc). I will give you an example to clarify if we create a form with default size 100,200 and set the windowstate = Maximized in the load event the size will be 100,200 but in shown event the size will be your screen size

–jeroen

via: .net – WinForms Load vs. Shown events – Stack Overflow.

Posted in .NET, C#, C# 1.0, C# 2.0, C# 3.0, C# 4.0, C# 5.0, Development, Software Development, WinForms | Leave a Comment »

One or more types required to compile a dynamic expression cannot be found. Are you missing references to Microsoft.CSharp.dll and System.Core.dll (via: C# 4.0 and .Net 3.5 – Stack Overflow)

Posted by jpluimers on 2012/10/25

If you get any of the two errors below while compiling your .NET app, then one of these things happened:

  1. You moved .NET 4 or higher code that makes use of dynamic into an assembly that does not reference the Microsoft.CSharp.dll and System.Core.dll assemblies.
  2. You tried changing the .NET version of a project back to .NET 3.5 or lower.

Note that it is not so much declaring a variable as dynamic, but using that variable.

Predefined type ‘Microsoft.CSharp.RuntimeBinder.Binder’ is not defined or imported

One or more types required to compile a dynamic expression cannot be found. Are you missing references to Microsoft.CSharp.dll and System.Core.dll?

–jeroen

via C# 4.0 and .Net 3.5 – Stack Overflow.

Posted in .NET, .NET 4.5, C#, C# 2.0, C# 3.0, C# 4.0, C# 5.0, Development, Software Development | Leave a Comment »

Dell’s new S2340T 23″ Multi-touch monitor: 10-finger touch at 1920 x 1080 full HD resolution in a 23″ inch form factor

Posted by jpluimers on 2012/10/24

A while ago, I wrote about Some notes on multi-touch and Windows.

Today, Dell introduced the S2340T 23″ monitor with multi touch support, aimed at Windows 8. It surpasses the minimim requirement of 5 fingers:

It also features a multi-position, articulating stand, a 10-point touch and Full HD 1920 x 1080 resolution.

This comes much closer to Microsoft Surface Table. Hopefully that SDK will fully support this monitor.

–jeroen

via: Dell’s new S2340T 23″ Multi-touch monitor brings touch to systems upgrading to Windows 8 – Direct2Dell – Direct2Dell – Dell Community.

Posted in .NET, Development, Microsoft Surface, Software Development | 1 Comment »

Reminder to research this: TFS bindings, .vspscc and .vssscc files

Posted by jpluimers on 2012/10/24

I need to put some time into researching how TFS source control bindings really work, as it is not only based on these extensions:

  • .vssscc (Visual Studio Solution Source Code Control)
  • .vspscc (Visual Studio Project Source Code Control)

Information is also stored in the .sln solution files. These postings should get me going:

–jeroen via: Google search for TFS+vspscc+vssscc

Posted in .NET, Development, Software Development, Source Code Management, TFS (Team Foundation System) | Leave a Comment »

Simple SharePoint CAML Query Tester using jQuery & SPServices –

Posted by jpluimers on 2012/10/23

Interesting SharePoint CAML XML trouble shooting tool:

this script is NOT meant to help you *build* your queries. There are other tools for that. This is purely a testing tool to help you during those “doh” moments.

-jeroen

via: Simple CAML Query Tester using jQuery & SPServices –.

Posted in .NET, Development, JavaScript/ECMAScript, jQuery, Scripting, SharePoint, Software Development, Web Development | Leave a Comment »

Research List: CruiseControl.net (ccnet) links

Posted by jpluimers on 2012/10/18

I’ve been working with some continuous integration setups using CruiseControl.net for a suite of Visual Studio 2010 projects stored on Team Foundation Server 2010.

A few links from my research list:

–jeroen

Posted in .NET, C#, Continuous Integration, CruiseControl.net, Development, Software Development, Source Code Management, TFS (Team Foundation System) | Leave a Comment »

Word Clouds and algorithms to generate them

Posted by jpluimers on 2012/10/17

While preparing for the Category Cloud series of posts, I found a lot of helpful links.

Here they are:

–jeroen

Posted in .NET, ASP.NET, C#, Development, HTML, Software Development, Web Development | Leave a Comment »

How To Backup Bootable USB Drive

Posted by jpluimers on 2012/10/15

I needed to make backups of a couple of maintenance USB drives.

It turned out to be pretty easy: How To Backup Bootable USB Drive describes how to do that based on a small USB Image Tool developed in .NET.

One of the things you can do with this is backup bootable Windows installation media.

Note it is a simple tool, so it backups only same size to same size. For more advanced copy purposes, use something like the professional tools from Acronis.

–jeroen

Posted in .NET, Development, Hardware Interfacing, Power User, Software Development, USB, Windows, Windows 7, Windows 8, Windows Server 2000, Windows Server 2003, Windows Server 2003 R2, Windows Server 2008, Windows Server 2008 R2, Windows Vista, Windows XP | Leave a Comment »