problem with wmi

G

Guest

I have got a problem with WMI's Win32_LocalTime:
here is the msdn reference:
http://msdn.microsoft.com/library/en-us/wmisdk/wmi/win32_localtime.asp


I'D like to have a timer which pass me an event every thursday at 7 am.
I have got a method to produce a querystring to configure this timer
(according to msdn refernce). This method put querystring to an instance of
sysevents class as i described below:

void ProduceQuery()
{
SysEvents eventStart = new SysEvents();
StringBuilder sb = new StringBuilder ();
sb.Append("AND TargetInstance.DayOfWeek=4");
sb.Append(" ");
sb.Append("AND TargetInstance.Hour=7");
sb.Append(" ");
sb.Append("AND TargetInstance.Minute=0");
sb.Append(" ");
sb.Append("AND TargetInstance.Second=0");
eventStart.SetTimerStart (sb.ToString());
eventStart.OnTimerElapsedStart +=new
SysEvents.TimerHandler(eventStart_OnTimerElapsedStart);
}
private void eventStart_OnTimerElapsedStart(object sender,
EventArrivedEventArgs e)
{
//code goes here
}


Here is the code of SysEvents class:

public delegate void TimerHandler (object sender, EventArrivedEventArgs e);
public event TimerHandler OnTimerElapsedStart;

public void SetTimerStart(string queryString)
{
ManagementEventWatcher w = null;
ManagementOperationObserver observer = new
ManagementOperationObserver();
WqlEventQuery q = new WqlEventQuery ();
q.QueryString = "Select * from
__InstanceModificationEvent Where
TargetInstance
ISA 'Win32_LocalTime' "+queryString ;
w=new ManagementEventWatcher(q);
w.EventArrived +=new EventArrivedEventHandler(w_EventArrived);
w.Start();
}
private void w_EventArrived(object sender, EventArrivedEventArgs e)
{
if(OnTimerElapsedStart != null) OnTimerElapsedStart (sender,e);
}

And it doesn work - I havent got this event at thursday, 7am. If I put
querystring="" sysevents generates events every second ( as described in
msdn).
Does anybody know, why?
Thaks
 
N

Nicholas Paldino [.NET/C# MVP]

DAMAR,

This might be a foolish question, but why are you handling this
yourself? Why not create a task with Windows Scheduler to run your task
every thursday at 7 am, and just write an EXE to do what you need it to do
at that time? Why reinvent the wheel?

Hope this helps.
 
G

Guest

Well, I cannot do this, because I write an application - watchdog. I get a
configuration file once a day and this file contains all information about
scheduled applications. But I dont know what application should i watch and i
dont know when - so i cannot use windows scheduler, I have to do it myself by
hardcoding it.
Hope, you can help me
Damar

Nicholas Paldino said:
DAMAR,

This might be a foolish question, but why are you handling this
yourself? Why not create a task with Windows Scheduler to run your task
every thursday at 7 am, and just write an EXE to do what you need it to do
at that time? Why reinvent the wheel?

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

DAMAR said:
I have got a problem with WMI's Win32_LocalTime:
here is the msdn reference:
http://msdn.microsoft.com/library/en-us/wmisdk/wmi/win32_localtime.asp


I'D like to have a timer which pass me an event every thursday at 7 am.
I have got a method to produce a querystring to configure this timer
(according to msdn refernce). This method put querystring to an instance
of
sysevents class as i described below:

void ProduceQuery()
{
SysEvents eventStart = new SysEvents();
StringBuilder sb = new StringBuilder ();
sb.Append("AND TargetInstance.DayOfWeek=4");
sb.Append(" ");
sb.Append("AND TargetInstance.Hour=7");
sb.Append(" ");
sb.Append("AND TargetInstance.Minute=0");
sb.Append(" ");
sb.Append("AND TargetInstance.Second=0");
eventStart.SetTimerStart (sb.ToString());
eventStart.OnTimerElapsedStart +=new
SysEvents.TimerHandler(eventStart_OnTimerElapsedStart);
}
private void eventStart_OnTimerElapsedStart(object sender,
EventArrivedEventArgs e)
{
//code goes here
}


Here is the code of SysEvents class:

public delegate void TimerHandler (object sender, EventArrivedEventArgs
e);
public event TimerHandler OnTimerElapsedStart;

public void SetTimerStart(string queryString)
{
ManagementEventWatcher w = null;
ManagementOperationObserver observer = new
ManagementOperationObserver();
WqlEventQuery q = new WqlEventQuery ();
q.QueryString = "Select * from
__InstanceModificationEvent Where
TargetInstance
ISA 'Win32_LocalTime' "+queryString
;
w=new ManagementEventWatcher(q);
w.EventArrived +=new EventArrivedEventHandler(w_EventArrived);
w.Start();
}
private void w_EventArrived(object sender, EventArrivedEventArgs e)
{
if(OnTimerElapsedStart != null) OnTimerElapsedStart (sender,e);
}

And it doesn work - I havent got this event at thursday, 7am. If I put
querystring="" sysevents generates events every second ( as described in
msdn).
Does anybody know, why?
Thaks
 
W

Willy Denoyette [MVP]

You are missing a space between 'Win32_Locatime' and your querystring, the
resultant string looks like
Win32_Locatime'AND Targe...

which should throw an Unparsable querystring exception....

Willy.
 
G

Guest

if i erase dayofweek an left hour , mins and sec - it works. but its not what
i expected
 
W

Willy Denoyette [MVP]

DAMAR said:
if i erase dayofweek an left hour , mins and sec - it works. but its not
what
i expected

There seems to be a problem when adding DayOfWeek to the filter, a possible
solution is to filter on Hour (7), Minute(0) and second(0).
When the event arrives (at 7h) you can check the DayOfWeek=5 from the event
instance data and proceed when equal.

Note that I would remove the second from the filter, that way the event will
be fired 60 times at 7:00 and you are sure that it will work even if you
miss an event.

Willy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top