2014-02-11 54 views
0

我正在开发一个应用程序,其中应用程序会向用户提问一个问题,其中有几个 - 例如询问用户是否要评估应用程序。我需要运行此方法,但它大大增加了应用程序启动时间。我怎样才能在后台运行这个?我检查了堆栈溢出的其他问题没有太多的帮助。在后台运行方法

简称为::

checkUserStats(); 

方法:

private void checkUserStats() 
{ 
    // Load settings from IsolatedStorage first 
    try 
    { 
     userRemindedOfRating = Convert.ToBoolean(settings["userRemindedOfRating"].ToString()); 
    } 
    catch (Exception) 
    { 
     userRemindedOfRating = false; 
    } 

    try 
    { 
     wantsAndroidApp = Convert.ToBoolean(settings["wantsAndroidApp"].ToString()); 
    } 
    catch (Exception) 
    { 
     wantsAndroidApp = false; 
    } 

    //Check if the user has added more 3 notes, if so - remind the user to rate the app 
    if (mainListBox.Items.Count.Equals(4)) 
    { 
     //Now check if the user has been reminded before 
     if (userRemindedOfRating.Equals(false)) 
     { 
      //Ask the user if he/she wants to rate the app 
      var ratePrompt = new MessagePrompt 
      { 
       Title = "Hi!", 
       Message = "I See you've used the app a little now, would u consider doing a review in the store? It helps a lot! Thanks!:)" 
      }; 
      ratePrompt.IsCancelVisible = true; 
      ratePrompt.Completed += ratePrompt_Completed; 
      ratePrompt.Show(); 

      //Save the newly edited settings 
      try 
      { 
       settings.Add("userRemindedOfRating", true); 
       settings.Save(); 
      } 
      catch (Exception) 
      { 
      } 

      //Update the in-memory boolean 
      userRemindedOfRating = true; 
     } 
     else if (userRemindedOfRating.Equals(true)) 
     { 
      // Do nothing 
     } 
    } 
    else 
    { 
    } 

    // Ask the user if he/she would like an android app 
    if (wantsAndroidApp.Equals(false)) 
    { 
     // We haven't asked the user yet, ask him/her 
     var androidPrompt = new MessagePrompt 
     { 
      Title = "Question about platforms", 
      Message = "Hi! I just wondered if you wanted to have this app for android? If so, please just send me a quick email. If enough people wants it, I'll create it:)" 
     }; 
     androidPrompt.IsCancelVisible = true; 
     androidPrompt.Completed += androidPrompt_Completed; 
     androidPrompt.Show(); 

     //Save the newly edited settings 
     try 
     { 
      settings.Add("wantsAndroidApp", true); 
      settings.Save(); 
     } 
     catch (Exception) 
     { 
     } 

     //Update the in-memory boolean 
     wantsAndroidApp = true; 
    } 
    else if (wantsAndroidApp.Equals(true)) 
    { 
     // We have asked the user already, do nothing 
    } 
} 

我现在尝试这样做:在后台运行需要的方法

使用:

using System.ComponentModel; 

声明:

BackgroundWorker worker; 

初始化:

worker = new BackgroundWorker(); 
worker.DoWork+=worker_DoWork; 

方法:

private void worker_DoWork(object sender, DoWorkEventArgs e) 
{ 
    checkUserStats(); 
} 

但它会导致System.UnauthorizedAccessException的:无效在我app.xaml.cs跨线程访问

回答

0

你可以使用后台工作线程并将你的方法调用放入它里面

'Silverlight BackgroundWorker类提供了一种在后台线程上运行耗时操作的简单方法。 BackgroundWorker类使您能够检查操作的状态,并让您取消操作。 当您使用BackgroundWorker类时,您可以在Silverlight用户界面中指示操作进度,完成和取消。例如,您可以检查后台操作是完成还是取消,并向用户显示消息。'

你基本上只需要初始化一个backgroundworker对象并订阅它的DoWork事件。

并为您的异常

private void worker_DoWork(object sender, DoWorkEventArgs e) 
    { 
     Dispatcher.BeginInvoke(() => 
     { 
     checkUserStats(); 
     }); 
    } 

补救措施只是给看看这个msdn文章 和一个more文章。

+0

查看更新的问题请 – Erik

+0

检查答案编辑 –

+0

这使我的一天!谢谢阿曼!:) – Erik