在主应用程序和周期性任务之间共享数据 [英] Share data between main app and periodic task

查看:18
本文介绍了在主应用程序和周期性任务之间共享数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在另一个之后发布了这个特定问题无法解决.

I post this specific question after the other one I wasn't able to solve.

简而言之:即使我创建了一个静态类(带有静态变量和/或属性),主应用程序和后台代理也不使用相同的静态类,而是都创建了它的一个新实例;所以不可能在这些项目之间共享数据!!

Briefly: even if I create a static class (with static vars and/or properties), main app and background agent don't use the same static class, but both create a new instance of it; so it's impossible to share data between these projects!!

测试:

  • 创建一个新的 Windows Phone 应用程序(称为 AppTest)
  • 添加 ScheduledTask 项目(称为代理)
  • AppTest中引用项目Agent
  • 创建一个新的 Windows Phone 库项目(称为共享)
  • AppTestAgent 都引用了项目 Shared
  • Create a new Windows Phone application (called AppTest)
  • Add a ScheduledTask project (called Agent)
  • In AppTest put a reference to project Agent
  • Create a new Windows Phone Library project (called Shared)
  • Both in AppTest and Agent put a reference to project Shared

然后使用这个基本的测试代码.

Then use this basic test code.

AppTest

private readonly string taskName = "mytest";
PeriodicTask periodicTask = null;

public MainPage()
{
    InitializeComponent();

    Vars.Apps.Add("pluto");
    Vars.Order = 5;

    StartAgent();
}

private void RemoveTask()
{
    try
    {
        ScheduledActionService.Remove(taskName);
    }
    catch (Exception)
    {
    }
}
private void StartAgent()
{
    periodicTask = ScheduledActionService.Find(taskName) as PeriodicTask;
    if (periodicTask != null)
        RemoveTask();
    periodicTask = new PeriodicTask(taskName)
    {
        Description = "test",
        ExpirationTime = DateTime.Now.AddDays(14)
    };

    try
    {
        ScheduledActionService.Add(periodicTask);
        ScheduledActionService.LaunchForTest(taskName, 
                TimeSpan.FromSeconds(10));
    }
    catch (InvalidOperationException exception)
    {
    }
    catch (SchedulerServiceException)
    {
    }
}

代理

protected override void OnInvoke(ScheduledTask task)
{
    if (Vars.Apps.Count > 0) 
        Vars.Order = 1;

    NotifyComplete();
}

共享

public static class Vars
{
    public static List<string> Apps = null;
    public static int Order;

    static Vars()
    {
        Apps = new List<string>();
        Order = -1;
    }
}

当您调试主应用程序时,您可以看到调用了静态类的静态构造函数(这是正确的),但是当调用代理时 Vars 未使用",而是再次调用构造函数,所以创建一个不同的实例.
为什么?如何在主应用程序和后台代理之间共享数据?
我已经尝试将 Vars 类放在代理类和命名空间中,但行为是相同的.

When you debug main app you can see that static constructor for static class is invoked (this is correct), but when agent is invoked Vars is not "used" but constructor is invoked another time, so creating a different instance.
Why? How can I share data between main app and background agent?
I've already tried to put Vars class in agent class and namespace, but the behaviour is the same.

推荐答案

静态变量的值根据加载的 应用程序域,它是您正在运行的进程的子集".因此,每个 AppDomain 的静态变量具有不同的值,因此每个运行的进程也具有不同的值.

Values of static variables are 'instanced' per loaded App Domain, which is a 'subset' of your running process. So static variables have different values per AppDomain, and therefore also per running process.

如果必须在进程之间共享数据,则需要将其存储在某处(例如数据库),或者需要在进程之间设置一些通信(例如 MSMQ 或 WCF).

If you have to share data between processes, you need either to store it somewhere (e.g. a database), or you need to setup some communication between the processes (e.g. MSMQ or WCF).

希望这会有所帮助.

这篇关于在主应用程序和周期性任务之间共享数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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