Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8202307
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T07:08:10+00:00 2026-06-07T07:08:10+00:00

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

  • 0

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!!

To test it:

  • 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)
    {
    }
}

Agent

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

    NotifyComplete();
}

Shared

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T07:08:13+00:00Added an answer on June 7, 2026 at 7:08 am

    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.

    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).

    Hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

EDIT: This post was originally specific to ASP.NET, but after thinking about it I'm
I've tagged this post as WordPress, but I'm not entirely sure it's WordPress-specific, so
I thought I would post this here not so much as a question but
This is my first time here so I hope I post this question at
I'm not sure if this is the right place to post this question but
Author post-edit: Chosen solution (Original question remains below this box) SUMMARY: You SHOULD NOT
Hello all! This is my first post on stackoverflow. After hours of searching and
Not sure if this is the proper place to post this question, but I've
I have a pretty specific one here. I'm basically Trying to post an array
I post this previously in Adobe Forum but haven't got any answers so far.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.