In another thread - another method and then run Sub...

I wrote a method to extract a string from a webpage using StreamConnection and it works perfectly... except that it runs on the main thread.  I wonder if there is a simple way to have a method that returns an object (String in this case) something to run on a thread and return it to the first thread.

Everything I try the only thing I can get to work on the second thread is what's on the run method void, but that returns nothing... because it sucks...

Until now what I thought is to pass the variables I need in the constructor, then set the connection stream real term where I want retruned is written in a class variable and another method that returns the class variable that has been filled with the run method... but that doesn't seem to just... thought he worked in a simple test I ran...

A slightly different way (but completely equivalent) to work in a another thread is to create a class that implements Runnable and feed a thread. If, for any reason, you do not want to expand Thread (because your worker class extends already something else and it would be difficult to refactor), then this variant is a lifeline. You do something like this:

class WorkerClass extends AnotherClass implements Runnable {    // ...    public void run() {       // do time-consuming work here    }}

// later...WorkerClass worker = new WorkerClass();// initialize worker object. Then: new Thread(worker).start();

As it is easy to turn a class in an executable (you simply add "implements Runnable" and a run() method), it's an easy way to avoid the subclassing Thread.

Regardless of how you use to perform work in a separate thread, there is always the question of how to get the results. With the help of the observer is a common way to handle this, but in general, this means that the results are returned in the worker thread, not the thread from the main event. If you want to update the user interface, this can be a problem. Another approach is to structure your code something like the following:

/** * Process the results of some time-consuming operation. */public void acceptResults(ResultType result) {  // ...}

/** * Start a thread to carry out time-consuming work in a * separate thread and deliver the results back in the * event dispatching thread. */public void launchWork() {  new Thread() {        public void run() {           final ResultType res = computeResults();          Runnable r = new Runnable() {                public void run() {                   acceptResults(res);               }         }         Application.getApplication().invokeLater(r);      }

       /**        * Do time-consuming work in this method       */       private ResultType computeResults() {         // connect to server, interpret reply, etc.       } }.start();}

You have the right idea that is running in a thread separate is how to manage a time consuming task. The point of all, however, is that you (the event dispatching thread) don't want to wait for the task at hand. Both the observer model and the code here offer a solution for a worker thread to do the work without blocking the thread of the event. The invokeLater() method was kindly provided by BlackBerry people to give the worker threads neatly transfer control on the event dispatching thread when needed.

Tags: BlackBerry Developers

Similar Questions

  • installation of windows xp/vista/7 on USB and then running windows FULL of her not ONLY START from USB

    is it possible to install windows xp/vista/7 on a USB key and then run the FULL version of windows of him anywhere I want? Please, I beg you!

    Friday, January 28, 2011 18:56:58 + 0000, tupac shakur says:

    is it possible to install windows xp/vista/7 on a USB key and then run the FULL version of windows of him anywhere I want? Please, I beg you!

    # With all these versions of Windows, the installation is
    customized for specific hardware. This will not work on different
    material.

    Ken Blake, Microsoft MVP

  • Wait for the host disconnects and then run a command

    Hello world

    I have a stop script for my virtual infrastructure, now I have to add code to stop an Equallogic AFTER the last host table is turned off.

    I know the commands to do this, but I need help to write a loop that will test the ip address of the last host management and wait until this station responds to pings.

    In my script I have to establish what host will be put off to the end so it's not a problem... How can I keep ping IP management host to die and then run the commands Equallogic?

    I hope that I managed to explain to me... I was exploring the Test-Connection cmdlet but it seems that it displays che echo of the ping instead of State... I'm a bit lost around this, can someone help me please?

    Something like that would do the trick?

    $count = 0

    $maxWait = 10

    {}

    $count += 1

    Write-Output "$(get-date-f HH) $(get-date-f JJ/MM/AAAA)-host is offline."

    sleep 5

    } until (!) (Test-Connection 192.168.110.85 - BufferSize) count 16-1 - calm)- or ($count - gt $maxWait))

    If {($count - gt $maxWait)

    Write-Output "Host always online."

    }

    else {}

    Write-Output "$(get-date-f HH) $(get-date-f JJ/MM/AAAA)-host is offline."

    }

  • Start threading with another processID, and then close the application

    Hello

    It is a delicate issue, so maybe something for all wizards among us

    OS 4.6.0 9000 "BOLD", put to the test through Wifi and 3 G networks

    As far as my knowledge about operating systems goes, it is not possible to start a process of your own application, close the application and then update the application. This is because the process began has the processID of the app as a parentID. So long as the process is running the application is running and access its files of cod.

    I'm trying to get an update mechanism in my application. At the moment I am citing a new private and out of my application (via the call onClose() on all the screens on the stack or the System.exit (0)) after the start of the thread of displayPage() browsersessions. I know, that probably would never work, but it is the simplest approach and worth a try. With the two variants to close the parent application ota download starts but hangs at about 10%. I guess that's because then a cod of the application file is tried to be crushed. The following threads and knowledge previously mentioned on OSs, it would be the point where the application must be fully completed.

