Thursday, April 30, 2009

Set ProgressBar properties in a Thread

delegate void SetControlValueCallback(Control oControl, string propName, object propValue);
private void SetControlPropertyValue(Control oControl, string propName, object propValue)
{
if (oControl.InvokeRequired)
{
SetControlValueCallback d = new SetControlValueCallback(SetControlPropertyValue);
oControl.Invoke(d, new object[] { oControl, propName, propValue });
}
else
{
Type t = oControl.GetType();
PropertyInfo[] props = t.GetProperties();
foreach (PropertyInfo p in props)
{
if (p.Name.ToUpper() == propName.ToUpper())
{
p.SetValue(oControl, propValue, null);
}
}
}
}


Thread thread = new Thread(new ThreadStart(Generate));
thread.Name = "Generate";
thread.IsBackground = true;
thread.Start();


The control properties like progressbar properties can be set in Generate Method


SetControlPropertyValue(progressBar1, "Visible", true);
SetControlPropertyValue(progressBar1, "Minimum", 0);
SetControlPropertyValue(progressBar1, "Maximum", dtCompareSets.Rows.Count);

Tuesday, April 28, 2009

Authenticate the logged on windows user

string userId = Environment.UserName;

public class Authenticate
{
[DllImport("SenaInet.dll")]
static extern int getAuthToken (string tokentype, StringBuilder tokenbuf,ref long tokenbuflen, StringBuilder idbuf, ref long idbuflen);

public static string UserToken()
{
String tokenType = "ldap";
String userid;
String token;
int retcode = 0;
long tbuflen = 16000;
StringBuilder tbuf = new StringBuilder (Convert.ToInt32 (tbuflen));
long ibuflen = 16000;
StringBuilder ibuf = new StringBuilder (Convert.ToInt32 (ibuflen));
retcode = getAuthToken(tokenType,tbuf,ref tbuflen,ibuf,ref ibuflen);
token = tbuf.ToString();
return token;
}
}

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.