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

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

问题描述

我发布后的other 之一我没能解决。

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

简单地说:即使我创建一个静态类(静态VAR和/或属性),主要的应用程序和后台代理不使用同一个静态类,但两者创建它的一个新的实例;所以这是不可能的,这些项目之间共享数据!

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 把一个参考项目的代理

  • 创建一个新的Windows Phone库项目(所谓的共享

  • 无论是在 AppTest 代理把一个参考项目的共享

  • 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

然后用这个基本的测试code。

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;
    }
}

在调试的主要应用程序,你可以看到静态类的静态构造函数被调用(这是正确的),但是当代理调用瓦尔不是拿来主义,但构造函数被调用另一个时间,所以创建一个不同的实例。结果
为什么?我如何共享主应用程序和后台代理之间的数据?结果
我已经试图把瓦尔类代理类和命名空间,但行为是一样的。

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天全站免登陆