Avatar billede fredand Forsker
02. december 2006 - 17:07 Der er 1 løsning

How come I can't stop this thread from sleeping?

Hello!

I'm playing around with J2ME and tries to do a modal Alert that returns the value of the user input.
I do not think it's possible from just the class in the API.

How ever. In one thread I start and show a Alert like:

ConfirmAlert confirmAlert = new ConfirmAlert("Please confirm!", "Ok", "Cancel", "This will reset The database");
int i = confirmAlert.showAlert(this);
closeAlert()

The confirm alert just looks like:
public class ConfirmAlert extends MyAppAlert
{
    private Command cancel;
    private Command confirm;

    public ConfirmAlert(String t, String confirmText, String cancelText, String message )
    {
        super(t, message);
        cancel = new Command( cancelText, Command.CANCEL, 2 );
        confirm = new Command( confirmText, Command.OK, 1 );
        setString(message);
        addCommand(cancel);
        addCommand(confirm);
        setCommandListener(this);
        setTimeout(Alert.FOREVER);
    }
}

The Alert is shown and in the extended method I start sleep the current thread.
But even though I change the value of the state the thread does not stop sleeping.
The state has the same value.

Below is my code with the loop that I'm hope that I may stop by pushing a command and change the state.

I do not see where I go wrong. I also hope I have posted enough info for you guys.
Best regards
Fredrik

import java.io.*;
import java.util.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;


public class MyAppAlert extends Alert implements CommandListener
{
    int state = 0;

    public MyAppAlert(String t, String message)
    {
        super(t, message, null, AlertType.WARNING);
        setTimeout(Alert.FOREVER);
    }

    public int showAlert(InfoCanvas infoCanvas)
    {
        infoCanvas.showAlert(this);

        Thread thread = Thread.currentThread();

        try
        {
            while( getState() == 0 )
            {
                System.out.println("State " + state);
                thread.sleep( MyAppMIDlet.SLEEPTIME );
            }
        }
        catch( InterruptedException e )
        {
            e.printStackTrace();
        }
        return state;
    }

    public void commandAction( Command c, Displayable d )
    {
        setState(c.getCommandType());
        System.out.println(getState() + " Command "+c.getCommandType());
    }

    public synchronized int getState()
    {
        return state;
    }

    public synchronized void setState(int s)
    {
        state = s;
    }
}
Avatar billede fredand Forsker
29. december 2010 - 21:06 #1
Old!

I close this one!

Best reagrds
Fredrik
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester