Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

[Help Request] Event Subscribe Issue

Event Delegate Dynamic timer totalseconds

  • Please log in to reply
2 replies to this topic

#1 logicPwn

logicPwn

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 177 posts
  • Location:Fresno, CA
  • Programming Language:C#, PHP, JavaScript, PL/SQL

Posted 10 May 2012 - 10:40 AM

Basically it won't let me subscribe to the event in my DogWatcher class. I have a list<> that handles the WatchDog. When I fire up my application it doesn't have any "dogs" to watch. When the user adds an application it calls the Add function which makes a new WatchDog object and then adds it to the dynamic list and trys to subscribe to it. It doesn't fire an exception I believe but it just keeps throwing null error when my code calls the Event in the WatchDog class, because there are no subscribers. Can anybody point me in the right direction?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Threading;
using System****;
using System.Reflection;

namespace Idle_Master
{
	public delegate void DogStateChangeD(GameProfile profile, DogState state, bool change);

	public enum DogState { Running, Idle };

	class WatchDog
	{
		private GameProfile _profile = null;
		private Process _process = null;
		//
		private string FileNameNoExt = null;
		private Thread IdleChecker = null;
		private System.Threading.Timer StartWatcher;
		//
		public event DogStateChangeD DogStateChange;

		public WatchDog(GameProfile profile_) : this(profile_, null) { }

		public WatchDog(GameProfile profile_, Process process_)
		{
			_profile = profile_;
			if (process_ != null) { _process = process_; }
			//
			FileNameNoExt = Path.GetFileNameWithoutExtension(_profile.Path);
			StartWatcher = new Timer(new TimerCallback(StartWatcher_Tick));
			IdleChecker = new Thread(new ThreadStart(threadedIdleWait));
			//
			if (isRunning) { IdleChecker.Start(); DogStateChange(_profile, DogState.Running, true); }
			else { StartWatcher.Change(250, 1000); DogStateChange(_profile, DogState.Running, false); }
		}

		private void StartWatcher_Tick(object state)
		{
			if (isRunning) { IdleChecker.Start(); DogStateChange(_profile, DogState.Running, true); }
		}

		private void _process_Exited(object sender, EventArgs e)
		{
			IdleChecker.Abort();
			StartWatcher.Change(250, 1000);
			DogStateChange(_profile, DogState.Running, false);
		}

		public GameProfile Profile { get { return _profile; } }

		public Process Game_Process
		{
			get { return _process; }
			set { _process = value; }
		}

		public bool isRunning
		{
			get
			{
				if (_process == null)
				{
					Process[] runningProcs = Process.GetProcessesByName(FileNameNoExt);
					if (runningProcs.Length > 0)
					{
						_process = runningProcs[0];
						_process.Exited += _process_Exited;
						return true;
					}
					else { return false; }
				}
				else { return (_process.HasExited) ? false : true; }
			}
		}

		private void threadedIdleWait()
		{
			DateTime idleTime = new DateTime(1, 1, 1);
			while (1 == 1)
			{
				if (_process.WaitForInputIdle(0))
				{
					if (idleTime.Year == 1)
					{
						idleTime = DateTime.Now;
					}
					else
					{
						if (DateTime.Now.Subtract(idleTime).TotalSeconds > 2) //TODO: > Settings.IdleTime
						{
							DogStateChange(_profile, DogState.Idle, true);
						}
					}
				}
				else
				{
					idleTime = new DateTime(1, 1, 1);
					DogStateChange(_profile, DogState.Idle, false);
				}
				Thread.Sleep(500);
			}
		}
	}

	public class DogWatcher
	{
		List<WatchDog> WatchList = new List<WatchDog>();
		//
		public event DogStateChangeD DogStateChange;

		public void Add(GameProfile gp)
		{
			WatchList.Add(new WatchDog(gp));
			WatchList[WatchList.Count - 1].DogStateChange += DogWatcher_DogStateChange;
		}

		private void DogWatcher_DogStateChange(GameProfile profile, DogState state, bool change)
		{
			DogStateChange(profile, state, change);
		}
	}
}

  • 0
"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live."
- Martin Golding

#2 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 10 May 2012 - 07:34 PM

Yeah, there is chance that first few firing may get missed. What about trying as following?

1. Change your 'WatchDog' class constructors to accept a delegate of type 'DogStateChangedD' and on the first line of constructor add that delegate to the event as follows...


public WatchDog(GameProfile profile_, DogStateChangeD delegate_) : this(profile_, null, delegate_) { }

public WatchDog(GameProfile profile_, Process process_, DogStateChangeD delegate_)
{
    DogStateChange += delegate_; 
    // Other code will go here
}


2. Then change your 'DogWatcher' class as following...

public class DogWatcher
{
        List<WatchDog> WatchList = new List<WatchDog>();
        public event DogStateChangeD DogStateChange;

        public void Add(GameProfile gp)
        {
            WatchList.Add(new WatchDog(gp, new DogStateChangeD(DogWatcher_DogStateChange)));
        }

        private void DogWatcher_DogStateChange(GameProfile profile, DogState state, bool change)
        {
            if (DogStateChange != null)
            {
                DogStateChange(profile, state, change);
            }
        }
}

  • 0

#3 logicPwn

logicPwn

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 177 posts
  • Location:Fresno, CA
  • Programming Language:C#, PHP, JavaScript, PL/SQL

Posted 11 May 2012 - 02:30 PM

OMG your a genius. Do you have a brief logical explanation of why and what I did wrong?
  • 0
"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live."
- Martin Golding





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download