How to communicate with two instance of UWP App

92 views Asked by At

I have an uwp app, which can have multiple instances Now if I change something on one instance, other instance need to get the update right away.

We have tried to use Out-Proc AppSerivce but its doesn't seems persistent (AppService connection is killed by Platform frequently).

What could be possible options to achieve that.

1

There are 1 answers

8
Roy Li - MSFT On

Currently, there is no API that designed for such scenario. A workaround is that you could save these updated values in a local file (if the data is large and complicated, convert the data into Json format will be better). And then use a timer to check that file. When the updated value is saved in the file in one instance, other instances could check value and update themself.

Here are the steps which I made a simple sample:

  1. Created a blank multi-instance UWP application
  2. Get the local folder and create a file that used for sharing data.
  3. Create a timer to check the file and read data at fixed intervals.

Here are the codes that I made.

MainPage.Xaml:

    <StackPanel Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <Button Content="Click" Click="Button_Click"/>
    <TextBlock Text="{x:Bind sourceStr,Mode=TwoWay}"/>
   </StackPanel>

MainPage.CS:

  public sealed partial class MainPage : Page,INotifyPropertyChanged
{
    private string _sourceStr;

    public event PropertyChangedEventHandler PropertyChanged;

    public string sourceStr
    {
        get 
        {
            return _sourceStr;
        }

        set 
        {
            if (_sourceStr != value) 
            {
                _sourceStr = value;
                RaisePropertyChanged("sourceStr");
            }
        }
    }

    public void RaisePropertyChanged(string name)
    {
        if (PropertyChanged != null)
            this.PropertyChanged.Invoke(this,new PropertyChangedEventArgs(name));
    }

    private StorageFolder localFolder;
    private StorageFile savedFile;
    private DispatcherTimer dispatcherTimer;

    public MainPage()
    {
        InitializeComponent();
        this.Loaded += MainPage_Loaded;
    }

    private async void MainPage_Loaded(object sender, RoutedEventArgs e)
    {
        localFolder = ApplicationData.Current.LocalFolder;
        savedFile = await localFolder.CreateFileAsync("sample.txt", CreationCollisionOption.ReplaceExisting);
        InitializeTimer();
    }

    private async void Button_Click(object sender, RoutedEventArgs e)
    {
        //change value and update the local file
        sourceStr = "234";
        await UpdateData();
    }


    public void InitializeTimer() 
    {
        dispatcherTimer = new DispatcherTimer();
        dispatcherTimer.Tick += DispatcherTimer_Tick; ;
        dispatcherTimer.Interval = new TimeSpan(0, 0, 1);
        dispatcherTimer.Start();
    }

    private async void DispatcherTimer_Tick(object sender, object e)
    {
       await LoadData();
    }

    public async Task UpdateData() 
    {
        // Save data into file
        // this is a simple value demo. If the data is large and complicated, I'd suggest you use Json format.
        await Windows.Storage.FileIO.WriteTextAsync(savedFile, sourceStr);
    }

    public async Task LoadData() 
    {
        //load data from file
        // this is a simple value demo. If the data is large and complicated, I'd suggest you use Json format.
        
        string str = await Windows.Storage.FileIO.ReadTextAsync(savedFile);

        if (!str.Equals(sourceStr) )
            sourceStr = str;
    }

}