Skip to main content
Home Forums Silverlight Programming Programming with .NET - General Call webService in Timer event?
13 replies. Latest Post by jackbond on March 13, 2008.
(0)
yelong
Member
22 points
22 Posts
03-13-2008 12:37 PM |
got the error: Invalid cross-thread access
any help?
thanks
jackbond
Contributor
2806 points
722 Posts
03-13-2008 4:04 PM |
What kind of Timer are you using? You probably should be using a DispatcherTimer. I'm not even sure if you can call web services from a non UI thread, but from the looks of the error you are receiving, you probably can't.
03-13-2008 4:12 PM |
thanks,
i got another workaround.add a backgroundworker, timer calls this backgroundworker. the backgroundworker's completed event calls webservice function.
03-13-2008 4:55 PM |
You're getting the same effect there since the backgroundworker invokes it's completed event on the ui thread. But unless you're actually doing background work, you've added unnecessary complexity to your code. Is your only goal to call a web service on a set interval?
Nokola
66 points
13 Posts
03-13-2008 4:59 PM |
Another way to do it is to use the Dispatcher, so you don't have to create an extra background worker
Action _onTimerAction = new Action(OnTimer);
{
// call web service/update UI here
More info: http://blogs.msdn.com/nikola/archive/2008/03/13/calling-web-services-and-accessing-ui-from-timer-event-in-silverlight.aspx
03-13-2008 5:01 PM |
how to call from ui thread?
03-13-2008 5:09 PM |
yelong: how to call from ui thread? thanks,
Using the Dispatcher, this is one way to do it:
this.Dispatcher.BeginInvoke(new Action(MyFunction));private void MyFunction(){ //... insert code here ... }
"this" is the Page object in this case.
this is a good one. using Action instead backgroundworker.
thank you, Nokola.
03-13-2008 5:30 PM |
Nokola: Action _onTimerAction = new Action(OnTimer);Timer _timer = new Timer(new TimerCallback(delegate(object action) { Dispatcher.BeginInvoke(_onTimerAction); }), null, 0, 3000); private void OnTimer() { // call web service/update UI here }
}
I may be missing something here, but if you want an event to fire on the UI thread, why wouldn't you just use a DispatcherTimer?
03-13-2008 5:39 PM |
i'm not a SL expert. i didn't know DispatcherTimer . now i'm using it.
DispatcherTimer is probably preferred (easier to read) in this case. The Dispatcher.BeginInvoke is the usual "invoke on UI thread' code that I use so it came first to mind.
03-13-2008 5:50 PM |
Nokola:DispatcherTimer is probably preferred (easier to read) in this case.
I saw your post here and your blog entry. They're a little confusing as you are creating a Timer with the sole purpose of invoking on the ui thread, which is basically the DispatcherTimer's job.
03-13-2008 6:03 PM |
jackbond: Nokola:DispatcherTimer is probably preferred (easier to read) in this case. I saw your post here and your blog entry. They're a little confusing as you are creating a Timer with the sole purpose of invoking on the ui thread, which is basically the DispatcherTimer's job.
Thank you for your comment. I agree that DispatcherTimer is better for ontimer events, and also updated this on my blog. btw, I also referenced back to you and this forum for more clarification, hope you don't mind.
DispatcherTimer timer = new DispatcherTimer();timer.Interval = new TimeSpan(1000);timer.Tick += new EventHandler(timer_Tick);timer.Start();
Thanks,
03-13-2008 6:19 PM |
Nokola:hope you don't mind
Not at all, thanks for clarifying.