2014-02-11 2 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; 

방법 :하지만이 System.UnauthorizedAccessException 원인

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

: 내 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

그건 내 하루가되었습니다! 감사합니다 Aman! :) – Erik

관련 문제