الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

كيف أغلق الحاسوب برمجيا ؟

مغلق
بدأه MK77 في 31 مايو 2007 · 2 رد · 609 مشاهدة · في Microsoft Visual C#.NET
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

رجاء أريد غلق الحاسوب برمجيا ولا أعرف كيفية ذلك :(

#2

ابحث في المنتدى هنا عن كلمة Shutdown, فان شاء الله ستجد أكثر من موضوع.

جرب هذا : /index.php?showtopic=69430

#Read

http://MoutazShams.blogspot.com

projects003.gif

أعتذر عن التغيب هذه الأيام نظرا للانشغال المريب

#3

هذا السورس كود لبرنامج يقوم باغلاق الجهاز في الوقت المحدد وقد يفيدك

using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;

using Org.Mentalis.Utilities;

namespace TimerComputerShutdown
{
	/// <summary>
	/// Summary description for TimerComputerShutdown.
	/// </summary>
	public class TimerComputerShutdown : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Label 
			  labelWhatDoYouWantTheComputerToDo;
		private System.Windows.Forms.ComboBox comboBox;
		private System.Windows.Forms.Label 
			  labelSelectDateTimeToShutdownComputer;
		private System.Windows.Forms.DateTimePicker 
			  dateTimePicker;
		private System.Windows.Forms.Label labelDateTimeNow;
		private System.Windows.Forms.Label 
			  labelDateTimeShutdown;
		private System.Windows.Forms.Label labelNow;
		private System.Windows.Forms.Label labelShutdown;
		private System.Windows.Forms.Panel panelParent;
		private System.Windows.Forms.Panel panelChild;
		private System.Windows.Forms.CheckBox checkBox;
		private System.Windows.Forms.Button buttonAbout; 
		private System.Windows.Forms.Button buttonCancel;
		private System.Windows.Forms.Button buttonInvoke;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container 
							   components = null;

		private System.Threading.ThreadStart threadStart;
		private System.Threading.Thread thread;

		[DllImport("user32.dll")] private static extern 
			bool SetForegroundWindow(IntPtr hWnd);
		[DllImport("user32.dll")] private static extern 
			bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
		[DllImport("user32.dll")] private static extern 
			bool IsIconic(IntPtr hWnd);

		private const int SW_HIDE = 0;
		private const int SW_SHOWNORMAL = 1;
		private const int SW_SHOWMINIMIZED = 2;
		private const int SW_SHOWMAXIMIZED = 3;
		private const int SW_SHOWNOACTIVATE = 4;
		private const int SW_RESTORE = 9;
		private const int SW_SHOWDEFAULT = 10;

		public TimerComputerShutdown()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code 
			// after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
					...
		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new TimerComputerShutdown());
		}

		private void TimerComputerShutdown_Load
				 (object sender, System.EventArgs e)
		{
			this.threadStart = new System.Threading.
							 ThreadStart(Application_Tick);
			this.thread = new System.Threading.Thread(threadStart);

			this.panelChild.Visible = false;

			this.dateTimePicker.Value = System.DateTime.Now;
		}

		protected override void OnClosing(CancelEventArgs e)
		{
			try
			{
				if (this.thread.IsAlive)
				{
					this.thread.Abort();
					this.thread.Join();
				}
			}
			catch (System.Threading.ThreadAbortException 
									 threadAbortException)
			{
				System.Threading.Thread.ResetAbort();
			}
			catch (System.Exception exception)
			{

			}
			finally
			{
				System.Windows.Forms.Application.Exit();
			}

			base.OnClosing(e);
		}

		public void Computer_Shutdown()
		{
			if (this.checkBox.Checked)
			{
				System.Diagnostics.Process[] processes = 
				   System.Diagnostics.Process.GetProcesses();

				foreach (System.Diagnostics.Process 
						   processParent in processes)
				{
					System.Diagnostics.Process[] processNames = 
								 System.Diagnostics.Process.
								 GetProcessesByName
								 (processParent.ProcessName);

					foreach (System.Diagnostics.Process 
							processChild in processNames)
					{
						try
						{
							System.IntPtr hWnd = 
								   processChild.MainWindowHandle;

							if (IsIconic(hWnd))
							{
								ShowWindowAsync(hWnd, SW_RESTORE);
							}

							SetForegroundWindow(hWnd);

							if (!(processChild.
							   MainWindowTitle.Equals(this.Text)))
							{
								processChild.CloseMainWindow();
								processChild.Kill();
								processChild.WaitForExit();
							}
						}
						catch (System.Exception exception)
						{

						}
					}
				}
			}

			System.Windows.Forms.Application.Exit();

			switch (this.comboBox.SelectedIndex)
			{
				case 0:
					Org.Mentalis.Utilities.
							WindowsController.ExitWindows
							(RestartOptions.PowerOff, false);
					break;
				case 1:
					Org.Mentalis.Utilities.WindowsController.
						 ExitWindows(RestartOptions.Reboot, false);
					break;
				case 2:
					Org.Mentalis.Utilities.WindowsController.
						 ExitWindows(RestartOptions.Suspend, false);
					break;
				case 3:
					Org.Mentalis.Utilities.WindowsController.
						 ExitWindows(RestartOptions.Hibernate, false);
					break;
				case 4:
					Org.Mentalis.Utilities.WindowsController.
						 ExitWindows(RestartOptions.LogOff, false);
					break;
			}
		}

		private void comboBox_SelectedIndexChanged
				 (object sender, System.EventArgs e)
		{
			this.labelSelectDateTimeToShutdownComputer.Text =
				 "Select Date/Time to" + " " + 
				 this.comboBox.SelectedItem.ToString() + 
				 " " + "computer:";
			this.labelDateTimeShutdown.Text = "Date/Time" + 
				 " " + this.comboBox.SelectedItem.ToString() + ":";
		}

		private void checkBox_CheckedChanged(object sender, 
										System.EventArgs e)
		{
			if (this.checkBox.Checked)
			{
				Warning warning = new Warning();
				warning.ShowDialog();
			}
		}

		public bool dateTimePicker_Validated()
		{
			if (this.dateTimePicker.Value.
				   CompareTo(System.DateTime.Now) < 0)
			{
				Error error = new Error();
				error.ShowDialog();

				this.dateTimePicker.Value = System.DateTime.Now;

				return false;
			}
			else
			{
				return true;
			}
		}

		private void dateTimePicker_ValueChanged
				 (object sender, System.EventArgs e)
		{
			this.labelShutdown.Text = 
				   this.dateTimePicker.Value.ToString();
		}

		public void Application_Tick()
		{
			while (!(this.labelNow.Text.ToString().
				   Equals(this.dateTimePicker.Value.ToString())))
			{
				this.labelNow.Text = System.DateTime.Now.ToString();
			}

			this.Computer_Shutdown();
		}

		private void buttonAbout_Click(object sender, 
									System.EventArgs e)
		{
			About about = new About();

			about.ShowDialog();
		}

		private void buttonCancel_Click(object sender, 
									  System.EventArgs e)
		{
			try
			{
				if (this.thread.IsAlive)
				{
					this.thread.Abort();
					this.thread.Join();
				}
			}
			catch (System.Threading.
				   ThreadStateException threadStateException)
			{
				System.Threading.Thread.ResetAbort();
			}
			catch (System.Exception exception)
			{

			}
			finally
			{
				System.Windows.Forms.Application.Exit();
			}
		}

		private void buttonInvoke_Click(object sender, 
									System.EventArgs e)
		{
			if (this.dateTimePicker_Validated())
			{
				this.thread.Start();

				this.comboBox.Enabled = false;
				this.dateTimePicker.Enabled = false;
				this.panelChild.Visible = true;
				this.buttonInvoke.Enabled = false;
			}
		}
	}
}

هذا الموضوع مغلق.

مواضيع مشابهة