title: How to run a task every n minutes
body: <p>I wrote a code to update DDNS which works fine. I now need to run this code every <code>n</code> minutes: how would I go doing that?</p>
  
  <p>I tried using:</p>
  
  <pre><code>while (true)
  {
      this.DoMyMethod();
      Thread.Sleep(TimeSpan.FromMinutes(1));
  }
  </code></pre>
  
  <p>and I am still having some trouble. What is the best way to run this task every <code>n</code> minutes?</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using System.Windows;
  using System.Windows.Controls;
  using System.Windows.Data;
  using System.Windows.Documents;
  using System.Windows.Input;
  using System.Windows.Media;
  using System.Windows.Media.Imaging;
  using System.Windows.Navigation;
  using System.Windows.Shapes;
  using System.IO;
  using System.Net;
  using System.Net.Http;
  using System.Windows.Forms;
  using System.Timers;
  
  namespace GoogleDDNS
  {
      /// &lt;summary&gt;
      /// Interaction logic for MainWindow.xaml
      /// &lt;/summary&gt;
      public partial class MainWindow : Window
      {
          public MainWindow()
          {
              InitializeComponent();
          }
  
          private void Button_Click(object sender, RoutedEventArgs e)
          {
              if (username.Text == "")
              {
                  System.Windows.MessageBox.Show("Please enter the username");
                  username.Focus();
                  return;
              }
              if (password.Text == "")
              {
                  System.Windows.MessageBox.Show("Please enter the password");
                  password.Focus();
                  return;
              }
              if (subdomain.Text == "")
              {
                  System.Windows.MessageBox.Show("Please enter the subdomain");
                  subdomain.Focus();
                  return;
              }
  
              var client = new WebClient { Credentials = new NetworkCredential(username.Text, password.Text) };
              var response = client.DownloadString("https://domains.google.com/nic/update?hostname=" + subdomain.Text);
  
              responseddns.Content = response;
  
              Properties.Settings.Default.usernamesave = username.Text;
              Properties.Settings.Default.passwordsave = password.Text;
              Properties.Settings.Default.subdomainsave = subdomain.Text;
  
              Properties.Settings.Default.Save();
          }
  
          private void Window_Loaded(object sender, RoutedEventArgs e)
          {
              username.Text = Properties.Settings.Default.usernamesave;
              password.Text = Properties.Settings.Default.passwordsave;
              subdomain.Text = Properties.Settings.Default.subdomainsave;
          }
      }
  }
  </code></pre>