3

The following c# code runs OK as a console app on a Win 10 dev box when built for .Net 4.5:

static void Main(string[] args)
    {
        try
        {
            Console.Write(DateTime.Now.ToString());
        }
        catch (Exception Ex)
        {
            Console.Write(Ex.Message);
        }

    }

But when run on a Win 7 box with .Net 4.6.1 it has started crashing showing the following message:

    Unhandled Exception: System.Runtime.InteropServices.SEHException: External compo
    nent has thrown an exception.
       at System.TimeZoneInfo.TransitionTime.Equals(TransitionTime other)
       at System.TimeZoneInfo.CreateAdjustmentRuleFromTimeZoneInformation(RegistryTi
    meZoneInformation timeZoneInformation, DateTime startDate, DateTime endDate, Int
    32 defaultBaseUtcOffset)
       at System.TimeZoneInfo..ctor(TimeZoneInformation zone, Boolean dstDisabled)
       at System.TimeZoneInfo.GetLocalTimeZoneFromWin32Data(TimeZoneInformation time
    ZoneInformation, Boolean dstDisabled)
       at System.TimeZoneInfo.CachedData.GetCurrentOneYearLocal()
       at System.TimeZoneInfo.CachedData.GetOneYearLocalFromUtc(Int32 year)
       at System.TimeZoneInfo.GetDateTimeNowUtcOffsetFromUtc(DateTime time, Boolean&
    isAmbiguousLocalDst)
       at System.DateTime.get_Now()
       at DateCrashTest.Program.Main(String[] args) in c:\Users\Simon\Documents\Visu
    al Studio 2013\Projects\DateCrashTest\DateCrashTest\DateCrashTest\Program.cs:lin
    e 15

I have two questions:

1) Why is the exception not trapped?

2) Why does the program run OK on a different Win7 box with same .Net Framework and in the same TimeZone? I have checked the .Net Framework Install on the problem box and it appears to be OK. The problem has appeared recently - it did not occur a few days ago. Rebooting has not helped.

SimonKravis
  • 553
  • 1
  • 3
  • 24
  • If it was working and stopped working then it may something to do with windows update (if there was an update done). Just thinking outloud... – CodingYoshi Nov 23 '17 at 03:58
  • 1
    This is a problem with that machine. See [SEHException not caught by Try/Catch](https://stackoverflow.com/q/16436597/402022). I suggest to [Use the System File Checker tool to repair missing or corrupted system files](https://support.microsoft.com/en-us/help/929833/use-the-system-file-checker-tool-to-repair-missing-or-corrupted-system) and [Run Diagnostics to Check Your System for Memory Problems](https://technet.microsoft.com/en-us/library/ff700221.aspx). Also make sure the system is updated. – Theraot Nov 23 '17 at 04:06
  • It appears to be a problem while reading the local time zone information from the registry. Can you try changing the machine's time zone to something else, then change back to the correct time zone? Doing so will update the local time zone information it is reading from. – Matt Johnson-Pint Nov 23 '17 at 23:15
  • Tried changing zone to another with same time difference from UTC - no effect. Also, registry entries for HKLM\System\CurrentControlSet\Control\TimeZoneInformation are the same in Win 7 box with no problems as in the one with the fault. – SimonKravis Nov 23 '17 at 23:22
  • Does it occur with any time zone set? Or just a specific one? What does `tzutil /g` give (on the command line)? – Matt Johnson-Pint Nov 23 '17 at 23:33
  • AUS Eastern Standard Time – SimonKravis Nov 23 '17 at 23:40
  • Tried sfc /scannow following Theraot's suggestion. It reported no integrity violations – SimonKravis Nov 23 '17 at 23:41
  • The Win 7 Box on which DateTime.Now worked OK should have had th same updates as the one on which it fails. They are both part of a University network – SimonKravis Nov 23 '17 at 23:42
  • Check that the registry info in `HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones\AUS Eastern Standard Time` are the same on both the good and bad machines. (regex export works well). Check the subkey (Dynamic DST) as well. – Matt Johnson-Pint Nov 23 '17 at 23:47
  • Tried changing Time Zone to Cen Australian Standard time - problem still there – SimonKravis Nov 23 '17 at 23:47
  • Reg Keys for AUS Eastern Std are the same on good and bad machines – SimonKravis Nov 24 '17 at 01:30
  • No memory errors on bad machine either – SimonKravis Nov 24 '17 at 01:44
  • The catch cannot catch this exception, the CLR treats it like a CSE, a corrupted state exception. CSEs generate an instant abort. It certainly fits the "corrupted" label, this method does not do anything dangerous that could easily trigger an exception. Corrupt data in the registry does not explain it. Very hard to guess at, this machine ought to have its disk reformatted or replaced. – Hans Passant Nov 26 '17 at 16:41

1 Answers1

0

The problem is not in your code. Most likely there is something wrong with that particular Windows 7 instance. As others already suggested you could run sfc /scannow, but it could be also corrupted registry or something else.

I would suggest to reinstall the machine as it's the simplest and the fastest solution to this problem.

Dávid Molnár
  • 10,673
  • 7
  • 30
  • 55
  • Re-installed Windows and problem disappeared, but have spent hours rebuilding environment. Found 30 copies of mscorlib.dll on bad machine, 26 on machine with no probs. File size and date seemed the same for all instances. (mscorlib.dll contains TimeZone code). – SimonKravis Nov 28 '17 at 03:54
  • Probably the whole .NET installation was corrupted. – Dávid Molnár Nov 28 '17 at 10:36
  • 1
    .Net integrity checker showed no problems - a Windows mystery – SimonKravis Nov 29 '17 at 02:44