Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi,

I would like to understand how would it be possible to use threads with bing maps.
The task at hand is that of making a polygon (shape) blink and at the same time keep the browser responding for the user to navigate through the map.

At my attempt to make the polygon blink (using client side code [now I know that javascript is single threaded but I still can't figure out a solution]), I ended up freezing the whole browser.

Thanks for any help guys,
J
Posted

1 solution

For a proper answer, you'd have to post what code you're using already. However, in general, you can avoid freezing the UI by using setTimeout, which you can read about here: http://ejohn.org/blog/how-javascript-timers-work/.

Basically, do a small bit of work, then call setTimeout so the UI can take control for a few milliseconds before you resume doing more work. Repeat that and you can simulate multiple threads.

EDIT: Here's an example (a number will increment every 30 milliseconds using a timeout rather than a loop):
HTML
<html>
<head>
  <title>Timer</title>
</head>
<body>
  <div id="info">0</div>
  <script type="text/javascript">
    // Do not pollute the global scope.
    (function() {
      var val = 0;
      var fnRepeat;
      fnRepeat = function() {
        val = val + 1;
        var infoElement = document.getElementById("info");
        infoElement.removeChild(infoElement.childNodes[0]);
        infoElement.appendChild(document.createTextNode(val.toString()));
        setTimeout(fnRepeat, 30);
      };
      fnRepeat();
    })();
  </script>
</body>
</html>


The same concept can be applied for any operation you would typically perform in a loop.
 
Share this answer
 
v2
Comments
Jukatzu 22-Jan-13 17:56pm    
Thanks for the quick reply Sir.

I do not wish to include any code since this is a personal project. However, for you to understand me better, let me give an identical scenario. Let's say I want to make a circle move continuously on the map along the x-axis. At the same time I want to be able to travel around the world with my cursor and zoom, pan...whatever. If I put in a while loop for the circle to keep moving left, how would I be able to re-gain control over the map at the same time? I have seen the link you gave me, but that speaks of one process waiting for the other, whereas in my case I do not wish to wait. One process has to run infinitely whilst the other allows user control in real-time. Hopes this explains better.

Thanks again for your time!
AspDotNetDev 22-Jan-13 18:18pm    
I updated my answer to show you an example.
Jukatzu 22-Jan-13 18:23pm    
Thank you! I will apply it to my case and see what happens. I can see what you mean better now. Will get back to you soon.

Much obliged.
Jukatzu 23-Jan-13 18:36pm    
Worked exactly as intended.
Should have thought of using the function recursively stupid me.
However I am very grateful for your help.
AspDotNetDev 23-Jan-13 19:12pm    
I'm glad to hear it worked for you :-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900