Wednesday, October 15, 2008

Asynchronour And Thread Method calls

using System.Threading;
using System.Runtime.Remoting.Messaging;


public partial class Form1 : Form
{
public delegate void ShowMessage();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
ShowMessage myMessage = new ShowMessage(GetInvoked);
myMessage.BeginInvoke(new AsyncCallback(RefreshWindow), null);
Thread myThread = new Thread(GetThreadInvoked);
myThread.Name = "GetInvokedClick";
myThread.IsBackground = true;
myThread.Start();
}
private void GetInvoked()
{
MessageBox.Show("Async Started");
}
private void GetThreadInvoked()
{
MessageBox.Show("Thread Started");
}
private void RefreshWindow(IAsyncResult asResult)
{
AsyncResult result = (AsyncResult)asResult;
ShowMessage msg = (ShowMessage)result.AsyncDelegate;
msg.EndInvoke(asResult);
}
}

Tuesday, September 9, 2008

Resolve ConfigurationSettings.AppSetting is absolete warning

While migrating any .Net application from .Net 2003 to .Net 2005 and above we usally get this warning

This warning exists because this method is obsolete, it has been replaced by System.Configuration!System.Configuration.ConfigurationManager.AppSettings

Steps to resolve this issue
1. Add a project reference to System.Configuration.dll.
2. Replace all ConfigurationSettings.AppSettings with ConfigurationManager.AppSettings.

Further if u still get Configuration file app.config or web.config cannot be read

Follow this steps
1. Click on App.config
2. Change build action propert to "Embedded Resource" to "None"

Tuesday, August 26, 2008

1. ConfigurationManager.Appsettings always returns null

Solution : click on the app.Config file and look at properties window

Setting Build Action to Embedded Resource is causing this problem change this to None this will resolve this problem

Friday, April 25, 2008

How to convert milliseconds in GMT to datetime to local timezone

public static DateTime CovertToLocalDateTime(long value)
{
DateTime local = new DateTime();
try
{
long ticks = value * TimeSpan.TicksPerMillisecond;
DateTime utc = new DateTime(1970, 1, 1);
DateTime dotnetEquivalent = utc.Add(new TimeSpan(ticks));
local = dotnetEquivalent.ToLocalTime();
}
catch (Exception ex)
{ }
return local;
}

Thursday, March 27, 2008

Get local machine's current time zone

System.TimeZone.CurrentTimeZone gives you the machine's current time zone.