I am posting this specific question after another which I could not solve.
In short: even if I create a static class (with static options and / or properties), the main application and the background agent do not use the same static class, but both create a new instance; therefore, it is not possible to exchange data between these projects.
To check this:
- Create a new Windows Phone app (called AppTest )
- Add a ScheduledTask project (called Agent )
- In AppTest, put a link to the Agent project
- Create a new Windows Phone Library project (called Shared )
- Both in AppTest and Agent put a link to the General project.
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 the main application, you can see that the static constructor for the static class is called (this is correct), but when the agent is called, Vars not used, but the constructor is called at another time, therefore creating another instance.
What for? How can I exchange data between the main application and the background agent?
I already tried putting the Vars class in the agent class and namespace, but the behavior is the same.
c # windows-phone-7
Marco
source share