0

I currently have four if statements in my code to change certain displays. On the if statement for InQueueInRing > 10 I would like it to cycle every .5 seconds between the Red, Yellow, and Green images until the condition is no longer true. I'm not sure where to start with cycling images. Do I just list the image source multiple times with a Thread.Sleep(1000) in between them?

I tried to use Loading new image each second but i can't convert type system.threading.timer to system.windows.media.imagesource. updated code below.

Below are my if statements

    if (e.CmsData.Skill.AgentsAvailable > 0)
    {
        Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action)(() =>
        {
            callsWaitingData.Text = e.CmsData.Skill.AgentsAvailable.ToString();
            callsWaitingData.Foreground = new SolidColorBrush(Colors.Green);
            callsWaitingText.Text = "Available";
            callimgae.Source = new BitmapImage(new Uri("pack://application:,,,/ScoreBoardClientTest;component/images/circle_green.png", UriKind.Absolute));
        }));
    }
    else if (e.CmsData.Skill.InQueueInRing > 10)
    {
        Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action)(() =>
        {
            callsWaitingData.Text = e.CmsData.Skill.InQueueInRing.ToString();
            callsWaitingData.Foreground = new SolidColorBrush(Colors.Red);
            callsWaitingText.Text = "Waiting";
            timer = new system.threading.timer(OnTImerEllapsed, new object(), 0, 2000);
        }));
    }
    else if (e.CmsData.Skill.InQueueInRing > 0)
    {
        Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action)(() =>
        {
            callsWaitingData.Text = e.CmsData.Skill.InQueueInRing.ToString();
            callsWaitingData.Foreground = new SolidColorBrush(Colors.Red);
            callsWaitingText.Text = "Waiting";
            callimgae.Source = new BitmapImage(new Uri("pack://application:,,,/ScoreBoardClientTest;component/images/circle_red.png", UriKind.Absolute));
        }));
    }
    else if (e.CmsData.Skill.AgentsAvailable == 0)
    {
        Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action)(() =>
        {
            callsWaitingData.Text = e.CmsData.Skill.AgentsAvailable.ToString();
            callsWaitingData.Foreground = new SolidColorBrush(Colors.Yellow);
            callsWaitingText.Text = "Available";
            callimgae.Source = new BitmapImage(new Uri("pack://application:,,,/ScoreBoardClientTest;component/images/circle_yellow.png", UriKind.Absolute));
        }));
    }



private void OnTimerEllapsed(object state)
        {
            if (!this.Dispatcher.CheckAccess())
            {
                this.Dispatcher.Invoke(new Action(LoadImages));
            }
        }   

        private void LoadImages()
        {
            string stringUri = switcher ? @"pack://application:,,,/ScoreBoardClientTest;component/images/circle_red.png" :
                                          @"pack://application:,,,/ScoreBoardClientTest;component/images/circle_yellow.png";
                                         // @"pack://application:,,,/ScoreBoardClientTest;component/images/circle_green.png";
            this.callimgae.Source = new BitmapImage(new Uri(stringUri));

            switcher = !switcher;
        }  
Community
  • 1
  • 1
mcavanaugh418
  • 127
  • 1
  • 12
  • updated code with a new attempt from another article i found. – mcavanaugh418 Apr 05 '17 at 21:32
  • also to note in my attempt I tried to use all three images but I had to comment out the last one due to an error stating only assignment, call, increment, decrement, and new object expressions can be used as a statement. even though it's not a statement and in the correct format. – mcavanaugh418 Apr 05 '17 at 21:35
  • OK, i think I fixed this myself with some trial and error. the correct answer is the question and I'll mark this answered by myself as soon as stack overflow allows me to. – mcavanaugh418 Apr 05 '17 at 21:45

1 Answers1

1

So I can't see the rest of your code, but if your are going to frequently use these images be sure to pre-load or cache them somewhere before doing this kind of operation (for performance).

There are plenty of threading/process classes you can use but a bare bones one I enjoy using is just the Thread class. Here is how to set up the thread..

Thread myThread = new Thread(threadDelegate);

The threadDelegate is of the class ThreadStart which is the the work you want the thread to do. Here is one way to set up the threadDelegate..

ThreadStart threadDelegate = new ThreadStart(() => {
    while (isCycling == true)
    {
        // Your code here
        Thread.Sleep(1000);
    }
});

The isCycling is for the loop to stop on some condition like after some odd cycles or prematurely exit. Finally you can make your thread start the delegate/work you gave using the start method..

myThread.Start();

If you want the parent thread (i.e. main thread) to wait for the thread to stop or not move on until the cycling is done you can use the Join method on myThread.

Lukas Körfer
  • 13,515
  • 7
  • 46
  • 62
Alex Ehlert
  • 466
  • 4
  • 7
  • so being a new developer... I've never pre-loaded images before and performance is something I'm starting to have a problem with. Can you assist with this by any chance? – mcavanaugh418 Apr 05 '17 at 22:57
  • What platform are you working with using C#? If it's WPF or ASP or general C# I can point you in the right direction. If it's with Unity or Unreal or really any game engines, I wouldn't be much help. I don't know enough about them to give you clear answers about performance. – Alex Ehlert Apr 05 '17 at 23:20
  • I am using WPF as a platform. Visual Studio 15 although I'm sure knowing that makes little difference. – mcavanaugh418 Apr 05 '17 at 23:28
  • load the image into resources. in VS you can paste copied images into the resources tab for an easy route. https://msdn.microsoft.com/en-us/library/bbwz4bhx(v=vs.100).aspx – ferday Apr 05 '17 at 23:43
  • Yes, ferday mentioned one of the ways to help performance. Creating a resource file will create a table which will hold your images and you retrieve the images using the unique key you give it. – Alex Ehlert Apr 06 '17 at 00:31
  • Another improvement I was mentioning before was loading and storing the images you are going to alternate with into variables (maybe a List) in the class this method is inside. Then simply use the variable you stored the images in. Remember to set the list or object of the class to null after your are done so the memory manager can garbage collect your images. – Alex Ehlert Apr 06 '17 at 00:37