Eclipse RCP-- how to do Pause/Resume button functionality with threads using(wait and notify) - eclipse

Problem statement:--
I have a UI button "Perform"-- which will initiate a big background process in loop in a different thread sequentially.
As soon the process is started the button text will be toggled to "Pause"
Now on press of "Pause", I need to suspend the thread execution and "Resume" will finally notify to go ahead.
On button press"Perform",thread gets started, and button toggles to "Pause" and if I press Pause,thread is suspended.But I am not able to resume the running thread.If I check the Thread state in resume button press event, I get value=0 which is equivalent to NONe. Please help. Following is my code....
Blockquote
performBButton.addSelectionListener(new SelectionListener() {
#Override
public void widgetSelected(SelectionEvent e) {
Button btnToggle = (Button) e.getSource();
MyJob myJob = new MyJob();
if (btnToggle.getText().equalsIgnoreCase("Perform")) {
btnToggle.setText("Pause");
myJob.schedule();
}
else if (btnToggle.getText().equals("Pause")) {
buttonToggle.setText("Resume");
isPaused=!isPaused;
}
else if (btnToggle.getText().equals("Resume")) {
buttonToggle.setText("Pause");
isPaused=!isPaused;
synchronized (myJob) {
myJob.notify();
}
}
}
});
class MYJob extends Job{
public void run(IProgessMonito monitor) {
#Override
public void run() {
for (int i=0;i<50000;i++) {
System.out.println("Iteration "+i +isPaused);
synchronized (this) {
if (isPaused) {
wait();
}
}
}
Thread.sleep(1000);//I have removed try catch while pasting here
}
}
}
}

changing wait() to myThread.wait() actually solved my problem, by declaring myThread globally. but its not the correct way , any further suggestions will be helpful...

Related

SWT: Changing the background color of a label two times

My problem is to change a background color in a runnable. I am trying to change the background color of a label like traffic lights do (red -> yellow -> green) and I have not much experience with SWT. So I do not see why my attempt doesn't work:
Display.getDefault().asyncExec(new Runnable() {
public void run()
{
label.setBackground(red);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
label.setBackground(yellow);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
label.setBackground(green);
}
});
What happens is that nothing is changing during the waiting time (2*1 second) and the last color (in this case green) is shown - no red and no yellow. Could you please give me a hint what my mistake is? Or an idea how to solve this problem?
Display.asyncExec runs the entire Runnable in the User Interface thread. So your two Thread.sleep calls run in the UI thread and stop the thread from updating the UI.
To do something with a delay use the timerExec method of Display to schedule a Runnable to run after a delay.
// Set first color. Note: use `asyncExec` if you are not in the UI thread
label.setBackground(red);
// Schedule changes
Display.getDefault().timerExec(1000, new Runnable() {
#Override
public void run()
{
if (!label.isDisposed()) {
label.setBackground(yellow);
}
}
});
Display.getDefault().timerExec(2000, new Runnable() {
#Override
public void run()
{
if (!label.isDisposed()) {
label.setBackground(green);
}
}
});

Second activity is still start on handler although press back button for quiting app

My application has an Introduce activity that show process bar before using app.
pb = (ProgressBar) findViewById(R.id.pb_loader);
final Handler h = new Handler() {
#Override
public void handleMessage(Message message) {
pb.setVisibility(View.INVISIBLE);
Intent it = new Intent(FirstIntroActivity.this, SecondIntroActivity.class);
it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(it);
}
};
h.sendMessageDelayed(new Message(), 3000);
But after I press BACK button to exit application, my phone is turn back to application and go to SECOND activity ( after 3000ms ). How to resolve this error?
Alternative is to use a Timer to schedule start of your second activity.we can cancel starting the second activity by cancelling timer in OnBackPressed() callback.
private Timer timer;
#Override
protected void onResume() {
super.onResume();
timer = new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
// add code to start your second activity
}
}, 2000);
}
#Override
public void onBackPressed() {
timer.cancel();
super.onBackPressed();
}

continuous actions to be performed when I long tapped MGWT button until the touch end event fired

