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

Windows Service Management

مغلق
بدأه Joker70j في 6 سبتمبر 2003 · 3 رد · 848 مشاهدة · في Microsoft Visual C#.NET
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

احببت اضع هذا الموضوع المترجم من قبلي لانه جميل جداً

اتمنى ان يعجبكم الموضوع

ربما في هذه الايام ساقوم بترجمة المقالات واضعها في المنتدى

الكلام مابين القوسين () هذا ه غير مترجم اي كتاب اضافيه مني

المقدمة :

عندما كانت اقرأ مكتبة الـMSDN.NET , أنا ابحث عن موضوع مفيد عن Windows services ( خدمات الويندز ) لذالك أنا حاولت ابني تطبيقات بنفسي عن windows service management ( إدرة الخدمات الويندز ) . هذا الفكره اتت من خدمات التطبيق التي في بني ميكروسوفت في لوحة التحمكم , أمل أن هذا التطبيق يستطيع أن يصبح نافعاً

بناء التطبيق :

لبناء هذا التطبيع استخدمة الفيئة( class كلاس ) ServiceController . هذهي الفيئة تحاتج الى مساحة الاسماء System.ServiceProcess . تستطيع الحصول عليها من المكتبة MSDN . هنا الخطوات ( تابع الخطوات )

1. افتح تطبيعق جديد من نوع Windows Application , يفضل WinServiceManage كاسم الى المشروع

Figure1.gif

2.بخصوص الفيئة System.ServiceProcess عليك باضافتها الى المشروع هكذه , اضغط على قائمة المشروع على Reference ثم Add Reference و ختر System.ServiceProcess.dll

Figure2.gif

3. بناء الواجه كالشكل 3

المحتويات النافده

Label

Button

Panel

ListView

Figure3.gif

خصائص ListView ...

Name = ServiceList 
FullRowSelect = True 
GridLines = True 
Columns = أنظر الى الشكل  4
MultiSelect = False 
Sorting = Ascending 
View = Detail

التبع المظهر الخصائص من الـ ListView

Name:  Name  = ServiceName 
       Text  = Service Name 
       Width = 126 

Description: Name  = Description 
             Text  = Description 
             Width = 214 

Status: Name  = Status 
        Text  = Status 
        Width = 74

Figure4.gif

شكل 4

4.اضف هذا السطر الكود هذا الى اعلى ملف form1.cs

using System.ServiceProcess;

واضف هذا المتغير الى فيئة Form1

 public ServiceController[] services;

5.هذا الكود ضعه في الزر "All View"

try
   {
    ListViewItem datalist;    
    services = ServiceController.GetServices();   
    ServiceList.Items.Clear();  
    foreach(ServiceController service in 

services)
    {    
        datalist = new 

System.Windows.Forms.ListViewItem(service.Service

Name.ToString());
        

datalist.SubItems.Add(service.DisplayName);    
        

datalist.SubItems.Add(service.Status.ToString());
        ServiceList.Items.Add(datalist);    
    }                

   }
   catch(Exception er)
   {
    MessageBox.Show(er.Message,"Error 

Exception",MessageBoxButtons.OK, 
                          MessageBoxIcon.Error); 
   }

6. اضعظ بلزر الايمن من الـ Mouse على قائمة الفيئه لإضافه إجراء (Method )

Figure5.gif

Figure6.gif

هنا الكود الاجراء

public void ListChange()
  {
    if(ServiceList.SelectedItems.Count!=0)
    {
        ListViewItem item = 

ServiceList.FocusedItem;
        if(item!=null)
        {
            if(item.SubItems[2].Text=="Running")
            {
                StartBtn.Enabled = false;
                StopBtn.Enabled = true;
            }
            else
            {
                StartBtn.Enabled = true;
                StopBtn.Enabled = false;
            }
        }    
    }
  }

7. الاجراء السابق في حدث ( Event ) من ListView : Click,KeyDown, and KeyUp

 ListChange();

8. هذه الاسطر الكود في الزر "Start"

private void StopBtn_Click(object sender, 

System.EventArgs e)
 {

    try
    {
        if(ServiceList.SelectedItems.Count!=0)
        {
            int n = 

ServiceList.FocusedItem.Index;  
            if(services[n].CanStop)
            {
                services[n].Stop();
                StartBtn.Enabled = true;
                StopBtn.Enabled = false;
                

ServiceList.FocusedItem.SubItems[2].Text = 

"Stopped";  
            }
            else
            {
                MessageBox.Show("This service 

couldn't be stopped","Error",
                                

MessageBoxButtons.OK, MessageBoxIcon.Error);
            }                 
        }
        else
        {
            MessageBox.Show("Please,choose a 

service that you want to stop",
                            

"Information",MessageBoxButtons.OK, 
                            

MessageBoxIcon.Information);
        }
    }
    catch(Exception er)
    {
        MessageBox.Show(er.Message,"Error 

Exception",MessageBoxButtons.OK, 
                        MessageBoxIcon.Error);
    }

}

9. هذه الاسطر الكود في الزر "Stop"

 private void StartBtn_Click(object sender, 

System.EventArgs e)
  {
    try
    {
        if(ServiceList.SelectedItems.Count!=0)
        {
            int n = 

ServiceList.FocusedItem.Index;      
            services[n].Start();
            StartBtn.Enabled = false;
            StopBtn.Enabled = true;
            

ServiceList.FocusedItem.SubItems[2].Text = 

"Running"; 
        }
        else
        {
            MessageBox.Show("Please,choose a 

service that you want to stop",
                            

"Information",MessageBoxButtons.OK, 
                            

MessageBoxIcon.Information);
        }
    }
    catch(Exception er)
    {
        MessageBox.Show(er.Message,"Error 

Exception",MessageBoxButtons.OK, 
                        MessageBoxIcon.Error);
    }
  }

10. شغل التطبيق ( انتها البرنامج )

المرجع :

مكتبت MSDN.NET

المصدر المقاله :

http://www.codeproject.com/csharp/winservicemanage.asp

تم تعديل هذه المشاركة بواسطة Joker70j في 6 سبتمبر 2003 في 23:11

#2

طبعاً وضعت هذا الموضوع الى الفائده

لهذا ليس هناك بعض التناسب لهذا ربما تحتاج الى الرجوع المصدر المقاله

وشكراً

#3

أشكرك على اهتمامك و ياليت تواصل مشوارك بالترجمة لأننا في حاجة إليك

#4

شكراً أخي ferass على الرد

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

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