2012-07-09 78 views
0

我無法讓它進入睡眠狀態,我希望它在繼續之前暫停一秒。在我的循環中添加睡眠

public static void main(String[] args) { 
     //supply your own path instead of using this one 
     String path = "\\image.JPG"; 
     for(;;) 
      SPI.INSTANCE.SystemParametersInfo(  
       new UINT_PTR(SPI.SPI_SETDESKWALLPAPER),  
       new UINT_PTR(0),  
       path,   
       new UINT_PTR(SPI.SPIF_UPDATEINIFILE | SPI.SPIF_SENDWININICHANGE)); 
     } 

當我嘗試添加Thread.sleep(1000);它給了我一個錯誤,無法訪問的代碼。

+3

你在哪裏添加它? – 2012-07-09 17:53:34

+0

你想在哪裏停止這 – 2012-07-09 17:53:36

+0

你在哪裏添加Thread.sleep()調用? – 2012-07-09 17:53:46

回答

1

試試這個......在for循環中添加sleep方法。

public static void main(String[] args) { 
     //supply your own path instead of using this one 
     String path = "\\image.JPG"; 

     for(;;){ 

       try{ 
        Thread.sleep(1000); 
        SPI.INSTANCE.SystemParametersInfo(  
        new UINT_PTR(SPI.SPI_SETDESKWALLPAPER),  
        new UINT_PTR(0),  
        path,   
        new UINT_PTR(SPI.SPIF_UPDATEINIFILE | SPI.SPIF_SENDWININICHANGE)); 
        }catch(Exception ex){ 

        } 


      } 
+0

表示感謝! – jerhynsoen 2012-07-09 18:24:33

+0

你是最受歡迎的 – 2012-07-09 18:25:32

1

確保您將sleep()添加到for循環的內部,而不是外部。

您還需要趕上潛力InterruptedException

for(;;) 
    SPI.INSTANCE.SystemParametersInfo(  
     new UINT_PTR(SPI.SPI_SETDESKWALLPAPER),  
     new UINT_PTR(0),  
     path,   
     new UINT_PTR(SPI.SPIF_UPDATEINIFILE | SPI.SPIF_SENDWININICHANGE)); 

    try { 
     Thread.sleep(1000l); 
    } catch(InterruptedException e){} 
}