在后台运行方法

时间:2014-02-11 14:21:28

标签: c# asynchronous windows-phone-8 background windows-phone

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

简单地称之为:

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中的无线跨线程访问

1 个答案:

答案 0 :(得分:0)

您可以使用后台工作线程并将方法调用放在其中

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

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

您的例外补救措施

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

请看一下这篇msdn文章 和一篇more文章。