Possible Duplicate:
Locking main() thread
Hello,
Below you'll find my code, Main calls two threads, one initiates an event handler that returns the values of registry keys after they have been changed. The other sets up a timer which writes the changes to an XML file every few minutes. Basically I'm looking to run the write over and over while I wish the the initiation of the event handler to run only once, but remain open to accept events. Is there any way to do this? Any wait handlers which will still allow code to run etc? Please note that this is a background application, with no console as I don't want any user interaction with the system (I know typically a service is the way to go but when I asked similar questions when running a service I was told to make an application and an application makes more sense for how I want to run it/use it.)
Thanks for any help thats given beforehand.
public class main
{
static void Main(string[] args)
{
runner one = new runner();
runner two = new runner();
Thread thread1 = new Thread(new ThreadStart(one.TimerMeth));
Thread thread2 = new Thread(new ThreadStart(two.start));
thread1.Start();
thread2.Start();
}
}
public class runner
{
RegistryValueChange valuechange;
List<regkey> RegKeys = new List<regkey>();
static object locker = new object();
public void start()
{
if (File.Exists("C:\\test.xml"))
{
file load = new file();
RegKeys = load.read(RegKeys);
}
string hiveid = "HKEY_USERS";
WindowsIdentity identity = WindowsIdentity.GetCurrent();
string id = identity.User.ToString();
string key1 = id + "\\\\Software\\\\Microsoft\\\\Windows NT\\\\CurrentVersion\\\\Windows Messaging Subsystem\\\\Profiles\\\\Outlook\\\\0a0d020000000000c000000000000046";
List<string> value1 = new List<String> { "01020402", "test" };
valuechange = new RegistryValueChange(hiveid, key1, value1);
valuechange.RegistryValueChanged += new EventHandler<RegistryValueChangedEventArgs>(valuechange_RegistryValueChanged);
file test = new file();
test.checkfile("C:\\test.xml");
}
void valuechange_RegistryValueChanged(object sender, RegistryValueChangedEventArgs e)
{
}
public void TimerMeth()
{
System.Timers.Timer timer = new System.Timers.Timer();
timer.Elapsed += new ElapsedEventHandler(OnElapsedTime);
timer.Interval = 300000;
timer.Enabled = true;
}
private void OnElapsedTime(object source, ElapsedEventArgs e)
{
lock (locker)
{
file write = new file();
write.write(RegKeys);
}
}
}