0

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.

deba
  • 87
  • 5

1 Answers1

0

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;
    }

}
Roy Li - MSFT
  • 8,043
  • 1
  • 7
  • 13
  • Understood File based polling solution may be one workaround. But there any faster push based or event based mechanism possible ? Or can we use CoreWCF to make it robust ? – deba Dec 01 '22 at 15:38
  • @deba the File solution is just a solution from the UWP side. Using WCF might be possible. You might check these links:https://stackoverflow.com/questions/36632282/how-to-make-a-uwp-that-will-work-with-the-default-wcf-service-application and https://stackoverflow.com/questions/36110365/uwp-with-wcf-service – Roy Li - MSFT Dec 02 '22 at 02:29
  • @deba Have you tried to use WCF to communicate between different instances? – Roy Li - MSFT Dec 09 '22 at 08:54
  • we tried to use Core-WCF Whenever we try to add this connection Service reference in UWP project, we are getting exception below There was an error downloading 'http://localhost:8001/communicator/$metadata'. . . System.NullReferenceException: Object reference not set to an instance of an object. at CoreWCF.Channels.RequestDelegateHandler.HandleRequest(HttpContext context) at CoreWCF.Channels.ServiceModelHttpMiddleware.InvokeAsync(HttpContext context) at CoreWCF.Description.ServiceMetadataExtension.HttpGetImpl.<>c__DisplayClass39_0.<b__0>d.MoveNext() – deba Dec 09 '22 at 09:53
  • this url is directly accesible from chrome browser. So it seems CoreWCF is hosted properly but from UWP App, connection Service reference can not be added – deba Dec 09 '22 at 09:58
  • @deba Have you checked the answer from Sunteen from the link I posted to use normal WCF? – Roy Li - MSFT Dec 09 '22 at 10:18
  • with normal WCF, workaround solution can be made. But we already have one .Net 6 Console app as hybrid solution, this app also need to receive notification as well, for this .NET 6 Console App we can not use normal WCF, – deba Dec 14 '22 at 06:30
  • @deba Well, there is no other ways to do that. We might still need to go back and use the file instead. – Roy Li - MSFT Dec 14 '22 at 07:24