Click here to Skip to main content
15,909,835 members
Home / Discussions / .NET (Core and Framework)
   

.NET (Core and Framework)

 
AnswerRe: SHA-2, is it in .NET? Pin
Paul Conrad14-Sep-05 19:59
professionalPaul Conrad14-Sep-05 19:59 
QuestionGDI Region Union Bug Pin
Steve Knauber2-Sep-05 4:27
Steve Knauber2-Sep-05 4:27 
QuestionApplication Architecture Pin
radasys2-Sep-05 3:51
radasys2-Sep-05 3:51 
AnswerRe: Application Architecture Pin
radasys5-Sep-05 0:38
radasys5-Sep-05 0:38 
Question.Net and HtmlHelp function Pin
daisyliu1-Sep-05 6:34
daisyliu1-Sep-05 6:34 
QuestionTimeZone.CurrentTimeZone not updated when machine's time zone changes. Pin
Matt Casto1-Sep-05 6:17
Matt Casto1-Sep-05 6:17 
AnswerRe: TimeZone.CurrentTimeZone not updated when machine's time zone changes. Pin
Matt Casto2-Sep-05 4:59
Matt Casto2-Sep-05 4:59 
AnswerRe: TimeZone.CurrentTimeZone not updated when machine's time zone changes. Pin
Richard Deeming5-Sep-05 8:17
mveRichard Deeming5-Sep-05 8:17 
The problem is that the CurrentTimeZone property caches the result for the lifetime of the AppDomain, so you won't see any changes until your AppDomain is restarted.

The simplest option is to use reflection to create a new instance of the internal CurrentSystemTimeZone class. The following code will cache the current time zone for 5 minutes, and allow you to manually refresh the time zone as well:
C#
using System;
using System.Reflection;
 
public sealed class CurrentTimeZone
{
    private const int RefreshAfterMinutes = 5;
    private static readonly object _lockMe = new object();
    private static readonly Type _timeZoneType;
    private static readonly ConstructorInfo _timeZoneConstructor;
    private static TimeZone _instance = TimeZone.CurrentTimeZone;
    private static DateTime _instanceCreated = DateTime.UtcNow;
    
    static CurrentTimeZone()
    {
        _timeZoneType = TimeZone.CurrentTimeZone.GetType();
        _timeZoneConstructor = _timeZoneType.GetConstructor(
            BindingFlags.Instance | BindingFlags.NonPublic,
            null, new Type[0], null);
    }
    
    private static TimeZone CreateInstance()
    {
        return (TimeZone)_timeZoneConstructor.Invoke(null);
    }
    
    private static void UpdateIfStale()
    {
        TimeSpan age = _instanceCreated - DateTime.UtcNow;
        if (age.TotalMinutes > RefreshAfterMinutes)
        {
            _instance = CreateInstance();
            _instanceCreated = DateTime.UtcNow;
        }
    }
    
    public static TimeZone Instance
    {
        get
        {
            lock(_lockMe)
            {
                UpdateIfStale();
                return _instance;
            }
        }
    }
    
    public static void Refresh()
    {
        lock(_lockMe)
        {
            _instance = CreateInstance();
            _instanceCreated = DateTime.UtcNow;
        }
    }
}

All you need to do is replace each instance of TimeZone.CurrentTimeZone with CurrentTimeZone.Instance, and you should see the changes within 5 minutes.


"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
GeneralRe: TimeZone.CurrentTimeZone not updated when machine's time zone changes. Pin
Matt Casto5-Sep-05 15:32
Matt Casto5-Sep-05 15:32 
GeneralRe: TimeZone.CurrentTimeZone not updated when machine's time zone changes. Pin
Richard Deeming5-Sep-05 23:50
mveRichard Deeming5-Sep-05 23:50 
GeneralRe: TimeZone.CurrentTimeZone not updated when machine's time zone changes. Pin
Matt Casto6-Sep-05 1:17
Matt Casto6-Sep-05 1:17 
GeneralRe: TimeZone.CurrentTimeZone not updated when machine's time zone changes. Pin
Richard Deeming6-Sep-05 6:57
mveRichard Deeming6-Sep-05 6:57 
Question[security] setting dynamically the proxy.Credentials with the currentIdentity Pin
joaoPaulo1-Sep-05 3:36
joaoPaulo1-Sep-05 3:36 
Questionlook for some options which can pass through firewall Pin
at2000131-Aug-05 16:35
at2000131-Aug-05 16:35 
AnswerRe: look for some option witch can pass through firewall Pin
Andy Brummer31-Aug-05 17:55
sitebuilderAndy Brummer31-Aug-05 17:55 
GeneralRe: look for some option witch can pass through firewall Pin
at2000131-Aug-05 23:52
at2000131-Aug-05 23:52 
GeneralRe: look for some option witch can pass through firewall Pin
Andy Brummer1-Sep-05 3:34
sitebuilderAndy Brummer1-Sep-05 3:34 
QuestionExtracting Frames From The AVI Movie Pin
Nabeel Younus Khan31-Aug-05 11:04
Nabeel Younus Khan31-Aug-05 11:04 
QuestionWindows Service arguments Pin
vSoares31-Aug-05 5:44
professionalvSoares31-Aug-05 5:44 
QuestionPicture Box control for .NET PocketPC application Pin
Sevugan31-Aug-05 5:13
Sevugan31-Aug-05 5:13 
AnswerRe: Picture Box control for .NET PocketPC application Pin
Terry Dong31-Aug-05 16:15
Terry Dong31-Aug-05 16:15 
Questiontapi problem Pin
iram130-Aug-05 18:45
iram130-Aug-05 18:45 
Question.Net and Memory Resources Pin
Stanciu Vlad30-Aug-05 10:49
Stanciu Vlad30-Aug-05 10:49 
AnswerRe: .Net and Memory Resources Pin
Dave Kreskowiak30-Aug-05 12:04
mveDave Kreskowiak30-Aug-05 12:04 
GeneralRe: .Net and Memory Resources Pin
Stanciu Vlad30-Aug-05 19:40
Stanciu Vlad30-Aug-05 19:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.