#! perl -slw use strict; require 5.008; use threads; use threads::shared; use Thread::Queue; use Win32::Clipboard; $|++; # Disable buffering so we see the changes immediatly my $flag : shared = 0; # A flag to tell the thread when to die my $q = new Thread::Queue; # Create a thread sub watchClipboard{ my $clip = Win32::Clipboard(); # Get a handle to the CB while ( !$flag ) { # Repeat until we're told to die $clip->WaitForChange(); # Block waiting for an event # Once something happens, queue it back to the main program $q->enqueue( $clip->Get() ); } }; my $watcher = threads->create( 'watchClipboard' ); my $i =0; # A count to allow the demo to self terminate while( $i < 600 ) { # Wait for 1 minute of inactivity before calling it quits. while( $q->pending ) { # If the watcher put anything in the queue, display it. $i = 0; print( $/, 'From the clipboard: ', $q->dequeue ); } # Show we can be doing other things printf "\rMeanwhile, just killing time: %2.1f", $i++/10; # Stop the demo from chewing cpu. select undef,undef,undef, .1; } $flag = 1; # Set the flag asking the thread to die Win32::Clipboard()->Set(''); # Put something on the CB to break the wait $watcher->join; # Wait for it to die. exit;