Skip to main content

Microsoft Silverlight

Answered Question Dispatcher.BeginInvoke error in App.xaml fileRSS Feed

(0)

parimaln
parimaln

Member

Member

120 points

100 Posts

Dispatcher.BeginInvoke error in App.xaml file

Hi,

I wish to read xml files available in my Silverlight Web hosting project in the App.xaml file and then call the Page.xaml file once reading is done using web client. But i am getting error "Invalid cross-thread access". This approach was working fine in Beta 1. I have to read all the xml files before page SL pages are displayed. My code in App.xaml.cs file:

public App()

{

this.Startup += this.Application_Startup;

this.Exit += this.Application_Exit;

this.UnhandledException += this.Application_UnhandledException;

ThreadStart start = new ThreadStart(NonUIWork);

Thread thread = new Thread(start);

thread.Start();

InitializeComponent();

}

void NonUIWork()

{

//BELOW LINE GIVES ME AN ERROR: Invalid cross thread access

this.RootVisual.Dispatcher.BeginInvoke(() =>

{

DoSomething();

}

);

}

void DoSomething()

{

HttpWebRequest xmlCommonData = (HttpWebRequest)WebRequest.Create(new Uri(GetAppPath() + "XML/Common.xml"));xmlCommonData.BeginGetResponse(new AsyncCallback(CommonDataResponseCallBack), xmlCommonData);

}

.......

 

regards,

Parimal

Tomek Kmiecik
Tomek Km...

Member

Member

356 points

69 Posts

Microsoft

Re: Dispatcher.BeginInvoke error in App.xaml file

Hey,

The exception is thrown from RootVisual getter (the RootVisual property also cannot be accessed from non-UI thread). The solution is to store the dispatcher somewhere before the second thread is started:

private Dispatcher _dispatcher; 
public App()
{
    this.Startup += this.Application_Startup;
    this.Exit += this.Application_Exit;
    this.UnhandledException += this.Application_UnhandledException;
    
   
InitializeComponent();
    
   
ThreadStart start = new ThreadStart(NonUIWork);
    Thread thread = new Thread(start);
    thread.Start();

  
_dispatcher = RootVisual.Dispatcher;
}

void
NonUIWork()
{
   _dispatcher.BeginInvoke(() => {  });
}

Regards,
Tomek

parimaln
parimaln

Member

Member

120 points

100 Posts

Re: Dispatcher.BeginInvoke error in App.xaml file

Thanks for the response.

In which namespace i can get the Dispatcher object as it is not resolving the App.xaml.cs file?

regards,

Parimal

Tomek Kmiecik
Tomek Km...

Member

Member

356 points

69 Posts

Microsoft

Re: Dispatcher.BeginInvoke error in App.xaml file

It's in System.Windows.Threading. Useful tip - when you type class name, press Ctrl + . (dot), and Visual Studio will display menu allows to add approriate using automatically.

Regards,
Tomek

parimaln
parimaln

Member

Member

120 points

100 Posts

Re: Dispatcher.BeginInvoke error in App.xaml file

Thanks for the response once again.

I wish to call Page.xaml from App.xaml.cs file. But before that i wish to read all the xml files in my app using web client. Where [method] should i call the Page class in the App.xaml.cs file so that it gets called after i finish my XML reading. If i put it in DoSomething method, it gets called first that the App_Startup method. If i put it in App_Starup event, it calls first here also. I wish to call the "

this.RootVisual = new Page();" method after i finish my xml reading in app.xaml.cs file?

regards,

Parimal

Tomek Kmiecik
Tomek Km...

Member

Member

356 points

69 Posts

Microsoft
Answered Question

Re: Dispatcher.BeginInvoke error in App.xaml file

Hey,

I think that the simplest solution in your case is to call the web method from the UI thread, especially that you are using async method anyway (HttpWebRequest.BeginGetResponse(...)). Have you considered creating the page in the CommonDataResponseCallBack?

Regards,
Tomek

paul_houle
paul_houle

Member

Member

45 points

57 Posts

Re: Dispatcher.BeginInvoke error in App.xaml file

SL2B2 runs comm callbacks in threads that are drawn from a thread pool -- it doesn't make any difference what thread you begin the request from.  The following article talks about effective patterns for using the Dispatcher object:

 http://gen5.info/q/2008/06/25/getting-back-to-the-ui-thread-in-silverlight-2/

vplusplus
vplusplus

Member

Member

10 points

12 Posts

Re: Re: Dispatcher.BeginInvoke error in App.xaml file

Try System.Windows.Deployment.Current.Dispatcher.BeginInvoke(...)

sunrunner100
sunrunne...

Member

Member

5 points

8 Posts

Re: Re: Dispatcher.BeginInvoke error in App.xaml file

Thanks a ton vplusplus!

mbaocha
mbaocha

Member

Member

14 points

7 Posts

Re: Dispatcher.BeginInvoke error in App.xaml file

These appears to be a permission issue. What version of IIS are you using?

 

 

 

__________________________
Normal 0 false false false MicrosoftInternetExplorer4 /* Style Definitions */ table.MsoNormalTable {mso-style-name:"Table Normal"; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-parent:""; mso-padding-alt:0in 5.4pt 0in 5.4pt; mso-para-margin:0in; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Times New Roman"; mso-ansi-language:#0400; mso-fareast-language:#0400; mso-bidi-language:#0400;}

Cheap Affordable Web Hosting | Web  Hosting Nigeria | Best Web Design Company


  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities