473,503 Members | 12,003 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

updating and refreshing a listView control

I am developing a small little Service Control Application. I am
using a listview control with checkboxes and getting the list of
services I want to control from a text file. When you check a
checkbox, it should either stop the service or start the service
depending on each respective service's status. I want the listView
to update the status for each service as they are checked. instead
all I can seem to do is clear the listView and I try running the
ListServices Function I created again, but the application just
hangs. The code is below:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.ServiceProcess;
using System.IO;
using System.Diagnostics;

namespace AutoRestart_Services
{
/// <summary>
/// Summary description for scMainForm.
/// </summary>
public class scMainForm : System.Windows.Forms.Form
{
private System.Windows.Forms.MainMenu mainMenu1;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.MenuItem fileMenu;
private System.Windows.Forms.ListView listView1;

private System.Windows.Forms.ColumnHeader ServiceName;
private System.Windows.Forms.ColumnHeader ServiceStatus;
private System.Windows.Forms.MenuItem menuItem2;
private System.Windows.Forms.MenuItem menuItem3;
private System.Windows.Forms.Label label1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public scMainForm()
{
//
// 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()
{
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.fileMenu = new System.Windows.Forms.MenuItem();
this.menuItem2 = new System.Windows.Forms.MenuItem();
this.menuItem3 = new System.Windows.Forms.MenuItem();
this.listView1 = new System.Windows.Forms.ListView();
this.ServiceName = new
System.Windows.Forms.ColumnHeader();
this.ServiceStatus = new
System.Windows.Forms.ColumnHeader();
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// mainMenu1
//
this.mainMenu1.MenuItems.AddRange(new
System.Windows.Forms.MenuItem[] {
this.menuItem1,
this.menuItem2});
//
// menuItem1
//
this.menuItem1.Index = 0;
this.menuItem1.MenuItems.AddRange(new
System.Windows.Forms.MenuItem[] {
this.fileMenu});
this.menuItem1.Text = "File";
//
// fileMenu
//
this.fileMenu.Index = 0;
this.fileMenu.Text = "Close";
this.fileMenu.Click += new
System.EventHandler(this.fileMenu_Click);
//
// menuItem2
//
this.menuItem2.Index = 1;
this.menuItem2.MenuItems.AddRange(new
System.Windows.Forms.MenuItem[] {
this.menuItem3});
this.menuItem2.Text = "Configuration";
//
// menuItem3
//
this.menuItem3.Index = 0;
this.menuItem3.Text = "";
//
// listView1
//
this.listView1.BorderStyle =
System.Windows.Forms.BorderStyle.FixedSingle;
this.listView1.Columns.AddRange(new
System.Windows.Forms.ColumnHeader[] {
this.ServiceName,
this.ServiceStatus});
this.listView1.GridLines = true;
this.listView1.HeaderStyle =
System.Windows.Forms.ColumnHeaderStyle.Nonclickabl e;
this.listView1.Location = new System.Drawing.Point(16,
32);
this.listView1.Name = "listView1";
this.listView1.RightToLeft = System.Windows.Forms.RightToLeft.Yes;
this.listView1.Size = new System.Drawing.Size(392, 168);
this.listView1.TabIndex = 2;
this.listView1.View = System.Windows.Forms.View.Details;
this.listView1.ItemCheck += new
System.Windows.Forms.ItemCheckEventHandler(this.li stView1_ItemCheck);
//
// ServiceName
//
this.ServiceName.Text = "Service Name";
this.ServiceName.Width = 275;
//
// ServiceStatus
//
this.ServiceStatus.Text = "Service Status";
this.ServiceStatus.TextAlign =
System.Windows.Forms.HorizontalAlignment.Right;
this.ServiceStatus.Width = 90;
//
// label1
//
this.label1.BackColor = System.Drawing.SystemColors.Highlight;
this.label1.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.label1.Font = new System.Drawing.Font("Microsoft Sans
Serif", 9.75F, System.Drawing.FontStyle.Regular,
System.Drawing.GraphicsUnit.Point,
((System.Byte)(0)));
this.label1.ForeColor =
System.Drawing.SystemColors.ActiveCaptionText;
this.label1.Location = new System.Drawing.Point(16, 16);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(392, 16);
this.label1.TabIndex = 5;
this.label1.Text = "Windows Services";
//
// scMainForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.BackColor = System.Drawing.SystemColors.Desktop;
this.ClientSize = new System.Drawing.Size(426, 219);
this.Controls.AddRange(new
System.Windows.Forms.Control[] {
this.label1,
this.listView1});
this.FormBorderStyle =
System.Windows.Forms.FormBorderStyle.FixedSingle;
this.Menu = this.mainMenu1;
this.Name = "scMainForm";
this.Text = "Windows Service Controller";
this.Load += new
System.EventHandler(this.scMainForm_Load);
this.ResumeLayout(false);

}
#endregion

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

}

private void scMainForm_Load(object sender, System.EventArgs
e)
{
ListServices();
}

private void ListServices()
{

//Open text file and read each line to determine what services we
want to manipulate
StreamReader sr;
string line;
sr=File.OpenText("ntservices.txt");
line=sr.ReadLine();

//Add each line into an array for later
ArrayList lines = new ArrayList();
//Loop through text file lines and add to array
while(line!=null)
{
lines.Add(line);
line=sr.ReadLine();
}

sr.Close();
//Create ServiceController to administer a Windows Service
ServiceController sc = new ServiceController();

//Run the autoRestartServices function and pass each line from the
text file to
//it, so that each service will be administered.
for(int x = 0; x < lines.Count; x++)
{
sc.ServiceName = lines[x].ToString();
listView1.Items.Add(sc.DisplayName);
listView1.Items[x].SubItems.Add(sc.Status.ToString());
}
listView1.CheckBoxes = true;

}

private void fileMenu_Click(object sender, System.EventArgs
e)
{
scMainForm.ActiveForm.Close();
}
private void listView1_ItemCheck(object sender,
System.Windows.Forms.ItemCheckEventArgs e)
{

if (e.CurrentValue==CheckState.Unchecked)
{

autoRestartServices(listView1.Items[e.Index].Text);
listView1.Update();

}
else

{

autoRestartServices(listView1.Items[e.Index].Text);
listView1.Update();

}


}

private void autoRestartServices(string serviceDisplayName)
{

//Create ServiceController to administer a Windows Service
ServiceController sc = new ServiceController();
sc.DisplayName = serviceDisplayName;

//STOP THE WINDOWS SERVICE
if(sc.Status.ToString() == "Running")
{
//Stop the service
sc.Stop();

//Wait for status of service to change to stopped
sc.WaitForStatus(ServiceControllerStatus.Stopped);

}

//RESTART THE WINDOWS SERVICE
if(sc.Status.ToString() == "Stopped")
{

//Restart the service
sc.Start();

//Wait for status of service to change to running
sc.WaitForStatus(ServiceControllerStatus.Running);
}
}
}

}

All help is appreciated. . . .
Thanks,

willow1480

Nov 16 '05 #1
0 2312

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
8179
by: Bernard André | last post by:
Hi All, context: I am using Access 97 tablkes with VB. I can see records in the MDB, using Adodc and datagrid. No problem. But when doing: rsprivate.AddNew rsprivate!For =...
1
3242
by: Lou | last post by:
I have a listview control. I change the data lvEvents.Items.SubItems.Text="AAAA"; When I debug Debug.WriteLine(lvEvents.Items.SubItems.Text); it shows the new value as "AAAA" but the grid...
0
1381
by: Art Guerra | last post by:
Any tips or information would be most appreciated! Here is my scenario simplified: I have a main application form (Form1) and on that form is a listView control. On a separate form (Form2), the...
10
5780
by: sqlboy2000 | last post by:
Hello all, I have something very simple going on here and I'm scratching my head as to what the problem is. There are 4 items in my project, 2 webforms, a user control, and a module: ...
2
1248
by: Kejpa | last post by:
Hi, I've got a number of objects that each have it's own thread. When the value changes I raise an event. Now, I want to handle the event in a form and show the value in a listview. With my first...
7
14950
by: BobAchgill | last post by:
I am trying to decide which of these controls to use to implement letting my user select a full row from MyList. The MyList has several columns which would be nice to sort by at run time. The...
5
10547
by: Charles Law | last post by:
Some of the eagle-eyed amongst you will spot this as a direct follow on from my earlier post about critical timing in .NET. I want to use a ListView to display my output (instead of the sluggish...
0
1182
by: pagates | last post by:
Hello, I use a COM-based ActiveX control as part of a C# Controls project. Part of this control is a ListView, and the ActiveX control has a property that returns the ListItems of this control....
2
2083
by: Spam Catcher | last post by:
Hi All, I want to bind a datagridview or ListView to a List that is constantly being updated (items are added/removed all the time). Any ideas what the best way is to do this? I tried binding...
0
7067
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7264
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7316
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6975
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
4992
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4666
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3160
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3148
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
371
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.