-1

I am trying to create a simple Kiosk-style web browser for work. This is supposed to rotate between two internet tabs automatically. There will NEVER be any user input. Static web pages, preassigned, and only for display in our main office. I am using a C#.NET WPF app with a TabControl and a web browser in each tab. I can preassign a web page to each tab, easy stuff. What I cannot do is generate the infinite loop to constantly switch between the two tabs. Any loop whatsoever [while(true), for(;;), do-while(true)] will keep the form from loading at all. The code provided is my attempt at just generating the web pages within the loop first, then I'll go back and add the logic for the auto-switching.

namespace LoopNet
{
    public partial class MainWindow : Window
    {
       public MainWindow()
       {
            InitializeComponent();

            while(true)
            {
                Calendar1.Navigate("http://www.google.com");
                GIS1.Navigate("http://www.YouTube.com");
            }
        }
    }
}
Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
Awrigh21
  • 11
  • 3
  • 2
    Why not use a Timer object and add a method to its Elapsed event, which could handle switching tabs. Check this SO post (https://stackoverflow.com/questions/3981550/refresh-ui-with-a-timer-in-wpf-with-backgroundworker) – Ryan Wilson Jun 29 '18 at 13:42
  • Your code is effectively switching between tabs thousands of times per second. Have you tried implementing a preset delay between tab switches? (There are other issues here, but you seem to not have considered the delay you want yet, since it's not actually mentioned in your code) – Flater Jun 29 '18 at 13:47

2 Answers2

-1

Anything called on the main thread like that will hang the app. As a comment said, use a Timer.

Note: if the Timer.Elapsed method is doing things TO the UI, then it won't like it. The quick solution to this is to use the Dispatcher invoke method.

you'd end up with something along these lines:

public void CreateTimer() 
{
   var timer = new System.Timers.Timer(1000); // fire every 1 second
   timer.Elapsed += HandleTimerElapsed;
}

public void HandleTimerElapsed(object sender, ElapsedEventArgs e)
{
    Application.Current.Dispatcher.Invoke(
    () => 
    {
         //do things on UI thread...
         SwitchTheTabs();
    }
);    
GPW
  • 2,528
  • 1
  • 10
  • 22
  • 1
    I agree with the downvote based on my original post - it was far too terse so I've edited it. If the downvote was due to something else then please leave a comment as to why so I can learn too. (commentless downvotes are just a bit mean IMO) – GPW Jun 29 '18 at 13:50
  • Looks like you fixed it pretty good to me, there is a DispatcherTimer object which I linked to on my comment which could be even better suited for the OPs question. P.S. I didn't do the downvote. – Ryan Wilson Jun 29 '18 at 13:56
-1

Add a Loaded event handler and stick it in there.

bgrachus
  • 31
  • 1
  • 6