public class CSMain
{
private CSMain() { }
static CSMain()
{
mUID = "2c81068a-7828-4769-8112-36754b67cdc5";
}
[STAThread]
static void Main()
{
try
{
bool firstInstance;
mSingleInstanceLock = new Mutex(true, mUID, out firstInstance);
if (firstInstance == true)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new frmLogin());
}
}
catch (Exception ex)
{
string err = ex.ToString();
}
}
private static string mUID;
private static Mutex mSingleInstanceLock;
}
Search This Blog
Monday, October 14, 2013
Find the idle time of windows application
Find how much time application idle in windows application
public partial class Form1 : Form,IMessageFilter
{
Timer timer;
private DateTime _wentIdle;
private int _idleTicks;
public Form1()
{
InitializeComponent();
Application.AddMessageFilter(this);
timer = new Timer();
timer.Interval = 1000;
timer.Tick += timer_Tick;
timer.Start();
}
void timer_Tick(object sender, EventArgs e)
{
TimeSpan diff = DateTime.Now - _wentIdle;
if (diff.TotalSeconds >= Settings.Default.IdleTimeout_Sec)
{
label1.Text = "Idle From - " + _wentIdle;
}
if (++_idleTicks >= Settings.Default.IdleTimeout_Sec)
{
label1.Text = "Idle From - " + _idleTicks;
}
if (_idleTicks > 100)
{ Application.Exit(); }
}
private void Form1_Load(object sender, EventArgs e)
{
Application.Idle += Application_Idle;
}
void Application_Idle(object sender, EventArgs e)
{
_wentIdle = DateTime.Now;
}
public bool PreFilterMessage(ref Message m)
{
if (isUserInput(m))
{
_wentIdle = DateTime.MaxValue;
_idleTicks = 0;
label1.Text = "We Are NOT idle!";
}
return false;
}
private bool isUserInput(Message m)
{
if (m.Msg == 0x200) { return true; }
if (m.Msg == 0x020A) { return true; }
if (m.Msg == 0x100) { return true; }
if (m.Msg == 0x101) { return true; }
return false;
}
private void button1_Click(object sender, EventArgs e)
{
Form2 f = new Form2();
f.Show();
}
}
public partial class Form1 : Form,IMessageFilter
{
Timer timer;
private DateTime _wentIdle;
private int _idleTicks;
public Form1()
{
InitializeComponent();
Application.AddMessageFilter(this);
timer = new Timer();
timer.Interval = 1000;
timer.Tick += timer_Tick;
timer.Start();
}
void timer_Tick(object sender, EventArgs e)
{
TimeSpan diff = DateTime.Now - _wentIdle;
if (diff.TotalSeconds >= Settings.Default.IdleTimeout_Sec)
{
label1.Text = "Idle From - " + _wentIdle;
}
if (++_idleTicks >= Settings.Default.IdleTimeout_Sec)
{
label1.Text = "Idle From - " + _idleTicks;
}
if (_idleTicks > 100)
{ Application.Exit(); }
}
private void Form1_Load(object sender, EventArgs e)
{
Application.Idle += Application_Idle;
}
void Application_Idle(object sender, EventArgs e)
{
_wentIdle = DateTime.Now;
}
public bool PreFilterMessage(ref Message m)
{
if (isUserInput(m))
{
_wentIdle = DateTime.MaxValue;
_idleTicks = 0;
label1.Text = "We Are NOT idle!";
}
return false;
}
private bool isUserInput(Message m)
{
if (m.Msg == 0x200) { return true; }
if (m.Msg == 0x020A) { return true; }
if (m.Msg == 0x100) { return true; }
if (m.Msg == 0x101) { return true; }
return false;
}
private void button1_Click(object sender, EventArgs e)
{
Form2 f = new Form2();
f.Show();
}
}
Using statement to connect to database
use using keyword to connect to database.
using System.Data;
using System.Data.SqlClient;
String conString = "Data Source=SERVER;UID=user;PWD=passsword Integrated Security=True;";
using (SqlConnection sqlCon = new SqlConnection (conString))
{
sqlCon.Open();
DataTable tblDatabases = sqlCon.GetSchema ("Databases");
sqlCon.Close();
foreach (DataRow row in tblDatabases.Rows)
{
Console.WriteLine ("Database: " + row["database_name"]);
}
}
using System.Data;
using System.Data.SqlClient;
String conString = "Data Source=SERVER;UID=user;PWD=passsword Integrated Security=True;";
using (SqlConnection sqlCon = new SqlConnection (conString))
{
sqlCon.Open();
DataTable tblDatabases = sqlCon.GetSchema ("Databases");
sqlCon.Close();
foreach (DataRow row in tblDatabases.Rows)
{
Console.WriteLine ("Database: " + row["database_name"]);
}
}
Subscribe to:
Posts (Atom)