using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Threading; using Microsoft.Win32; namespace TestRepoCleanup { /// /// Test Windows service functions (CleanupThread).
/// Two buttons and two methods handling click event: ///

Copyright (c) 2002 Alexis Grandemange
/// Mail: alexis.grandemange@pagebox.net

///
This program is free software; you can redistribute it and/or
	/// modify it under the terms of the GNU Lesser General Public
	/// License as published by the Free Software Foundation; version
	/// 2.1 of the License.
	/// This library is distributed in the hope that it will be useful,
	/// but WITHOUT ANY WARRANTY; without even the implied warranty of
	/// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
	/// GNU Lesser General Public License for more details.
	/// A copy of the GNU Lesser General Public License lesser.txt should be
	/// included in the distribution.
///
public class TestCleanup : System.Windows.Forms.Form { private System.Windows.Forms.Button butStart; private System.Windows.Forms.Button butStop; private System.Windows.Forms.Label label1; /// /// Required designer variable. /// private System.ComponentModel.Container components = null; private Thread cleanerThread; private System.Windows.Forms.TextBox tbCleanupPath; private System.Windows.Forms.CheckBox cbLog; private System.Windows.Forms.Label labInfo; private CleanupThread cleaner; public TestCleanup() { // // Required for Windows Form Designer support // InitializeComponent(); RegistryKey regKey = Registry.LocalMachine; regKey = regKey.OpenSubKey("SYSTEM\\CurrentControlSet\\Services\\Cleanup\\Parameters"); if (regKey != null) { tbCleanupPath.Text = (string)regKey.GetValue("ConfigFile"); int iToLog = (int)regKey.GetValue("LogDebug"); if (iToLog == 1) cbLog.Checked = true; regKey.Close(); } else tbCleanupPath.Text = "E:\\Inetpub\\wwwroot\\PageBox\\TestRepoCleanup\\cleanup.xml"; butStart.Enabled = true; butStop.Enabled = false; } /// /// Clean up any resources being used. /// protected override void Dispose( bool disposing ) { if( disposing ) { if(components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.butStart = new System.Windows.Forms.Button(); this.butStop = new System.Windows.Forms.Button(); this.cbLog = new System.Windows.Forms.CheckBox(); this.label1 = new System.Windows.Forms.Label(); this.tbCleanupPath = new System.Windows.Forms.TextBox(); this.labInfo = new System.Windows.Forms.Label(); this.SuspendLayout(); // // butStart // this.butStart.Location = new System.Drawing.Point(16, 112); this.butStart.Name = "butStart"; this.butStart.Size = new System.Drawing.Size(104, 32); this.butStart.TabIndex = 0; this.butStart.Text = "start"; this.butStart.Click += new System.EventHandler(this.onStart); // // butStop // this.butStop.Location = new System.Drawing.Point(136, 112); this.butStop.Name = "butStop"; this.butStop.Size = new System.Drawing.Size(104, 32); this.butStop.TabIndex = 0; this.butStop.Text = "stop"; this.butStop.Click += new System.EventHandler(this.onStop); // // cbLog // this.cbLog.Location = new System.Drawing.Point(16, 80); this.cbLog.Name = "cbLog"; this.cbLog.Size = new System.Drawing.Size(96, 24); this.cbLog.TabIndex = 3; this.cbLog.Text = "Log events"; // // label1 // this.label1.Location = new System.Drawing.Point(16, 16); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(224, 24); this.label1.TabIndex = 1; this.label1.Text = "RepoCleanup test"; // // tbCleanupPath // this.tbCleanupPath.Location = new System.Drawing.Point(16, 48); this.tbCleanupPath.Name = "tbCleanupPath"; this.tbCleanupPath.Size = new System.Drawing.Size(224, 22); this.tbCleanupPath.TabIndex = 2; this.tbCleanupPath.Text = ""; // // labInfo // this.labInfo.ForeColor = System.Drawing.Color.Red; this.labInfo.Location = new System.Drawing.Point(16, 160); this.labInfo.Name = "labInfo"; this.labInfo.Size = new System.Drawing.Size(224, 24); this.labInfo.TabIndex = 4; // // TestCleanup // this.AutoScaleBaseSize = new System.Drawing.Size(6, 15); this.ClientSize = new System.Drawing.Size(256, 191); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.labInfo, this.cbLog, this.tbCleanupPath, this.label1, this.butStop, this.butStart}); this.Name = "TestCleanup"; this.Text = "TestCleanup"; this.ResumeLayout(false); } #endregion /// /// Called when you click on Start button. /// Creates CleanupThread and worker thread. /// private void onStart(object sender, System.EventArgs e) { string cleanupPath = tbCleanupPath.Text; bool toLog = cbLog.Checked; RegistryKey regKey = Registry.LocalMachine; try { regKey = regKey.OpenSubKey("SYSTEM\\CurrentControlSet\\Services\\Cleanup", true); regKey = regKey.CreateSubKey("Parameters"); regKey.SetValue("ConfigFile", cleanupPath); if (toLog) regKey.SetValue("LogDebug", (int)1); else regKey.SetValue("LogDebug", (int)0); } catch(System.Security.SecurityException se) { labInfo.Text = "Not allowed to update the Registry"; } cleaner = new CleanupThread(cleanupPath, toLog); cleanerThread = new Thread(new ThreadStart(cleaner.ThreadRun)); try { cleanerThread.Start(); butStart.Enabled = false; butStop.Enabled = true; } catch (ThreadStateException ev) { CleanupThread.error("Cleanup.OnStart " + ev.Message); } catch (ThreadInterruptedException ev) { CleanupThread.error("Cleanup.OnStart " + ev.Message); } } /// /// Called when you click on Stop button. Terminates the worker thread. /// private void onStop(object sender, System.EventArgs e) { try { lock(cleaner) { cleaner.toStop = true; Monitor.Pulse(cleaner); } cleanerThread.Join(); butStart.Enabled = true; butStop.Enabled = false; } catch (SynchronizationLockException ev) { CleanupThread.error("Cleanup.OnStop " + ev.Message); } catch (ThreadStateException ev) { CleanupThread.error("Cleanup.OnStop " + ev.Message); } catch (ThreadInterruptedException ev) { CleanupThread.error("Cleanup.OnStop " + ev.Message); } } /// /// The main entry point for the application. /// [STAThread] static void Main() { Application.Run(new TestCleanup()); } } }