I want to invoke a method continuously when I used MGWT button long tap handler, this should be done until I release the button. i.e; until the touch end event fired. For this I had written Timer inside the MGWT Button's long tap handler.I continuously calling the my task method inside the run method of the Timer. my code:
upButton.addLongTapHandler(new LongTapHandler() {
#Override
public void onLongTap(LongTapEvent event) {
upBtnTimer = new Timer() {
#Override
public void run() {
if(getValue() >= maxValue){
Window.alert("max val reached");
upBtnTimer.cancel();
}else{
setValue(getValue() + RATE);
}
}
};
upBtnTimer.scheduleRepeating(100);
}
});
And I also wrote touch end handler to the upButton. this is:
upButton.addTouchEndHandler(new TouchEndHandler() {
#Override
public void onTouchEnd(TouchEndEvent event) {
if(upBtnTimer!=null){
upBtnTimer.cancel();
upBtnTimer = null;
}
}
});
this is OK when I'm testing my mobile application on browser, but when I installed my application in iOS/Android device, this is not working. Only single tap event firing.
If you are clear with my requirement please tell me if there is another approach to do this. Thanks in advance.

GWT Delaying an action after an event trigger

I want to have the user click a button, then they will see a "Toast" message popup, which fades away , then the action that the button click performs should happen. What is the best way to do this? Should I trigger a Timer on catching the click event and let it timeout (While the toast is being displayed ) and then call the handling code or is there any built in delay mechanism in event handling I can use?
I don't want my toast to be involved at all in the event handling
If I really follow your requirements the following code should do:
// interface of the "Toast" no matter what the implementation actually is
public interface Toast
{
void open( String message );
void closeFadingAway();
}
// calling code
public class ClientCode
{
private static final int myDelay = 1000; // 1 second in millis
private Toast myToast;
void onMyAction()
{
myToast.open( "Your action is being handled..." );
Scheduler.get().scheduleFixedDelay( new RepeatingCommand()
{
#Override
public boolean execute()
{
myToast.closeFadingAway();
performAction();
return false; // return false to stop the "repeating" = executed only once
}
}, myDelay );
}
void performAction()
{
// do something interesting
}
}
Now, if you actually mean to be able to interrupt the action when the user presses some button in the toast this is a different story.
If you are using a popup panel you could use the addCloseHandler on the popup panel and from here call the method that would have otherwise been called by the button.
popUpPanel.addCloseHandler(new CloseHandler<PopupPanel>(){
#Override
public void onClose(CloseEvent<PopupPanel> event) {
// TODO Do the closing stuff here
}
});
So when the PopupPanel disappears and the close event triggers you can do your magic there.

How do I manage console output in a long running Eclipse plug-in?

I have written an Eclipse plugin that works. What happens, though, is that during the run, no console output is displayed. Only when the process is finished does the output show up in the console. Below is my handler, which appears as an extension point of type org.eclipse.ui.commands:
public class MyHandler extends AbstractHandler {
#Override
public Object execute(ExecutionEvent event) throws ExecutionException {
...
MessageConsoleStream out = myConsole.newMessageStream();
...
IConsoleView view = (IConsoleView) page.showView(id);
view.display(myConsole);
...
out.println("output that only shows up at the end");
myConsole.activate();
...
// Slow process
...
out.println("everything is done");
return null;
}
}
So while the process runs, nothing in the console. Then at the end, both output lines pop into view.
I'm obviously doing the console thing incorrectly, but I haven't found any good examples, nor has my experimentation proven very fruitful. Please advise.
You could consider using a ProgressMonitor (possibly with cancelation in case the user wants to abort), so that the user can see that there is something going on.
This worked:
public class Merge extends AbstractHandler {
private static MessageConsole myConsole = null;
private static ExecutionEvent event = null;
#Override
public Object execute(ExecutionEvent event) throws ExecutionException {
Merge.event = event;
//same idea as original post and other examples where it makes new or finds existing
myConsole = makeConsole(Merge.event);
Job job = new Job("My Job Name"){
#Override
protected IStatus run(IProgressMonitor monitor){
...
if (blah) {
MessageConsoleStream out = myConsole.newMessageStream();
out.println("output show up right away");
...
// Slow process
...
out.println("everything is done");
} else {
MessageDialog.openInformation(HandlerUtil.getActiveShell(Merge.event), "Information", "Please select valid file");
}
monitor.done();
return Status.OK_STATUS;
}
};
job.setUser(true);
job.schedule();
return null;
}
...
}
Maybe you can call out.flush() after every out.print...