    Now, even after all ways to close an app I know... even if I try to reinstall on OTA via the external browser, download hangs at 10% (App closed before!). Seems I have the same problem as this guy, but calling onClose on all screens does not solve him.

    Screw on the problems to kill a bb - app

    After the crash, the machine stops and restarts. Unless I start the app again once I'm able to reinstall via OTA Download in the external browser. This brings me to some open handles that could be left by my application close. Is it possible to really put an end to the application without the remains? In the following article, I read that there is some app cleaner memory running in the background. A cleaning of my application data can be requested manually when closing?

    Thread mentioning the cleaner memory application

    Article on cleaning memory

    Another approach, that I thought would write a separat Updater app. It would be installed via OTA on the original app (does not affect the files of applications). Then the application closes, the update must be run manually by the user. It will offer an opening to private download ota of the new app problem is still that the download would crash unless the former PPP has been terminated correctly before.

    Any ideas on this subject?

    Rob has soon

    Thanks Jerome,.

    He will give a try.

    Regarding the problem of update: call the external browser works fine, the only thing to be wary of is not to close the app but just ask the application to get to bottom:

    synchronized(UiApplication.getEventLock()) {
        synchronized (PersistentStore.getSynchObject()) {
            UiApplication.getUiApplication().requestBackground();
        }
    }
    

    from the download ota after that works almost perfectly (browser cache needs to be cleared before, cod size is 650 kb).

    Regarding the intelligent programming, I found this:

    Article on installing and updating applications programmatically

  • Can I change the operating with another drive, and then use the recovery CD to restore my operating system on the new drive?

    I have 2 hard drives. I want to replace the smallest disk, which can be found be the operating drive. If I removed and installed another drive and used the recovery CD the I bought with the computer, I'll be able to re - install new hard disk?

    Hello

    Yes, you can change the hard drive and reinstall Vista using recovery disks.

    You may or may not have problems of reactivation.

    If you do, use the telephone option and hold firmly explain to a real person in the situation.

    "How to activate Vista normally and by Activation of the phone '

    http://www.Vistax64.com/tutorials/84488-activate-Vista-phone.html

    See you soon.

  • Trying to download a game that won't play, I asked for help on the game site and they start saying "click on start and then"Run". Try as I might I can't find "run". Any suggestions

    I am unable to find the 'run' which, according to the website of the game should be pinned to my button 'start '. Any ideas?

    I am unable to find the 'run' which, according to the website of the game should be pinned to my button 'start '. Any ideas?

    It is an older version of windows.  In 7 or Vista, click Start, and then use the search box - enter the article they say "run" and press ENTER.

  • How do open Applescript AI can open the file and then run ExtendScript then close?

    Here is the code sample Applescript that I came with the help of the script guide/reference for CS6 and by doing some research online:

    Tell application "Adobe Illustrator"

    activate

    period of 10

    pfilepath the value "/ Users/username/Documents/Temp/someFile.ai".

    the pfile POSIX file pfilepath value

    Open the pfile as alias without options

    period of 10

    do javascript "#include" / Users/username/Documents/Temp/someScript.jsx ".

    delay 1

    quit smoking

    tell the end

    I just want to open the app (example here for, but also pleasant to do same for Photoshop), open a file, then run the ExtendScript file against her, and then close after treatment. With this example script, all that works is the app starts. No file not opened, any script executed, app does not close at the end. I see no error doesn't pop up or anything appear in the command prompt.

    Just ExtendScript sample triggers an alert popup for the test message

    Also note, the Applescript code snippet is executed from Python, but I think where/how it runs shouldn't matter (except perhaps where the error messages may appear). But in the worst case, I'll debug/run through the AppleScript Editor.

    Some tips on what's not here? Or is it technically correct?

    It solved here: MacScripter / POSIX Pass file path to Adobe Illustrator to run ExtendScript

  • Determine the end of the month and then run the script

    Hi all

    OK, I'm stumped. I can get all kinds of data to date... but I did not find a way (or the same example script) that shows how to retrieve the end of the month.


    Why?


    I have a master script that runs as a scheduled task. It runs every day and call other scripts based on the date. It is great for things like weekly reports that I just used $dow = {get-date}. DayofWeek but I need to run scripts at the end of the month.

    I can something together that gets the day, and then performs a logical ugly pop-based < or > but I don't know there's a smoother way to do.

    As always, any help greatly appreciated.

    See you soon

    Hello, medalklector-

    One way to do it, it is described by RMToups, Jr. at http://activelydirect.blogspot.com/2011/01/determine-last-day-of-month-in.html.  You should be able to set the output to fit your needs.  How does do for you?

  • can I install a program on a disk, and then run and install on another

    can I do because I want to move some paid to a newer computer

    Hello

    You can't move a program installed to another location / car / computer.

    Programs must be reinstalled from the original (CD, DVD or downloads) media.

