C# events/threading

  • Thread starter Thread starter Bardo
  • Start date Start date
B

Bardo

Hi,

I have a situation where I am capturing both a WMI event utilising the
"ManagementEventWatcher" in the "System.Management" namespace, and a
corresponding event ("EntryWritten") raised from the "EventLog" object in
the "System.Diagnostics" namespace.
When a certain event occurrs, both a WMI event is raised, and an event log
entry is written.
The problem I have is that I need to capture both events and somehow
correlate which WMI event relates to its associated "event log" event.
I need to retrieve data from the WMI object and also the event log to
populate a custom object.
I am having trouble understanding how the synchronisation of this should be
done. At the moment I have 2 class level variables - one is populated when
the WMI event is captured, and the other is populated when the event log
event is captured. I then check (in both onEvent methods) to see if they
are not null and then raise a custom event, then set the variables back to
null.

I am not too sure what better way to check for this, but I am pretty sure
that the way I am doing this is a big no no!

I hope I have explained this thoroughly enough - I am struggling to put the
problem into words!

Any help appreciated.

Bardo.
 
Hi Bardo,

I am researching the issue, I will update you with new information ASAP.
Have a nice day.

Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
 
Hi Bardo,

I think there may be one problem with your solution.
Soon after the WMI event is fired and WMI event variable is set, while the
eventlog variable has not been modified.
If at this time, another WMI event is fired and the WMI event variable is
set again for the latter event, then the latter WMI event will correlate
with the former eventlog event.

I think you should keep these 2 variables' access mutex, then the
association will be correct.

Beside this, I think your way of solution is suitable.
Do you find any other problem about your solution?

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| Reply-To: "Bardo" <[email protected]>
| From: "Bardo" <[email protected]>
| Subject: C# events/threading
| Date: Tue, 14 Oct 2003 16:07:37 +1000
| Lines: 30
| Organization: Bryn Systems
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <[email protected]>
| Newsgroups: microsoft.public.dotnet.languages.csharp
| NNTP-Posting-Host: 65-58.dsl.connexus.net.au 203.222.65.58
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP09.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:191130
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| Hi,
|
| I have a situation where I am capturing both a WMI event utilising the
| "ManagementEventWatcher" in the "System.Management" namespace, and a
| corresponding event ("EntryWritten") raised from the "EventLog" object in
| the "System.Diagnostics" namespace.
| When a certain event occurrs, both a WMI event is raised, and an event log
| entry is written.
| The problem I have is that I need to capture both events and somehow
| correlate which WMI event relates to its associated "event log" event.
| I need to retrieve data from the WMI object and also the event log to
| populate a custom object.
| I am having trouble understanding how the synchronisation of this should
be
| done. At the moment I have 2 class level variables - one is populated
when
| the WMI event is captured, and the other is populated when the event log
| event is captured. I then check (in both onEvent methods) to see if they
| are not null and then raise a custom event, then set the variables back to
| null.
|
| I am not too sure what better way to check for this, but I am pretty sure
| that the way I am doing this is a big no no!
|
| I hope I have explained this thoroughly enough - I am struggling to put
the
| problem into words!
|
| Any help appreciated.
|
| Bardo.
|
|
|
 
Hi Jeffrey,

Thanks for the response.
Yes I thought that the solution I had would be a bit buggy in correlating
the events.
I am fairly new to threading issues, so is it possible for you to elaborate
when you say to keep the 2 variables access mutex?
Should the class level vars be static?
I have read the MSDN docco on the mutex class and I understand that it is
used for synchronising threads/processes, but I am still a bit unsure on how
to implement this.

I will try playing around with some samples now.

Cheers,

Bardo.

"Jeffrey Tan[MSFT]" said:
Hi Bardo,

I think there may be one problem with your solution.
Soon after the WMI event is fired and WMI event variable is set, while the
eventlog variable has not been modified.
If at this time, another WMI event is fired and the WMI event variable is
set again for the latter event, then the latter WMI event will correlate
with the former eventlog event.

I think you should keep these 2 variables' access mutex, then the
association will be correct.

Beside this, I think your way of solution is suitable.
Do you find any other problem about your solution?

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| Reply-To: "Bardo" <[email protected]>
| From: "Bardo" <[email protected]>
| Subject: C# events/threading
| Date: Tue, 14 Oct 2003 16:07:37 +1000
| Lines: 30
| Organization: Bryn Systems
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <[email protected]>
| Newsgroups: microsoft.public.dotnet.languages.csharp
| NNTP-Posting-Host: 65-58.dsl.connexus.net.au 203.222.65.58
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP09.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:191130
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| Hi,
|
| I have a situation where I am capturing both a WMI event utilising the
| "ManagementEventWatcher" in the "System.Management" namespace, and a
| corresponding event ("EntryWritten") raised from the "EventLog" object in
| the "System.Diagnostics" namespace.
| When a certain event occurrs, both a WMI event is raised, and an event log
| entry is written.
| The problem I have is that I need to capture both events and somehow
| correlate which WMI event relates to its associated "event log" event.
| I need to retrieve data from the WMI object and also the event log to
| populate a custom object.
| I am having trouble understanding how the synchronisation of this should
be
| done. At the moment I have 2 class level variables - one is populated
when
| the WMI event is captured, and the other is populated when the event log
| event is captured. I then check (in both onEvent methods) to see if they
| are not null and then raise a custom event, then set the variables back to
| null.
|
| I am not too sure what better way to check for this, but I am pretty sure
| that the way I am doing this is a big no no!
|
| I hope I have explained this thoroughly enough - I am struggling to put
the
| problem into words!
|
| Any help appreciated.
|
| Bardo.
|
|
|
 
