Wait for workspace job to finish
<p>Background:
I am creating a plug-in that triggers another plug-in's command. This command does a check (background process) and implements markers on specific programs.</p>

<p>Problem:
I have successfully programmatically triggered the command however, I need to wait for it to finish because if I don't wait, I will only get NULL set of markers[]. </p>

<p>So far, here is what I have</p>

<pre><code>IWorkspace workspace = ResourcesPlugin.getWorkspace();
IResource resource = workspace.getRoot();
resource.deleteMarkers(null, true, 0);
markers = resource.findMarkers(IMarker.MARKER, true, IResource.DEPTH_INFINITE);
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_SHIFT);
robot.keyPress(KeyEvent.VK_F2);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyRelease(KeyEvent.VK_SHIFT);    
robot.keyRelease(KeyEvent.VK_F2);

//...command is executed, now running background process
//...wait for all background process to finish

for (IMarker m : markers) {
    System.out.println("Id: " + m.getId());
    System.out.println("Message: " + m.getAttribute(IMarker.MESSAGE));
    System.out.println("Line Number: " + m.getAttribute(IMarker.LINE_NUMBER));  
}
</code></pre>

<p>I have tried implementing this </p>

<pre><code>ResourcesPlugin.getWorkspace().run(new IWorkspaceRunnable() {
    public void run(IProgressMonitor monitor) throws CoreException {

    }
}, new NullProgressMonitor());
</code></pre>

<p>as explained from here: <a href="https://dzone.com/articles/eclipse-gui-testing-is-viable-" rel="nofollow noreferrer">https://dzone.com/articles/eclipse-gui-testing-is-viable-</a></p>

<blockquote>
  <ol start="3">
  <li>Instead of using sleep() to wait for something to be done, wait for the real thing to be done. A heavily loaded machine can cause
  processing times to vary. Instead of having a fragile sleep(500L), use
  a reliable technique to determine when the job is really done.</li>
  </ol>
  
  <p>For example, if you know that your processing is holding a resource
  lock, post a no-op empty workspace job and wait on it inside your
  test. It will only be invoked once all other resource locks are
  released, so when it's complete you're guaranteed that your other job
  is done</p>
</blockquote>

<p>But I guess I am implementing it wrongly because it does not wait for the background jobs to finish.</p>

<p>Any advice on what to do will definitely help!</p>

<p>Edit: To add on, the job/task/process that was executed is from another plug-in that I do not know of. Hence, I do not know the job group, etc. All I'm relying on is waiting for the task to finish.</p>