简体   繁体   English

始终在Eclipse中运行线程

[英]Run a thread always in eclipse

I am writing an eclipse plugin which will always check the file in current active editor (if editor changes, then it will check new editor) and then highlight some lines in the editor. 我正在编写一个eclipse插件,它将始终在当前活动的编辑器中检查文件(如果编辑器发生更改,则它将检查新的编辑器),然后在编辑器中突出显示某些行。 (A database contains all file names and associated lines that need to be highlighted). (数据库包含所有文件名和需要突出显示的相关行)。

I was succeed to develop the plugin. 我成功开发了插件。 The problem is, it only checks the database once and produce my own error message if it can't connect to database. 问题是,它仅检查数据库一次,如果无法连接到数据库,则会产生我自己的错误消息。 I want the plugin to check the database regularly (for new updates) and as soon as its connected to database, it will highlight the editor immediately. 我希望该插件定期检查数据库(是否有新更新),并且一旦它连接到数据库,它将立即突出显示编辑器。

I think I can't use a eclipse WorkspaceJob for it because workspaceJob will check the editor only one time, but I want to keep it as a continuous process in a certain interval (say every 30 seconds). 我认为我不能为其使用eclipse WorkspaceJob ,因为workspaceJob仅会检查编辑器一次,但是我想将其作为一个连续的过程以一定的间隔(例如每30秒)进行保存。 So, I think a thread is suitable. 因此,我认为线程是合适的。

But how can I implement it? 但是我该如何实施呢? How can I run a thread in eclipse, that will run always? 如何在eclipse中运行一个始终运行的线程? Is there any better solution? 有没有更好的解决方案?

You can still use a WorkspaceJob with the schedule(long delay) method to set your 30 second delay. 您仍然可以使用带有schedule(long delay)方法的WorkspaceJob来设置30秒的延迟。 Have the job reschedule itself after it has run to get it to run on a regular basis: 在作业运行后重新安排其自身的时间,以使其定期运行:

WorkspaceJob job = new WorkspaceJob("jobname") {
    @Override
    public IStatus runInWorkspace(IProgressMonitor monitor)
            throws CoreException {
        // TODO add code here
        this.schedule(30000);
        return Status.OK_STATUS;
    }
};
job.schedule();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM