How to invoke a method from Main Window from DispatcherTimer located in a different class?

Kalpana 286 Reputation points
2022-01-19T08:56:24.377+00:00

Hi

I have got this DispatcherTimer code in a Timer class.
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Threading;

namespace HRImage
{
    class Timer
    {
        //property
        private DispatcherTimer _dispatcherTimer;

        public DispatcherTimer DispatcherTimer
        {
            get => _dispatcherTimer;
            set
            {
                _dispatcherTimer = value;
            }

        }
        //public static TimeSpan annTime { set => annTime = TimeSpan.Parse(Configdetails.annhours); get => annTime; }
        public TimeSpan AnnTime 
        {
            //set
            //{
            //    _annTime = ConvertAnnTimetoTS();
            //}
            get
            {
                return ConvertAnnTimetoTS();
            }
        }

        //public string strTimeSpan;

        //private TimeSpan _annTime;
        public TimeSpan hrTime 
        { 
            //set => hrTime = ConvertHRTimetoTS(); 
            get => ConvertHRTimetoTS(); 

        }
        public TimeSpan defTime 
        { 
            //set => defTime = ConvertDefTimetoTS(); 
            get => ConvertDefTimetoTS(); 
        }

        //method
        protected internal void LoadTimer(TimeSpan oldts, TimeSpan newts)
        {

            _dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
            _dispatcherTimer.Interval = oldts;
            //_dispatcherTimer.Tick += DispatcherTimer_Tick ;
            _dispatcherTimer.Tick += delegate (object sender, EventArgs e) { DispatcherTimer_Tick(sender, e, newts); };
            _dispatcherTimer.Start();
        }


        private void DispatcherTimer_Tick(object sender, EventArgs e, TimeSpan newts)
        {
            //what do after the said hours. 
            //move image to another folder

            _dispatcherTimer.Interval = newts;




        }

        protected internal TimeSpan ConvertAnnTimetoTS()
        {
            return TimeSpan.Parse(Configdetails.annhours);
        }

        protected internal TimeSpan ConvertHRTimetoTS()
        {
            return TimeSpan.Parse(Configdetails.hrimghours);
        }

        protected internal TimeSpan ConvertDefTimetoTS()
        {
            return TimeSpan.Parse(Configdetails.defimghours);
        }
    }
}

I have this code from the MainWindow.cs .

using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using static HRImage.NativeMethods;

namespace HRImage
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    /// 
    public partial class MainWindow : Window
    {

        private DateTime[] _listtimes = new DateTime[2];

        public DateTime[] Listtimes
        {
            get => _listtimes;
            set
            {
                _listtimes = value;
            }
        }


        public MainWindow()
        {
            InitializeComponent();

            LogHelper.Log(LogTarget.File, "HRImage Started");
            bdrforimage.BorderThickness = new Thickness(ImageLoader.LoadBorderValue());
            //CreateListofImagePaths();
            FirstExecution(DateTime.Now);
        }


        internal void FirstExecution(DateTime launchedtime)
        {

            //check if announcement exists
            FileOperation imagearr = new FileOperation();
            imagearr.MakeListofImagePaths();


            if(imagearr.CheckifFileExist(ImageLoader.AnnImage) == true)
            {
                imagearr.FileName = ImageLoader.AnnImage;
                imageplcholder.Source = new BitmapImage(imagearr.ConvertStringtoUri(imagearr.FileName));
                Timer loadtimer = new Timer();
                loadtimer.LoadTimer(loadtimer.AnnTime, loadtimer.hrTime);
                //ReleaseImagefromPlcHolder(imagearr.FileName);

                //afterTimerend-Reset this - call another method

            }
            else if(imagearr.CheckifFileExist(ImageLoader.AnnImage) == false)
            {
                imagearr.FileName = imagearr.LoadRandomImage();
                imageplcholder.Source = new BitmapImage(imagearr.ConvertStringtoUri(imagearr.FileName));
                Timer loadtimer = new Timer();
                loadtimer.LoadTimer(loadtimer.hrTime,loadtimer.hrTime);
                //ReleaseImagefromPlcHolder(imagearr.FileName);

            }
            else
            {
                imageplcholder.Source = new BitmapImage(imagearr.ConvertStringtoUri(ImageLoader.DefImage));
                Timer loadtimer = new Timer();
                loadtimer.LoadTimer(loadtimer.defTime,loadtimer.hrTime);

            }
        }


        internal void ReleaseImagefromPlcHolder(string dropcurrimage)
        {
            FileOperation imagearr = new FileOperation();
            imagearr.FileName = imagearr.LoadRandomImage();
            imageplcholder.Source = new BitmapImage(imagearr.ConvertStringtoUri(imagearr.FileName));
            imagearr.MoveFileName = dropcurrimage;
            imagearr.MoveFileAnnHR();
            FirstExecution(DateTime.Now);
        }

        private void headerThumb_DragDelta(object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e)
        {
            Left = Left + e.HorizontalChange;
            Top = Top + e.VerticalChange;
        }

        private void OnKeyDownHandler(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == Key.Return)
            {
                //textBox1.Text = "You Entered: " + textBox1.Text;
                this.Visibility = Visibility.Collapsed;
                this.Topmost = true;
                this.Visibility = Visibility.Visible;


            }
        }





    }
}

How can I get the DispatcherTimer's tick event as the Timer class to invoke the method ReleaseImagefromPlcHolder() located at the MainWindow class. I would need the parameter dropcurrimage to be passed as well.

Please advice.

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,676 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,278 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Viorel 112.5K Reputation points
    2022-01-19T19:32:46.897+00:00

    Try something like this:

    class Timer
    {
       public Action action = null;
       . . .
       private void DispatcherTimer_Tick(object sender, EventArgs e, TimeSpan newts)
       {
          _dispatcherTimer.Stop( );
          action?.Invoke( );
       }                
    }
    
    // in MainWindow, after creating the timer:
    
    Timer loadtimer = new Timer( );
    string filename = imagearr.FileName;
    loadtimer.action = ( ) => ReleaseImagefromPlcHolder( filename );
    . . .
    
    0 comments No comments