    See you soon.

  • Can I use my file of smart noticed lrdata on another Mac and then takes over the changes to the original?

    LR5 CC user. I tried to use Smart saw for the first time. I followed what appeared to be the best advice but now not sure since most of the functionality of guides working with a single computer and offline files. I'm travelling away from more than a month. I have a Mac Pro with thousands of images. I exported my 'best' 5000 smart insights in a lrdata file to use with my macbook pro laptop on a trip. I took the file lrdata with me on a hard drive. Not the file catalog. Now it seems that I can import these files (by double-clicking the lrdata file) but I'm afraid that if I change them editing will be lost when I return to the original. They have different file names. I'm basically losing the chance to do a touch up? Or is it possible to edit and send them to the original catalogue?

    10greenmen wrote:

    import these files (by double-clicking the lrdata file) but I'm afraid that if I change them editing will be lost when I return to the original.

    We should not import & edit the preview files smart (the DNG with loss with names like 7/7FB5/7FB580F4-00BB-4C39-8DE5-C09490BA6CB8.dng), instead of the photos they "represent."

    But if you do, here's how to transfer settings and/or metadata to preview files smart imported/released to the original photos which you have edited in the first place:

    o NOTE: you must import smart previews published directly in your main catalog when you get home (travel catalog import, or save first and just import files xmp - two works in a way).

    * Download and install RelativeAntics (free by me) plugin.

    To choose "Any Smart Previews" pre-set (in the plugin Manager).

    * Select the imported smart preview files.

    * Run 'Relative Antics"(menu file-> Plugin Extras).

    * Unselect all items of ' Define 'Related' section, * except * "Custom Relative."

    * Select whether you want to transfer cause adjustment or metadata...

    * and do - this will copy the metadata and settings of the selected imported/edition-smart-preview to the photo that should be edited in the first place (the "related" photo).

    Example:

    ==========

    Rob

  • I Zoom on the page, but if you go to another page and then return to the initial page, zoom has been reset. This didn't happen with Firefox 3.6.1, the zoom level would remain.

    This only happens with my laptop running windows 7. No problem with my desktop running windows vista.

    You are not running in mode for private browsing in Firefox when that happens, you?

  • In the menu Organizer media pictures will not display. When I import the photos are displayed until I try to display another file and then they are gone?

    I'm lost... I have recently updated to 12 PSE and my files all show them imported but I can't display pictures.

    Click the view menu and make sure you have all types of media choose to display.

  • Resize C: and add the new partition F: and THEN run HP_recovery - will be the F: partition survive?

    Hello!

    I resized C: and added another F: partition to back up my documents, pictures, etc. If I run HP_recovery, will she also shaped the new partition F:?

    I don't want THAT to happen...

    Babby, I don't think that the restore CD or partition will allow for the collection of partitions.  They will create a single partition only.

  • How to create a task and then run it later?

    We have a requirement like this: we insert records into a table. After inserting records, we need a web service ping command and tell him to read the new records.

    We call the WS in a trigger after-insert-for-all-row.

    Can we create employment and in the trigger, use DBMS_SCHEDULER. RUN_JOB ('call_web_service', use_current_session = > 'FALSE') to perform the task?

    When we create jobs, we can use start_date = > null to indicate that we do not want to run the task now, but later?

    Hello

    Yes, you are on the right track. If it is a work intended to be run on demand, then specify a start_date/end_date, nor a repeat_interval.

    You can even create as disabled (enabled-FALSE online).

    Use DBMS_SCHEDULER. RUN_JOB ('call_web_service', use_current_session-online 'FALSE') every time you want to run the task.

  • install new printer Epson WF-2530, will not install scanner drivers error message ' could not launchrequired program during installation, check the installation disc, and then run the installation again.

    having a problem installing drivers for a new Epson printer scanner all in a WF-2530 on my desktop using windows vista

    error message "could not launch the required program during installation, check the installation disc, then re-run the installer.

    I managed to install the software on my laptop using windows 7.

    spending some time with epson tech dept, after several attempts to solve the problem, they advised that it was a problem with the microsoft software.

    have tried to install it on the internet from the site of epson, but same error

    any help would be greatly appreciated

    < have="" tried="" installing="" over="" internet="" from="" epson="" website="" but="" same="">
    Do you mean that you have downloaded the Epson driver?

    http://www.Epson.com/cgi-bin/store/support/supDetail.jsp?BV_UseBVCookie=Yes&infoType=overview&OID=209709

    Try using the wizard adding printer in the Control Panel, printers. In the wizard, select EPSON under manufacturer. If you don't see your printer in the list, click the Windows Update button. It will take several minutes (about 20) be patient, Windows updates the list. You should see a hard drive activity during the update.

    If that wouldn't work, try to run the System File Checker:

    Use the System File Checker tool to troubleshoot missing or corrupted on Windows Vista or Windows 7 system files
    http://support.Microsoft.com/kb/929833?WA=wsignin1.0

Maybe you are looking for