7 Replies Latest reply on Dec 21, 2006 1:25 AM by iamnotwired

    How to display an Alert synchronously ?

    iamnotwired
      The Alert.show() function displays an alert asynchronously in Flex (the code line which follows the Alert.show() statement is executed well before the alert actually appears on screen).

      Anybody knows a way to force this alert to show itself syncronously instead please?

      Thank you in advance.
        • 1. Re: How to display an Alert synchronously ?
          M. Huisman
          Since it blanks out the whole screen, and awaits user interaction (thus pauses all other excecution of code) it seems impossible to do such a thing with alert.show.

          You might be able to create an alert (error) handling class, which picks up all the fired alerts, and shows them in an alternate way.

          Mark
          • 2. Re: How to display an Alert synchronously ?
            peterent Level 2
            Flex is deliberately designed NOT to work synchronously. It causes too many problems and the Flash Player has to continue running to process events anyway.

            Your best bet is to go with the flow - when the Alert comes up it is modal and input will not be allowed outside of the Alert. Your program should be written in such a way that it will not continue processing until the Alert's event listener is called.
            • 3. Re: How to display an Alert synchronously ?
              iamnotwired Level 1
              "Your program should be written in such a way that it will not continue processing until the Alert's event listener is called."
              How could I do precisely that in AS3 please? Any "sleep()" function I didn't find and that would be designed to stop the main script thread at a specific point? I tried to write a normal for(;;) loop but it just stopped the whole script and even prevent the Alert box to show up.

              Thank you.
              • 4. Re: How to display an Alert synchronously ?
                peterent Level 2
                In the following example an Alert box is popped up with a Yes/No question. At no time is the Flash Player (your Flex SWF) paused or waiting. This is purely event driven.

                private function showChoiceAlert() : void
                {
                Alert.show("This is a choice. Either pick Yes or No, please", "Choices",
                Alert.YES | Alert.NO, null,
                alertHandler);
                }

                You call this function as the result of some action, such as a button click. When either of the Alert's Yes or No buttons are picked, the function, alertHandler(), is called:

                private function alertHandler( event:mx.events.CloseEvent ) : void
                {
                // the event.detail indicates which button was pressed. Compare it
                // to the static values in the Alert class.

                if( event.detail == Alert.YES ) {
                result.text = "You picked Yes";
                } else if( event.detail == Alert.NO ) {
                result.text = "You picked No";
                } else {
                result.text = "I don't know what you picked!";
                }
                }

                In this example, the alertHandler fills a Label component's text property.

                In other words, you cannot 'wait' for the user to do something. The 'waiting' is simply the Flash Player doing whatever it does until the user picks a button on the Alert dialog which fires an event.
                • 5. Re: How to display an Alert synchronously ?
                  iamnotwired Level 1
                  Thank you for your replies Peter, even if I am still in trouble :)

                  You see, I am not writing the script, so I can't control it's workflow. The script is a direct translation from VBScript, where the original programmer can uses real modal boxes like this:

                  Dim Input
                  Input = InputBox("Enter your name")
                  MsgBox ("You entered: " & Input)

                  Translating to some AS3 it would become something like this:

                  var sInput:String = _vbs.InputBox("Enter your name");
                  _vbs.MsgBox("You entered:" + sInput);

                  Unfortunately, it cannot works like this in AS because we can't control the script threads and I understand I am screwed :(

                  Thank you anyway.

                  • 6. How to display an Alert synchronously ?
                    dishmael Level 1
                    You may be better off than you realize, you just have to change the way your thinking about the problem. Rather than code sequentially, move towards an event driven design. For your example:

                    Dim Input
                    Input = InputBox("Enter your name")
                    MsgBox ("You entered: " & Input)

                    You just need to move your MsgBox() to the event handler of the InputBox() using something similar to the example Peter provided. Create a custom component that contains an input box and use the PopUpManager.

                    When I first started with event driven programming I also had a hard time moving away from sequential programming. These boards have been a wealth of useful knowledge to help with the transition. So far I have not run into any sequential issues that could not be resolved with a little ingenuity and good programming practices. I can post a decent example for your example if you'd like. If you get stuck, post some questions along with some of your code and I'm sure you'll get lots of help.
                    • 7. Re: How to display an Alert synchronously ?
                      iamnotwired Level 1
                      quote:

                      Originally posted by: dishmael
                      You just need to move your MsgBox() to the event handler of the InputBox() using something similar to the example Peter provided.


                      Thanks for your replies guys, but Peter and you are missing a very important point: I AM NOT writing the original script. I am a software engineer and I write in C++, not in script. My job at the moment is to develop a VBScript to ActionScript translator. Our customers can script one of our products with VBScript to produce an HTML application. I am actually trying to broaden their horizon and let them create a Flex application instead (using the Flex SDK). The problem is that VBScript is event driven AND synchronous while AS is event driven and asynchronous. So I don't see how I will be able to translate VBScript synchronous calls like MsgBox() and Input() in AS :(