Hi Bardo,

If the two variable should be static lies on your program logic. The static
variable is assciated with the entire class, while the member variable is
associated with a certain instance.

I think if you use your two variables as non-static, then every time a WMI
event generates, you should create an instance of this class.
Because different instance did not share the same varables, the mutext is
no need.

If you use static variables, you need not create a new instance for every
event, but you should protect the share variables. This is what mutex take
effect.
You should protect your variables can not be access before it was clear to
null.
You can use .Net Mutex class to implement mutex access, I think it is easy
to use.

Because the mutex and sychronization is easy to generate error, I recommand
you create the variables as non-static.

Hope this helps,

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| Reply-To: "Bardo" <[email protected]>
| From: "Bardo" <[email protected]>
| References: <[email protected]>
<[email protected]>
| Subject: Re: C# events/threading
| Date: Thu, 16 Oct 2003 17:22:33 +1000
| Lines: 103
| Organization: Bryn Systems
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <[email protected]>
| Newsgroups: microsoft.public.dotnet.languages.csharp
| NNTP-Posting-Host: 65-58.dsl.connexus.net.au 203.222.65.58
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP11.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:191727
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| Hi Jeffrey,
|
| Thanks for the response.
| Yes I thought that the solution I had would be a bit buggy in correlating
| the events.
| I am fairly new to threading issues, so is it possible for you to
elaborate
| when you say to keep the 2 variables access mutex?
| Should the class level vars be static?
| I have read the MSDN docco on the mutex class and I understand that it is
| used for synchronising threads/processes, but I am still a bit unsure on
how
| to implement this.
|
| I will try playing around with some samples now.
|
| Cheers,
|
| Bardo.
|
| | >
| > Hi Bardo,
| >
| > I think there may be one problem with your solution.
| > Soon after the WMI event is fired and WMI event variable is set, while
the
| > eventlog variable has not been modified.
| > If at this time, another WMI event is fired and the WMI event variable
is
| > set again for the latter event, then the latter WMI event will correlate
| > with the former eventlog event.
| >
| > I think you should keep these 2 variables' access mutex, then the
| > association will be correct.
| >
| > Beside this, I think your way of solution is suitable.
| > Do you find any other problem about your solution?
| >
| > Best regards,
| > Jeffrey Tan
| > Microsoft Online Partner Support
| > Get Secure! - www.microsoft.com/security
| > This posting is provided "as is" with no warranties and confers no
rights.
| >
| > --------------------
| > | Reply-To: "Bardo" <[email protected]>
| > | From: "Bardo" <[email protected]>
| > | Subject: C# events/threading
| > | Date: Tue, 14 Oct 2003 16:07:37 +1000
| > | Lines: 30
| > | Organization: Bryn Systems
| > | X-Priority: 3
| > | X-MSMail-Priority: Normal
| > | X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| > | Message-ID: <[email protected]>
| > | Newsgroups: microsoft.public.dotnet.languages.csharp
| > | NNTP-Posting-Host: 65-58.dsl.connexus.net.au 203.222.65.58
| > | Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP09.phx.gbl
| > | Xref: cpmsftngxa06.phx.gbl
| microsoft.public.dotnet.languages.csharp:191130
| > | X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
| > |
| > | Hi,
| > |
| > | I have a situation where I am capturing both a WMI event utilising the
| > | "ManagementEventWatcher" in the "System.Management" namespace, and a
| > | corresponding event ("EntryWritten") raised from the "EventLog" object
| in
| > | the "System.Diagnostics" namespace.
| > | When a certain event occurrs, both a WMI event is raised, and an event
| log
| > | entry is written.
| > | The problem I have is that I need to capture both events and somehow
| > | correlate which WMI event relates to its associated "event log" event.
| > | I need to retrieve data from the WMI object and also the event log to
| > | populate a custom object.
| > | I am having trouble understanding how the synchronisation of this
should
| > be
| > | done. At the moment I have 2 class level variables - one is populated
| > when
| > | the WMI event is captured, and the other is populated when the event
log
| > | event is captured. I then check (in both onEvent methods) to see if
| they
| > | are not null and then raise a custom event, then set the variables
back
| to
| > | null.
| > |
| > | I am not too sure what better way to check for this, but I am pretty
| sure
| > | that the way I am doing this is a big no no!
| > |
| > | I hope I have explained this thoroughly enough - I am struggling to
put
| > the
| > | problem into words!
| > |
| > | Any help appreciated.
| > |
| > | Bardo.
| > |
| > |
| > |
| >
|
|
|
 
Back
Top