Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy do Clipboard Listener use more CPU usage?
    primarykey
    data
    text
    <p>I wrote program that listen Clipboard and worked correctly, but i saw that the program use more CPU usage. CPU usage is more 70%. Why? Can i reduce it? or i want Listener that listen windows's copy action. Is there Listener in java for this? This is my code:</p> <pre><code> package yoxlama; import java.awt.*; import java.awt.datatransfer.*; import java.io.IOException; import java.net.MalformedURLException; class ClipBoardListener extends Thread implements ClipboardOwner { Clipboard sysClip = Toolkit.getDefaultToolkit().getSystemClipboard(); static ClipBoardListener b; Interlsoft interlsoft = new Interlsoft(); public void run() { try{ Transferable trans = sysClip.getContents(this); regainOwnership(trans); }catch (Exception e) { try { b.finalize(); } catch (Throwable e1) { // TODO Auto-generated catch block e1.printStackTrace(); } b= new ClipBoardListener(); b.start(); System.out.println("NESE1"); } System.out.println("Listening to board..."); while(true) {} } public void lostOwnership(Clipboard c, Transferable t) { try { Thread.sleep(50); } catch(Exception e) { System.out.println("Exception: " + e); } try{ Transferable contents = sysClip.getContents(this); processContents(contents); regainOwnership(contents); }catch (Exception e) { try { b.finalize(); b= new ClipBoardListener(); b.start(); } catch (Throwable e1) { // TODO Auto-generated catch block e1.printStackTrace(); } System.out.println("nese"); } } void processContents(Transferable t) throws MalformedURLException, IOException { String str = getClipboard(t); if(str!=null){ str= str.replace("\n", "%0D%0A"); str= str.replace("\r", ""); str= str.replace("\t", ""); str= str.replace("/", ""); str= str.replace("-", ""); interlsoft.translate(str); } // System.out.println("Processing: " + getClipboard(t)); } void regainOwnership(Transferable t) { sysClip.setContents(t, this); } public static void main(String[] args) { b = new ClipBoardListener(); b.start(); } public static String getClipboard(Transferable t) { // Transferable t = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null); try { if (t != null &amp;&amp; t.isDataFlavorSupported(DataFlavor.stringFlavor)) { String text = (String)t.getTransferData(DataFlavor.stringFlavor); return text; } } catch (UnsupportedFlavorException e) { System.out.println("BURDA1"); } catch (IOException e) { System.out.println("BURDA1"); } return null; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload