Solution for “FrameworkDispatcher.Update has not been called exception” in WP XNA


While working with the XNA Framework in Windows Phone 7 application, you might face an exception which says “Invalid Operation Exception” with a message “FrameworkDispatcher.Update has not been called”.

 

This exception generally occurs when you are working with the sound effects. In this post, we will discuss about this exception and the solution to fix it.

 

Problem Statement

While working with the sound effects of XNA Framework in Windows Phone application, you might get an “Invalid Operation Exception” if you didn’t update the Framework Dispatcher properly. Here is the screenshot of the said exception:

 

InvalidOperationException from FrameworkDispatcher.Update

 

The exception details clearly says that the FrameWorkDispatcher.Update() was not called. Here is the exception message for you:

 

FrameworkDispatcher.Update has not been called. Regular FrameworkDispatcher.Update calls are necessary for fire and forget sound effects and framework events to function correctly.

 

The FrameworkDispatcher.Update() method called by the framework automatically if the application implements the “Game” class. The Game.Update() method automatically processes the said method by triggering the event processing in the XNA framework.

 

If you don’t use the “Game” class in an application which uses the XNA Framework, you must have to call the method FrameworkDispatcher.Update() explicitly, otherwise you will get the above exception in your application.

 

Solution

Event based systems place messages in a message queue and keep it for dispatching. XNA framework messages also follows it and places them in a queue which will be processed by the XNA framework. If you come across such exception detailed above, you have to call the FrameworkDispatcher.Update() method once per frame in a timer loop. You can also implement the IApplicationService interface and use the timer Tick event.

 

Here is the sample implementation code for IApplicationService to update the FrameworkDispatcher:

 
public class DispatcherService : IApplicationService
{
    private DispatcherTimer m_dispatcherTimer;
 
    public DispatcherService()
    {
        m_dispatcherTimer = new DispatcherTimer();
        m_dispatcherTimer.Interval = TimeSpan.FromTicks(10000);
        m_dispatcherTimer.Tick += frameworkDispatcherTimer_Tick;
        FrameworkDispatcher.Update();
    }
 
    void frameworkDispatcherTimer_Tick(object sender, EventArgs e)
    {
        FrameworkDispatcher.Update();
    }
 
    void IApplicationService.StartService(ApplicationServiceContext context)
    {
        m_dispatcherTimer.Start();
    }
    void IApplicationService.StopService()
    {
        m_dispatcherTimer.Stop();
    }
}

 

Now, you need to register this service in your application class (either XAML or Code behind). Here is the XAML code that you need to add in your App.xaml file:

 
<Application.ApplicationLifetimeObjects>
    <service:DispatcherService />
</Application.ApplicationLifetimeObjects>

 

I hope that this small tip will help you to fix the exception easily. You might have came to this page by searching the said exception message online. So, stay tuned to my blog for different articles on Technologies like Silverlight, Windows Phone, WPF, XAML, .NET and other stuffs.

 

Don’t forget to connect with me on Twitter and Facebook for technical updates and articles news. Also subscribe to my blog’s Newsletter to get all the updates delivered directly to your inbox. We won’t spam or share your email address as we respect your privacy.


4 comments

  1. My family every time say that I am killing my time here at web, except I know I am getting familiarity daily by reading thes pleasant content.

    regrds,
    A. Chan

    ReplyDelete
  2. Please help me to resolve this query on windows 8 metro.

    We are unable to loop the wav file with out any gap in Windows Phone 8 Metro. Wav file ends and start after 3-4 sec. I need to to loop the same wav file with out any gap in the background. We did the same in WP7 using XNA but in Metro, XNA is not available. Please suggest your valuable thought to resolve this issue.

    ReplyDelete
  3. or you can use edit Application_UnhandledException function



    Public Sub Application_UnhandledException(ByVal sender As Object, ByVal e As ApplicationUnhandledExceptionEventArgs) Handles Me.UnhandledException

    ' Show graphics profiling information while debugging.
    If Diagnostics.Debugger.IsAttached Then
    Diagnostics.Debugger.Break()
    End If


    If e.ExceptionObject Is Nothing OrElse e.ExceptionObject.Message Is Nothing Then
    Return
    End If

    If e.ExceptionObject.ToString().ToLower().Contains("frameworkdispatcher.update") Then
    'Stops the app from force closing
    'everything should continue to work without a problem
    e.Handled = True
    End If

    End Sub

    ReplyDelete
  4. i am getting error on xaml implementation error message "the namespace prefix"service" is not defined ....how to remove error for this

    ReplyDelete


 
© 2008-2014 Kunal-Chowdhury.com | Designed by Kunal Chowdhury
Back to top