Page view counter
Call webService in Timer event?
Last post 03-13-2008 6:19 PM by jackbond. 13 replies.
Sort Posts:
03-13-2008 12:37 PM
Call webService in Timer event?

got the error: Invalid cross-thread access 

any help?

thanks 

yelong

Loading...
Joined on 06-28-2007
Posts 21
03-13-2008 4:04 PM
Re: Call webService in Timer event?

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.

Jack Bond

Khet - The first Silverlight multiplayer game

Zork I: The Great Underground Empire

jackbond

Loading...
Joined on 03-21-2007
Posts 429
03-13-2008 4:12 PM
Re: Call webService in Timer event?

 thanks,

 i got another workaround.

add a backgroundworker, timer calls this backgroundworker. the backgroundworker's completed event calls webservice function.

 
 

yelong

Loading...
Joined on 06-28-2007
Posts 21
03-13-2008 4:55 PM
Re: Call webService in Timer event?

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?

Jack Bond

Khet - The first Silverlight multiplayer game

Zork I: The Great Underground Empire

jackbond

Loading...
Joined on 03-21-2007
Posts 429
03-13-2008 4:59 PM
Re: Call webService in Timer event?

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);

Timer _timer = new Timer(new TimerCallback(delegate(object action)                    { Dispatcher.BeginInvoke(_onTimerAction); }), null, 0, 3000); private void 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

 

-------
Please mark this post as answer if it answers your question!
Nikola - MSFT
http://blogs.msdn.com/nikola

Nokola

Loading...
Joined on 03-07-2008
Posts 13
03-13-2008 5:01 PM
Re: Re: Call webService in Timer event?

how to call from ui thread? 

thanks, 

yelong

Loading...
Joined on 06-28-2007
Posts 21
03-13-2008 5:09 PM
Re: Re: Call webService in Timer event?

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.

 

-------
Please mark this post as answer if it answers your question!
Nikola - MSFT
http://blogs.msdn.com/nikola

Nokola

Loading...
Joined on 03-07-2008
Posts 13
03-13-2008 5:09 PM
Re: Re: Call webService in Timer event?

 this is a good one. using Action instead backgroundworker.

 thank you, Nokola.
 

yelong

Loading...
Joined on 06-28-2007
Posts 21
03-13-2008 5:30 PM
Re: Call webService in Timer event?

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?

Jack Bond

Khet - The first Silverlight multiplayer game

Zork I: The Great Underground Empire

jackbond

Loading...
Joined on 03-21-2007
Posts 429
03-13-2008 5:39 PM
Re: Re: Call webService in Timer event?

i'm not a SL expert. i didn't know DispatcherTimer Embarrassed . now i'm using it.

 thanks,
 

yelong

Loading...
Joined on 06-28-2007
Posts 21
03-13-2008 5:39 PM
Marked as Answer
Re: Call webService in Timer event?

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.

 

-------
Please mark this post as answer if it answers your question!
Nikola - MSFT
http://blogs.msdn.com/nikola

Nokola

Loading...
Joined on 03-07-2008
Posts 13
03-13-2008 5:50 PM
Re: Call webService in Timer event?

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.

Jack Bond

Khet - The first Silverlight multiplayer game

Zork I: The Great Underground Empire

jackbond

Loading...
Joined on 03-21-2007
Posts 429
03-13-2008 6:03 PM
Re: Call webService in Timer event?

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,

 

-------
Please mark this post as answer if it answers your question!
Nikola - MSFT
http://blogs.msdn.com/nikola

Nokola

Loading...
Joined on 03-07-2008
Posts 13
03-13-2008 6:19 PM
Re: Call webService in Timer event?

Nokola:
hope you don't mind

Not at all, thanks for clarifying.

Jack Bond

Khet - The first Silverlight multiplayer game

Zork I: The Great Underground Empire

jackbond

Loading...
Joined on 03-21-2007
Posts 429
Microsoft Communities