title: Run a background process to change backgroundimage of another page using timer in Xamarin.forms
body: <p>Hi StackOverflow Team,</p>
  <p>I am trying to run a background process in my App. This background process should update just Background image on one of the pages in the App every 15 seconds. So far, I tried to create a timer in the App OnStart() method and update the backgroundimage of the page in the BeginInvokeOnMainThread() method but with no success. Can anyone help me with this?</p>
  <p>My Code -</p>
  <pre><code>    {
  
          private static Stopwatch stopWatch = new Stopwatch();
          private const int defaultTimespan = 20;
          private readonly HomePage homePage;
  
          public App()
          {
              InitializeComponent();
  
              try
              {
  
                  MainPage = new MainPage();
  
                  homePage = new HomePage();
  
              }
              catch(Exception ex)
              {
                  string str = ex.Message;
              }
          }
  
          protected override void OnStart()
          {
              if (!stopWatch.IsRunning)
              {
                  stopWatch.Start();
              }
  
              Device.StartTimer(new TimeSpan(0, 0, 10), () =&gt;
              {
                  // Logic for logging out if the device is inactive for a period of time.
  
                  if (stopWatch.IsRunning &amp;&amp; stopWatch.Elapsed.Seconds &gt;= defaultTimespan)
                  {
                      //prepare to perform your data pull here as we have hit the 1 minute mark   
  
                      // Perform your long running operations here.
  
                      Device.BeginInvokeOnMainThread(() =&gt;
                      {
                          // If you need to do anything with your UI, you need to wrap it in this.
                          //  homePage.BackgroundImageSource = &quot;goldengate.jpg&quot;;
                          homePage.ChangeBackgroundImage();
  
                      });
  
                      stopWatch.Restart();
                  }
  
                //  Always return true as to keep our device timer running.
                  return true;
              });
          }
  
          protected override void OnSleep()
          {
              //stopWatch.Reset();
          }
  
          protected override void OnResume()
          {
              //stopWatch.Start();
          }
          //void ChangeHomePageImage()
          //{
          //    Navigation.PushAsync(new HomePage(appBackground));
          //    Navigation.RemovePage(this);
          //}
  
  
      }
  
  MainPage - 
  &lt;MasterDetailPage xmlns=&quot;http://xamarin.com/schemas/2014/forms&quot;
               xmlns:x=&quot;http://schemas.microsoft.com/winfx/2009/xaml&quot;
               xmlns:d=&quot;http://xamarin.com/schemas/2014/forms/design&quot;
               xmlns:mc=&quot;http://schemas.openxmlformats.org/markup-compatibility/2006&quot;
               mc:Ignorable=&quot;d&quot;
               xmlns:local=&quot;clr-namespace:Excercise.Views&quot;
               x:Class=&quot;Excercise.MainPage&quot; IsPresented=&quot;False&quot;&gt;
  
      &lt;MasterDetailPage.Master&gt;
  
  
          &lt;local:MenuPage x:Name=&quot;menuPage&quot;&gt;&lt;/local:MenuPage&gt;
  
  
      &lt;/MasterDetailPage.Master&gt;
  
      &lt;MasterDetailPage.Detail&gt;
          &lt;NavigationPage&gt;
              &lt;x:Arguments&gt;
          &lt;local:HomePage x:Name=&quot;homePage&quot;&gt;&lt;/local:HomePage&gt;
              &lt;/x:Arguments&gt;
          &lt;/NavigationPage&gt;
      &lt;/MasterDetailPage.Detail&gt;
  &lt;/MasterDetailPage&gt;
  
  HomePage - 
   public partial class HomePage : ContentPage
      {
          private SQLiteAsyncConnection _connection;
          
          public HomePage()
          {
              InitializeComponent();
              //  BindingContext = new HomePageViewModel();
              _connection = DependencyService.Get&lt;ISQLiteDb&gt;().GetConnection();
              loadData(&quot;&quot;);
          }
  
          public HomePage(string BackgroundimgPath)
          {
              InitializeComponent();
              //  BindingContext = new HomePageViewModel();
              _connection = DependencyService.Get&lt;ISQLiteDb&gt;().GetConnection();
              loadData(BackgroundimgPath);
          }
          public HomePage(string City, string LocationKey, string StateID)
          {
              InitializeComponent();
              _connection = DependencyService.Get&lt;ISQLiteDb&gt;().GetConnection();
              // BindingContext = new HomePageViewModel();
              try
              {
                  // Method Calls
              }
              catch (Exception)
              {
                   DisplayAlert(&quot;Error&quot;, &quot;There was an error loading this page.&quot;, &quot;OK&quot;);
              }
          }
  
          protected override void OnAppearing()
          {
              this.Title = App.AppTitle;           
              this.firstStacklayout.Margin = new Thickness(0, (Application.Current.MainPage.Height * 0.25), 0, 0);
              base.OnAppearing();
          }
  
  </code></pre>