Dear All,
I have written a windows service which will hit the database at a
regular intervals, and display a popup on the desktop if a new record
is inserted into the table. But it seems to be not working.
for testing i implemented the code in windows form and my timer is
calling the function which hit the DB at regular intervals. This is
not happening in the windows service. Can any one pls help me out
with this as it is very urgent.
Below is the code i wrote in windows service. Any kind of help on
this is well appreciated.
public class Service1 : System.ServiceProcess.ServiceBase
{
protected Timer timer;
private static int initalCnt = 0;
private static int cnt = 0;
public Service1()
{
this.ServiceName = "ForumService";
timer = new Timer();
timer.Interval = 10000;
timer.Enabled = true;
timer.Elapsed += new ElapsedEventHandler(OnTimer);
timer.Start();
}
static void Main()
{
System.ServiceProcess.ServiceBase[] ServicesToRun;
ServicesToRun = new System.ServiceProcess.ServiceBase
[] { new Service1() };
System.ServiceProcess.ServiceBase.Run(ServicesToRun);
}
protected override void OnStart(string[] args)
{
if(Convert.ToInt32(Process.GetProcessesByName
("SysTray").Length) <= 0)
{
string arg = ConfigurationSettings.AppSettings
["url"].Trim() ;
System.Diagnostics.Process.Start
(@ConfigurationSettings.AppSettings["TrayExe"].ToString(),arg);
timer.Enabled = true;
}
}
protected void OnTimer(Object source, ElapsedEventArgs e)
{
string strQuery = string.Empty;
string strUserName =
ConfigurationSettings.AppSettings["username"].Trim().ToString();
if(strUserName.Length > 0)
strQuery = "select p.postid from
posts p,forums f,users u where postdate = (select max(postdate) from
posts) and p.forumid = f.forumid and f.forumgroupid = u.role and
u.username = '" + strUserName + "' and postlevel =1";
else
//strQuery = "select p.postid from
posts p,forums f,users u where postdate = (select max(postdate) from
posts) and p.forumid = f.forumid and f.forumgroupid = u.role and
and postlevel =1";
strQuery = "select count(*) from
posts";
string strCon =
ConfigurationSettings.AppSettings["DatabasePath"].ToString();
SqlConnection con = new SqlConnection
(@strCon);
SqlCommand cmd = new SqlCommand(strQuery,con);
try
{
con.Open();
cnt = Convert.ToInt32
(cmd.ExecuteScalar());
if(initalCnt != cnt && cnt != 0)
{
string arg =
ConfigurationSettings.AppSettings["url"].ToString()+ "/AddPost.aspx?
PostID=" + Convert.ToString(cmd.ExecuteScalar());
System.Diagnostics.Process.Start
(@ConfigurationSettings.AppSettings["TrayExe"].ToString(),arg);
initalCnt = cnt;
}
}
catch(Exception ex)
{
this.LogEvent(ex.Message);
}
finally
{
con.Close();
}
}
Thanks in Advance
Avinash
------------------------ Yahoo! Groups Sponsor --------------------~-->
Get Bzzzy! (real tools to help you find a job). Welcome to the Sweet Life.
http://us.click.yahoo.com/A77XvD/vlQLAA/TtwFAA/saFolB/TM
--------------------------------------------------------------------~->
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/AspNetAnyQuestionIsOk/
<*> To unsubscribe from this group, send an email to:
[EMAIL PROTECTED]
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/