Share data between main app and periodic task

前端 未结 4 1064
一个人的身影
一个人的身影 2020-12-10 16:01

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

4条回答
  •  半阙折子戏
    2020-12-10 16:23

    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.

提交回复
热议问题