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
MS recommends the following:
Passing information between the foreground application and background agents can be challenging because it is not possible to predict if the agent and the application will run simultaneously. The following are recommended patterns for this.
1.For Periodic and Resource-intensive Agents: Use LINQ 2 SQL or a file in isolated storage that is guarded with a Mutex. For one-direction communication where the foreground application writes and the agent only reads, we recommend using an isolated storage file with a Mutex. We recommend that you do not use IsolatedStorageSettings to communicate between processes because it is possible for the data to become corrupt.
2.For Audio Agents: Store custom data in the Tag property of the AudioTrack class. For notifications from the audio agent to the foreground application, read the Tag property in the PlayStateChanged event handler. To pass data from the foreground application to the audio agent, read the Tag property of the current track in the implementation of the OnPlayStateChanged(BackgroundAudioPlayer, AudioTrack, PlayState) method.
See here: http://msdn.microsoft.com/en-us/library/hh202944(v=vs.92).aspx
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.
Easiest thing is to use Isolated storage. For example, from the main app:
using (Mutex mutex = new Mutex(true, "MyData"))
{
mutex.WaitOne();
try
{
IsolatedStorageSettings.ApplicationSettings["order"] = 5;
}
finally
{
mutex.ReleaseMutex();
}
}
//...
and in the agent:
using (Mutex mutex = new Mutex(true, "MyData"))
{
mutex.WaitOne();
try
{
order = (int)IsolatedStorageSettings.ApplicationSettings["order"];
}
finally
{
mutex.ReleaseMutex();
}
}
// do something with "order" here...
You need to use Process-level synchronization and Mutex to guard against data corruption because the agent and the app are two separate processes and could be doing something with isolated storage at the same time.
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.