@@ -2,7 +2,7 @@ extern crate notify;
22
33use notify:: { Watcher , RecursiveMode , RawEvent , raw_watcher} ;
44use notify:: op:: Op ;
5- use std:: sync:: mpsc:: { channel, Sender } ;
5+ use std:: sync:: mpsc:: { channel, Sender , Receiver , RecvTimeoutError } ;
66
77extern crate app_dirs;
88use app_dirs:: * ;
@@ -13,6 +13,7 @@ use std::io::Seek;
1313use std:: io:: SeekFrom ;
1414use std:: io:: Read ;
1515use std:: thread;
16+ use std:: time:: Duration ;
1617
1718#[ macro_use] extern crate lazy_static;
1819extern crate regex;
@@ -156,7 +157,7 @@ impl FileScanner {
156157
157158const APP_INFO : AppInfo = AppInfo { name : "Logs" , author : "CrossoverWorkSmart" } ;
158159
159- fn create_log_watch_thread ( ui_tx : Sender < MeterControlMessage > ) /* -> JoinHandle<()>*/ {
160+ fn create_log_watch_thread ( ui_tx : Sender < MeterControlMessage > , shutdown_rx : Receiver < ( ) > ) -> thread :: JoinHandle < ( ) > {
160161 thread:: spawn ( move || {
161162 let mut path = get_app_root ( AppDataType :: UserConfig , & APP_INFO ) . unwrap ( ) ;
162163 path. push ( "deskapp.log" ) ;
@@ -174,21 +175,29 @@ fn create_log_watch_thread(ui_tx: Sender<MeterControlMessage>)/* -> JoinHandle<(
174175 // Add a path to be watched. All files and directories at that path and
175176 // below will be monitored for changes.
176177 watcher. watch ( path, RecursiveMode :: NonRecursive ) . unwrap ( ) ;
177- loop {
178- match rx. recv ( ) {
178+ ' recv_loop : loop {
179+ match rx. recv_timeout ( Duration :: from_secs ( 1 ) ) {
179180 Ok ( RawEvent { path : Some ( _path) , op : Ok ( op) , cookie : _cookie } ) => scanner. handle_event ( op) . unwrap ( ) ,
180181 Ok ( event) => println ! ( "broken event: {:?}" , event) ,
182+ Err ( RecvTimeoutError :: Timeout ) => ( ) , // that's OK
181183 Err ( e) => println ! ( "watch error: {:?}" , e) ,
182184 }
185+
186+ for _ in shutdown_rx. try_iter ( ) {
187+ println ! ( "Received shutdown message." ) ;
188+ break ' recv_loop;
189+ }
183190 }
184- } ) ;
191+ } )
185192}
186193
187194#[ cfg( target_os = "windows" ) ]
188195fn main ( ) {
189196 let ( ui_tx, ui_rx) = channel ( ) ;
190- create_log_watch_thread ( ui_tx) ;
191- ten_minutes:: TenMinutesMeter :: new ( ui_rx) . main ( ) ;
197+ let ( shutdown_tx, shutdown_rx) = channel ( ) ;
198+ let handle = create_log_watch_thread ( ui_tx, shutdown_rx) ;
199+ ten_minutes:: TenMinutesMeter :: new ( ui_rx, shutdown_tx) . main ( ) ;
200+ handle. join ( ) . ok ( ) ;
192201}
193202
194203#[ cfg( test) ]
0 commit comments