在后台代理不同的静态变量的值 [英] Static variable value different in background agent

查看:119
本文介绍了在后台代理不同的静态变量的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,显示了一些数据,并启动后台代理dinamically更新现场砖。由于现场的瓷砖在后台代理使用一些变种从主线程填充创建的内容,我决定(也许这是一个错误的决定,但它是唯一一个我以为合理的)写静态变量和主之间的共享属性的类线程和后台代理。
现在的问题是我写在主线程中一个变量的值,但是当后台代理执行发现本null值。为什么呢?结果
我提供了一个小例子,跳跃它足以让你明白。



静态部分

 公共类瓦尔
{
公共静态的IEnumerable<联系与GT;联系人;
公共静态无效测试()
{
INT NUM =联系人== NULL? -2:Contacts.Count();
// num是-2这里是因为通讯录是空!
}
}



后台代理

 公共类TileAgent:ScheduledTaskAgent 
{
保护覆盖无效OnInvoke(ScheduledTask任务)
{
//这是必须使用的BeginInvoke,以避免跨线程错误
Deployment.Current.Dispatcher.BeginInvoke(()=>
{
Vars.Test();
});

NotifyComplete();
}
}



主页

 公共部分类的MainPage:的PhoneApplicationPage 
{
私人无效Contacts_SearchCompleted(对象发件人,ContactsSearchEventArgs E)
{
busyIndi​​cator.IsRunning = FALSE;

{
Vars.Contacts = e.Results
。凡(.........);
ContactResultsData.DataContext = Vars.Contacts;
// Vars.Contacts.Count()= 67在这里!

如果(Vars.GetTile()!= NULL)
StartAgent();
}
赶上(System.Exception的)
{
//这没关系,没有结果
}
}
私人无效ContactResultsData_Tap(对象发件人,GestureEventArgs E)
{
INT NUM = Vars.Contacts == NULL? -2:Contacts.Count();
// NUM = 67在这里!
}
}



这有什么错我的代码?有没有更好的方式来完成我的任务?结果
考虑我的工作在Windows Phone从不到一个月的时间,所以我敢肯定,我还是做的非常糟糕的事情...



更新:结果
把正确的锁,以避免并行读/来自不同线程写入之后,我决定把一个显式静态构造函数以静态类

 公共类瓦尔
{
静态瓦尔()
{
的Debug.WriteLine(INIT);
}
}

和这个叫每次后台代理调用!结果
这说明,我看到我的空变量的原因,但我不明白:为什么一个静态类是重建每次结果
难道是因为后台代理是里面? DLL项目(这是必要的运行它)?结果
有没有一种方法,使一个类,仅创造了第一次,这可以在不同的线程之间共享(它们是在这种情况下处理?)<? / p>

解决方案

长的搜索后,我终于找到了的文章说明:




由于我们的EvenTiles应用及其PeriodicTask在
运行单独的进程,它们彼此完全,
含义分离,它们得到自己的变量副本它们都希望
存取,即使这些变量在一个单独的项目来定义。




所以不可能使用共享主应用程序和定期任务之间的数据简单的静态变量/性能;我们必须读/写数据库或独立存储或什么,我们请。



我觉得这是疯狂的,但是这是这个故事。


I have an app that shows some data and starts a background agent to update live tiles dinamically. Because of live tiles content is created in background agent using some var populated from main thread, I decided (maybe this was a wrong decision, but it's the only one I thought reasonable) to write a class with static variables and properties to share between main thread and background agent. Now the problem is I write a variable value in main thread, but when background agent executes finds this value null. Why?
I provide a small example, hopping it's enough for you to understand.

STATIC PART

public class Vars
{
    public static IEnumerable<Contact> Contacts;
    public static void Test()
    {
        int num = Contacts == null ? -2 : Contacts.Count();
        // num is -2 here because Contacts is null !!
    }
}

BACKGROUND AGENT

public class TileAgent : ScheduledTaskAgent
{
    protected override void OnInvoke(ScheduledTask task)
    {
        // It's necessary to use BeginInvoke to avoid Cross-thread errors
        Deployment.Current.Dispatcher.BeginInvoke(() =>
        {
            Vars.Test();
        });

        NotifyComplete();
    }
}

MAIN PAGE

public partial class MainPage : PhoneApplicationPage
{
    private void Contacts_SearchCompleted(object sender, ContactsSearchEventArgs e)
    {
        busyIndicator.IsRunning = false;
        try
        {
            Vars.Contacts = e.Results
                .Where(.........);
            ContactResultsData.DataContext = Vars.Contacts;
            // Vars.Contacts.Count() = 67 here !!!

            if (Vars.GetTile() != null)
                StartAgent();
        }
        catch (System.Exception)
        {
            // That's okay, no results
        }
    }
    private void ContactResultsData_Tap(object sender, GestureEventArgs e)
    {
        int num = Vars.Contacts == null ? -2 : Contacts.Count();
        // num = 67 here !!
   }
}

What's wrong with my code? Is there a better way to accomplish my task?
Consider I'm working on Windows Phone from less than one month, so I'm sure I'm still doing very bad things...

UPDATE:
After putting correct locks to avoid concurrent read/write from different threads, I decided to put an explicit static constructor to static class

public class Vars
{
    static Vars()
    {
        Debug.WriteLine("init");
    }
}

and this is called everytime background agent is invoked!!
This explains the reason I see my variable as null, but I don't understand: why a static class is recreated every time?
Could it be because background agent is inside a dll project (it's necessary to run it)?
Is there a way to make a single class, created only first time, that can be shared among different threads (are they processes in this situation?)?

解决方案

After a long search, I finally found an article stating:

Since our EvenTiles application and its PeriodicTask are running in separate processes, they are completely separated from each other, meaning that they get their own copies of variables they both want to access, even though these variables are defined in a separate project.

So it's impossible to share data between main app and periodic task using "simple" static variables/properties; we must read/write a database or the isolated storage or whatever we please.

I find this crazy, but this is the story.

这篇关于在后台代理不同的静态变量的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