in reply to Re: multiple infinitive loops
in thread multiple infinitive loops
A personal plee, ignore at will. Please insert some horizontal whitespace (and use proper indentation, though I realise that that can get screwed through posting/cutting/pasting).
sub check_1 { my $query = " SELECT * FROM __InstanceCreationEvent WHERE TargetInstance ISA'Win32_NTLogEvent' "; my $wbemloc = new Win32::OLE( "WbemScripting.SWbemLocator" ); $wbemloc->{ Security_ }{ Privileges } ->AddAsString( "SeSecurityPrivilege" ); my $wbemsvc = $wbemloc->ConnectServer( ".", "root/cimv2" ); $wbemsvc->{ Security_ }{ ImpersonationLevel } = 3; my $wbemevtsrc = $wbemsvc->ExecNotificationQuery( $query ); while( 1 ) { my $wbemobj = $wbemevtsrc->NextEvent(); my $msg = $wbemobj->{ TargetInstance }{ Message }; $msg =~ s/\t//g; my @array = split( /\r\n/, $msg ); my $tst = join( ';', @array ); $tst =~ s/;;/;/g; print $tst."\n"; } }
More readable than above? I had to resort to sorting the source to determine that check_1 and check_2 were identical.
Which brings me to my second point. Cut&paste code is bad (bad,bad;), and completely unnecessary. The whole point of subroutines is that you can call them twice or more. (Caveat: closures can complicate this!)
This works fine.
#! perl -slw use strict; use threads qw[ async ]; sub sub1 { my $no = shift; while(1) { print "in sub $no"; select undef, undef, undef, 0.1; } } async \&sub1, 1; ## Param = '1' sub1( 2 ); ## Param = '2' __END__ P:\test>397011 in sub 2 in sub 1 in sub 2 in sub 1 in sub 2 in sub 1 Terminating on signal SIGINT(2)
And that brings up the third point which is speculative because I don't have the "WbemScripting.SWbemLocator" (as far as I am aware) in order to verify this.
Many OLE .dll's use what I believe is called the 'apartment' model. The in's and out's of this are complicated, off-topic for this place, and I'm not sure I remember them correctly (if I ever did). Breifly, there are various ways of building DLLs, some of which allow that DLL to be called from as many processes, and as many threads within those processes as you like. Ie. they are fully reentrant. Others can only be entered once from within a given process. There are other variations as well--see MSDN for more/better/accurate information.
The upshot is that it may be that the DLL that provides the OLE object you are calling is not compatible with threading. It would be a good thing to know, before continuing to try do what your doing, if this is the case or not. The best I can suggest is that you consult the docs, or vendor, before expending more energy trying to solve this.
Alternatively, if you have and know VB, then try doing what your doing from (minimally) from there. If you hang up there too, then you probably have your answer.
Of course, it may be that your real code doesn't try to access the same identical object/event loop concurrently, and this is just more sample code.
Ultimately, sample code rarely helps solve real problems.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: multiple infinitive loops
by ozkaa (Acolyte) on Oct 06, 2004 at 20:52 UTC |