Skip to main content

Microsoft Silverlight

Answered Question Call webService in Timer event?RSS Feed

(0)

yelong
yelong

Member

Member

22 points

22 Posts

Call webService in Timer event?

got the error: Invalid cross-thread access 

any help?

thanks 

jackbond
jackbond

Contributor

Contributor

2806 points

722 Posts

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.

yelong
yelong

Member

Member

22 points

22 Posts

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.

 
 

jackbond
jackbond

Contributor

Contributor

2806 points

722 Posts

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?

Nokola
Nokola

Member

Member

66 points

13 Posts

Microsoft

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

yelong
yelong

Member

Member

22 points

22 Posts

Re: Re: Call webService in Timer event?

how to call from ui thread? 

thanks, 

Nokola
Nokola

Member

Member

66 points

13 Posts

Microsoft

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

yelong
yelong

Member

Member

22 points

22 Posts

Re: Re: Call webService in Timer event?

 this is a good one. using Action instead backgroundworker.

 thank you, Nokola.
 

jackbond
jackbond

Contributor

Contributor

2806 points

722 Posts

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?

yelong
yelong

Member

Member

22 points

22 Posts

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,
 

Nokola
Nokola

Member

Member

66 points

13 Posts

Microsoft
Answered Question

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

jackbond
jackbond

Contributor

Contributor

2806 points

722 Posts

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.

Nokola
Nokola

Member

Member

66 points

13 Posts

Microsoft

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

jackbond
jackbond

Contributor

Contributor

2806 points

722 Posts

Re: Call webService in Timer event?

Nokola:
hope you don't mind

Not at all, thanks for clarifying.

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities