آموزش ساخت برنامه تسک منیجر در #C
سلام بر تمامی کاربران وب سایت پروگرم 98 …
در این مطلب آموزش سی شارپ یاد می گیرین که برنامه ی معروف Task Manager رو بسازید و همچنین یاد می گیرید که چه طور یک پروسه یا یک برنامه رو درون خود برنامه تان حذف کنید.
خب ابتدا یک listbox و 2 دکمه برای Update و Kill اضافه کنید که اولی لیست پروسس ها را آپدیت میکند و دومی برای حذف یک پروسس می باشد.مانند تصویر زیر:
سپس بعد از کد InitializeComponent(); کد UpdateProcessList(); را قرار دهید تا کدهای شما به صورت زیر در آید :
public partial class frmProcessKiller : Form
{
///
/// constructor
///
public frmProcessKiller()
{
InitializeComponent();
// get an initial list of processes
UpdateProcessList();
}
{
///
/// constructor
///
public frmProcessKiller()
{
InitializeComponent();
// get an initial list of processes
UpdateProcessList();
}
پس از آن تابع UpdateProcessList را به صورت زیر تعریف کنید:
private void UpdateProcessList()
{
// clear the existing list of any items
lstProcesses.Items.Clear();
// loop through the running processes and add
//each to the list
foreach (System.Diagnostics.Process p in
System.Diagnostics.Process.GetProcesses())
{
lstProcesses.Items.Add(p.ProcessName + " - " + p.Id);
}
// display the number of running processes in
// a status message at the bottom of the page
tslProcessCount.Text = "Processes running: " +
lstProcesses.Items.Count.ToString();
}
{
// clear the existing list of any items
lstProcesses.Items.Clear();
// loop through the running processes and add
//each to the list
foreach (System.Diagnostics.Process p in
System.Diagnostics.Process.GetProcesses())
{
lstProcesses.Items.Add(p.ProcessName + " - " + p.Id);
}
// display the number of running processes in
// a status message at the bottom of the page
tslProcessCount.Text = "Processes running: " +
lstProcesses.Items.Count.ToString();
}
سپس در رویداد کلیک دکمه ی update تابعی (متدی) که در بالا تعریف شده را فراخوانی کنید.(به وسیله ی کد زیر:)
UpdateProcessList();
سپس در رویداد کلیک دکمه ی kill کد زیر را وارد کنید:
// loop through the running processes looking for a match
// by comparing process name to the name selected in the listbox
foreach (System.Diagnostics.Process p in
System.Diagnostics.Process.GetProcesses())
{
// by comparing process name to the name selected in the listbox
foreach (System.Diagnostics.Process p in
System.Diagnostics.Process.GetProcesses())
{