Hey guys, First off, am I correct in assuming that the browser loads pages using a single thread i.e. if you write a big for loop in javascript every other operation will hang until the loop completes. Silverlight has a System.Threading namespace, how many
threads can I create with it? In one of the Mix session videos I heard that Silverlight uses the browser networking stack for any communication with the server. What are the limitations other than no support for cross-domain access. At this point I am waiting
for the Orcas Beta 1 download to complete, just can't wait to get started on Silverlight.
As you noticed, the System.Threading namespace is nearly 100% exposed in the 1.1 Silverlight alpha. As currently exposed, there are no limits on the number of threads that can be created by System.Threading except the normal memory limitations of the OS.
I am one of the software developers working on the final design of System.Threading and it has been somewhat difficult to filter what will be exposed in System.Threading when we consider that the client applications will be running in the browser and a number
of operations (such as drawing the UI) can only reliably occur from a particular thread. This forces a complex code-pattern that neither the System.Threading classes nor the classes that allow browser or UI manipulation do not currently 100% enforce. For now,
your best bet is to try to write your code in multiple threads (if it makes sense for your app), but be aware that some operations may need to be called from the original thread or controlled by some type of synchronization primitive to ensure that the engine's
call to the browser does not expose an underlying thread-safety issue.
I envision some extremely powerful games that could be written in the browser by using Silverlight to implement clever multithreaded processing to track game-state and/or AI.
On the subject of threading, I am trying to create a code driven animation using the System.Threading.Timer class. The problem is Timer callbacks run from another thread, so when I try to move and element I am getting "Invalid Cross-Thread access".
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Threading;
namespace SilverlightProject1
{
public class Page : Canvas
{
Ellipse elpBall;
private Timer timer;
public void Page_Loaded(object o, EventArgs e)
{
elpBall = this.FindName("elpBall") as Ellipse;
More functionality to allow this is planned and should be there in releases before RTM, but I believe I can give you an idea of code soemthing to make this work before some more functionality is included.
In this alpha, try creating a timeline and check the time elapsed each time the callback occurs and do your work there.
For the final released product, we hope to deliver a more complete and easier way to do this.
Another option could be to use the System.Windows.Browser.HtmlTimer in System.Silverlight.dll.
HtmlTimer is not a high resolution timer (and you would get the warning below at compile time), but it might just do for what you need.
'System.Windows.Browser.HtmlTimer' is obsolete: 'This is not a high resolution timer and is not suitable for short-interval animations. A new timer type will be available in a future release.
This posting is provided "AS IS" with no warranties, and confers no rights.
There isn't a way to sync an operation with the UI thread currently in 1.1 Alpha and Silverlight is not thread safe so we don't allow users to modify on the non UI thread.
Using the HtmlTimer is a good alternative or use the animation/storyboard approach.
-mark
Program Manager
Microsoft
This post is provided "as-is"
this is NOT an accurate approach but just an example of periodically updating the UI thread. To get an accurate timer you should use the ACTUAL System.Threading.Timer class to change values and only use timelines to update the UI with newer values.
For example:
vaibhavk
0 Points
2 Posts
Threading in Silverlight
May 03, 2007 10:51 PM | LINK
Hey guys, First off, am I correct in assuming that the browser loads pages using a single thread i.e. if you write a big for loop in javascript every other operation will hang until the loop completes. Silverlight has a System.Threading namespace, how many threads can I create with it? In one of the Mix session videos I heard that Silverlight uses the browser networking stack for any communication with the server. What are the limitations other than no support for cross-domain access. At this point I am waiting for the Orcas Beta 1 download to complete, just can't wait to get started on Silverlight.
Thanks,
Vaibhav
toddreifsteck
Member
24 Points
2 Posts
Re: Threading in Silverlight
May 04, 2007 01:39 AM | LINK
As you noticed, the System.Threading namespace is nearly 100% exposed in the 1.1 Silverlight alpha. As currently exposed, there are no limits on the number of threads that can be created by System.Threading except the normal memory limitations of the OS.
I am one of the software developers working on the final design of System.Threading and it has been somewhat difficult to filter what will be exposed in System.Threading when we consider that the client applications will be running in the browser and a number of operations (such as drawing the UI) can only reliably occur from a particular thread. This forces a complex code-pattern that neither the System.Threading classes nor the classes that allow browser or UI manipulation do not currently 100% enforce. For now, your best bet is to try to write your code in multiple threads (if it makes sense for your app), but be aware that some operations may need to be called from the original thread or controlled by some type of synchronization primitive to ensure that the engine's call to the browser does not expose an underlying thread-safety issue.
I envision some extremely powerful games that could be written in the browser by using Silverlight to implement clever multithreaded processing to track game-state and/or AI.
Hope that helps!
Todd
Harvey
Member
34 Points
15 Posts
Re: Threading in Silverlight
May 04, 2007 03:18 AM | LINK
Hi Guys,
On the subject of threading, I am trying to create a code driven animation using the System.Threading.Timer class. The problem is Timer callbacks run from another thread, so when I try to move and element I am getting "Invalid Cross-Thread access".
Is there a way to create a thread safe heartbeat?
Here is some sample code to replicate the issue
------- XAML-----------
<Canvas x:Name="parentCanvas"
xmlns="http://schemas.microsoft.com/client/2007"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Loaded="Page_Loaded"
x:Class="SilverlightProject1.Page;assembly=ClientBin/SilverlightProject1.dll"
Width="640"
Height="480"
Background="White"
>
<Ellipse Canvas.Top="100" Canvas.Left="100" Fill="Black" Width="10" Height="10" x:Name="elpBall"></Ellipse>
</Canvas>
-----------------------------
------------------- Code------------------
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Threading;
namespace SilverlightProject1
{
public class Page : Canvas
{
Ellipse elpBall;
private Timer timer;
public void Page_Loaded(object o, EventArgs e)
{
elpBall = this.FindName("elpBall") as Ellipse;
timer = new Timer(Move, null, 1, -1);
}
private void Move(Object state)
{
this.elpBall.SetValue(Canvas.LeftProperty, 5);
}
}
}
-------------------------------------
Cheers,
Javier
toddreifsteck
Member
24 Points
2 Posts
Re: Threading in Silverlight
May 04, 2007 04:17 PM | LINK
More functionality to allow this is planned and should be there in releases before RTM, but I believe I can give you an idea of code soemthing to make this work before some more functionality is included.
In this alpha, try creating a timeline and check the time elapsed each time the callback occurs and do your work there.
For the final released product, we hope to deliver a more complete and easier way to do this.
Todd Reifsteck, Microsoft
gstasa
Member
12 Points
6 Posts
Re: Threading in Silverlight
May 04, 2007 09:31 PM | LINK
Another option could be to use the System.Windows.Browser.HtmlTimer in System.Silverlight.dll.
HtmlTimer is not a high resolution timer (and you would get the warning below at compile time), but it might just do for what you need.
'System.Windows.Browser.HtmlTimer' is obsolete: 'This is not a high resolution timer and is not suitable for short-interval animations. A new timer type will be available in a future release.
Jerod Moemeka
Member
12 Points
7 Posts
Re: Threading in Silverlight
May 22, 2007 09:37 PM | LINK
Hi,
the HTML Timer won't work. To update the UI thread (or use timers in general) do the following:
1) Add an empty timeline to your silverlight alpha project
2) in the page load event handler, give the timeline a 1 second duration (or whatever granularity you desire).Also handle the completed event.
3) In the completed event do whatever you want then begin the timer again.
EXAMPLE
=========================
void Page_Load(object o, EventArgs args){
this.myTimer.Duration = new Duration(new TimeSpan(0, 0, 1)); this.myTimer.Completed += new EventHandler(myTimer_Completed); this.myTimer.Begin();}
void myTimer_Completed(object sender, EventArgs e){
txtCount.Text = i.ToString();
i++;
myTimer.Begin();
}
You can optionaly call begin BEFORE doing stuff. Investigate Critical Sections first.
Mark Rideout
Contributor
2736 Points
307 Posts
Microsoft
Re: Threading in Silverlight
May 22, 2007 10:11 PM | LINK
There isn't a way to sync an operation with the UI thread currently in 1.1 Alpha and Silverlight is not thread safe so we don't allow users to modify on the non UI thread.
Using the HtmlTimer is a good alternative or use the animation/storyboard approach.
-mark
Program Manager
Microsoft
This post is provided "as-is"
Jerod Moemeka
Member
12 Points
7 Posts
Re: Threading in Silverlight
May 22, 2007 10:21 PM | LINK
FYI,
this is NOT an accurate approach but just an example of periodically updating the UI thread. To get an accurate timer you should use the ACTUAL System.Threading.Timer class to change values and only use timelines to update the UI with newer values. For example:
void Callback(object obj){
try{
i++;
}
catch (Exception ex){
}
}
xlj1000
Member
26 Points
13 Posts
Re: Threading in Silverlight
Jun 11, 2007 08:41 PM | LINK
What about the case in calling web service aynschrounously? Is it safe to modify UI in the callback?
Thanks
Jerod Moemeka
Member
12 Points
7 Posts
Re: Threading in Silverlight
Jun 25, 2007 04:13 AM | LINK
It still wont work. you'll have to use the same strategy.