none
[Wpf] [C#] i have a simple question

    Question

  • in a wpf app where can i put a loop that just starts as soon as the app starts. for example if i want to have a number:

    int i = 0;

    and want that number to increase by 1 every 10 secounds

    for (int i = 0; i < 10;)

    {

    i++;

    System.Threading.Thread.Sleep(10000);

    }

    but where can i place a loop like this? if i put it in the "public MainWindow()" the application never starts because the loop never ends. how can i make a public void that runs when the app starts but isn't mainwindow.

    P.S. i am pretty new with C# and am currently making a cookie clicker clone.

    Sunday, December 01, 2013 1:37 PM

Answers

  • Hi,

    For your requirement, we can use timer to implement this.

    The usual WPF timer is the DispatcherTimer, which is not a control but used in code. It basically works the same way like the WinForms timer:

    System.Windows.Threading.DispatcherTimer dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
    dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
    dispatcherTimer.Interval = new TimeSpan(0,0,1);
    dispatcherTimer.Start();
    
    
    private void dispatcherTimer_Tick(object sender, EventArgs e)
    {
      // Do something
    }

    #DispatcherTimer Class
    http://msdn.microsoft.com/en-gb/library/system.windows.threading.dispatchertimer(v=vs.110).aspx


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, December 02, 2013 8:22 AM
    Moderator

All replies

  • If you want to do something periodically it would generally be a better idea to use a timer than a for-loop with a Thread.Sleep. The problem is that you're looping over and causing your UI-thread to sleep, which will make your application UI unresponsive.

    Instead, you could do something like:

    private Timer timer; 
    
    public void StartTimer()
    {
        timer = new Timer();
        timer.Tick += timer_Tick;
        timer.Interval = 1000; 
        timer.Start();
    }
    
    private void timer_Tick(object sender, EventArgs e)
    {
        // This will be ran every second.
    }

    For more info on the Timer class, see: http://msdn.microsoft.com/en-us/library/system.timers.timer(v=vs.110).aspx

    HTH

    • Proposed as answer by chriga Sunday, December 01, 2013 8:14 PM
    Sunday, December 01, 2013 2:48 PM
  • You can put startup code in app.xaml.

        public partial class App : Application
        {
            protected void Application_Startup(object sender, StartupEventArgs e)
            {
    
            }
        }


    Bob - www.crowcoder.com

    Sunday, December 01, 2013 3:22 PM
  • i want an event to happen every secound and someone suggested i use this code:

    private Timer timer; 
    
    public void StartTimer()
    {
        timer = new Timer();
        timer.Tick += timer_Tick;
        timer.Interval = 1000; 
        timer.Start();
    }
    
    private void timer_Tick(object sender, EventArgs e)
    {
        // This will be ran every second.
    }

    and i added 

    using System.Timers;

    and that made my C# understand all of the code exept for "

    timer.Tick

    why is that? am i missing another using command? 

    Sunday, December 01, 2013 6:35 PM
  • There are multiple different Timer classes in .NET. 

    The example above uses the Timer in System.Windows.Forms. You may have to reference that assembly. If you want to use one of the other Timer types, the syntax will be slightly different.

    Here's a blog post that explains the different Timer types in further detail: http://msdn.microsoft.com/en-us/magazine/cc164015.aspx

    HTH


    Sunday, December 01, 2013 6:45 PM
  • The usual place to put code that has to be executed when the WPF Window is shown is the Loaded Event. But this might be a good case to use Initialised instead. This guarantees also that all the Elements you might want to write too actually exist (http://msdn.microsoft.com/en-us/library/ms748948.aspx#WindowLifetime).

    Note that there are 3 different Timer classes in .NET:

    http://msdn.microsoft.com/en-us/magazine/cc164015.aspx

    The System.Timers Timer is likely the best for your case.


    Let's talk about MVVM: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b1a8bf14-4acd-4d77-9df8-bdb95b02dbe2 Please mark post as helpfull and answers respectively.


    Sunday, December 01, 2013 7:09 PM
  • Hi,

    For your requirement, we can use timer to implement this.

    The usual WPF timer is the DispatcherTimer, which is not a control but used in code. It basically works the same way like the WinForms timer:

    System.Windows.Threading.DispatcherTimer dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
    dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
    dispatcherTimer.Interval = new TimeSpan(0,0,1);
    dispatcherTimer.Start();
    
    
    private void dispatcherTimer_Tick(object sender, EventArgs e)
    {
      // Do something
    }

    #DispatcherTimer Class
    http://msdn.microsoft.com/en-gb/library/system.windows.threading.dispatchertimer(v=vs.110).aspx


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, December 02, 2013 8:22 AM
    Moderator