473,738 Members | 8,397 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.Collecti ons;
using System.Componen tModel;
using System.Windows. Forms;
using System.Data;
using System.ServiceP rocess;
using System.IO;
using System.Diagnost ics;

namespace AutoRestart_Ser vices
{
/// <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.ColumnHea der ServiceName;
private System.Windows. Forms.ColumnHea der 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.Componen tModel.Containe r components = null;

public scMainForm()
{
//
// Required for Windows Form Designer support
//
InitializeCompo nent();

//
// TODO: Add any constructor code after InitializeCompo nent
call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Disp ose();
}
}
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 InitializeCompo nent()
{
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.ServiceNam e = new
System.Windows. Forms.ColumnHea der();
this.ServiceSta tus = new
System.Windows. Forms.ColumnHea der();
this.label1 = new System.Windows. Forms.Label();
this.SuspendLay out();
//
// mainMenu1
//
this.mainMenu1. MenuItems.AddRa nge(new
System.Windows. Forms.MenuItem[] {
this.menuItem1,
this.menuItem2} );
//
// menuItem1
//
this.menuItem1. Index = 0;
this.menuItem1. MenuItems.AddRa nge(new
System.Windows. Forms.MenuItem[] {
this.fileMenu}) ;
this.menuItem1. Text = "File";
//
// fileMenu
//
this.fileMenu.I ndex = 0;
this.fileMenu.T ext = "Close";
this.fileMenu.C lick += new
System.EventHan dler(this.fileM enu_Click);
//
// menuItem2
//
this.menuItem2. Index = 1;
this.menuItem2. MenuItems.AddRa nge(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.BorderSty le.FixedSingle;
this.listView1. Columns.AddRang e(new
System.Windows. Forms.ColumnHea der[] {
this.ServiceNam e,
this.ServiceSta tus});
this.listView1. GridLines = true;
this.listView1. HeaderStyle =
System.Windows. Forms.ColumnHea derStyle.Noncli ckable;
this.listView1. Location = new System.Drawing. Point(16,
32);
this.listView1. Name = "listView1" ;
this.listView1. RightToLeft = System.Windows. Forms.RightToLe ft.Yes;
this.listView1. Size = new System.Drawing. Size(392, 168);
this.listView1. TabIndex = 2;
this.listView1. View = System.Windows. Forms.View.Deta ils;
this.listView1. ItemCheck += new
System.Windows. Forms.ItemCheck EventHandler(th is.listView1_It emCheck);
//
// ServiceName
//
this.ServiceNam e.Text = "Service Name";
this.ServiceNam e.Width = 275;
//
// ServiceStatus
//
this.ServiceSta tus.Text = "Service Status";
this.ServiceSta tus.TextAlign =
System.Windows. Forms.Horizonta lAlignment.Righ t;
this.ServiceSta tus.Width = 90;
//
// label1
//
this.label1.Bac kColor = System.Drawing. SystemColors.Hi ghlight;
this.label1.Fla tStyle = System.Windows. Forms.FlatStyle .Popup;
this.label1.Fon t = new System.Drawing. Font("Microsoft Sans
Serif", 9.75F, System.Drawing. FontStyle.Regul ar,
System.Drawing. GraphicsUnit.Po int,
((System.Byte)( 0)));
this.label1.For eColor =
System.Drawing. SystemColors.Ac tiveCaptionText ;
this.label1.Loc ation = new System.Drawing. Point(16, 16);
this.label1.Nam e = "label1";
this.label1.Siz e = new System.Drawing. Size(392, 16);
this.label1.Tab Index = 5;
this.label1.Tex t = "Windows Services";
//
// scMainForm
//
this.AutoScaleB aseSize = new System.Drawing. Size(5, 13);
this.BackColor = System.Drawing. SystemColors.De sktop;
this.ClientSize = new System.Drawing. Size(426, 219);
this.Controls.A ddRange(new
System.Windows. Forms.Control[] {
this.label1,
this.listView1} );
this.FormBorder Style =
System.Windows. Forms.FormBorde rStyle.FixedSin gle;
this.Menu = this.mainMenu1;
this.Name = "scMainForm ";
this.Text = "Windows Service Controller";
this.Load += new
System.EventHan dler(this.scMai nForm_Load);
this.ResumeLayo ut(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.EventArg s
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.OpenTex t("ntservices.t xt");
line=sr.ReadLin e();

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

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

//Run the autoRestartServ ices 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.Display Name);
listView1.Items[x].SubItems.Add(s c.Status.ToStri ng());
}
listView1.Check Boxes = true;

}

private void fileMenu_Click( object sender, System.EventArg s
e)
{
scMainForm.Acti veForm.Close();
}
private void listView1_ItemC heck(object sender,
System.Windows. Forms.ItemCheck EventArgs e)
{

if (e.CurrentValue ==CheckState.Un checked)
{

autoRestartServ ices(listView1. Items[e.Index].Text);
listView1.Updat e();

}
else

{

autoRestartServ ices(listView1. Items[e.Index].Text);
listView1.Updat e();

}


}

private void autoRestartServ ices(string serviceDisplayN ame)
{

//Create ServiceControll er to administer a Windows Service
ServiceControll er sc = new ServiceControll er();
sc.DisplayName = serviceDisplayN ame;

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

//Wait for status of service to change to stopped
sc.WaitForStatu s(ServiceContro llerStatus.Stop ped);

}

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

//Restart the service
sc.Start();

//Wait for status of service to change to running
sc.WaitForStatu s(ServiceContro llerStatus.Runn ing);
}
}
}

}

All help is appreciated. . . .
Thanks,

willow1480

Nov 16 '05 #1
0 2346

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

Similar topics

3
8193
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 = rsusers!identification rsprivate!Receivedon = ladate
1
3258
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 still
0
1395
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 user is able to select a folder using a folderBrowser control. I want the paths to all files to be added to the listView control on Form1. My question lies around what thread should perform the item additions. Currently, on Form2, I referenced...
10
5821
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: WebForm1.aspx ChangeValue.aspx WebUserControl1.ascx Module1.vb
2
1260
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 approach I had to have a synclock on the listview the whole event handler, after some reading I've understood that updating a control from different threads should use the controls Invoke statement. Should I use it as well? It's not the...
7
15002
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 MyList data is resident in a dataset table. I'm stuck and can't choose either because. If I choose ListView as my control I don't understand how to programmatically get the data from the dataset table
5
10593
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 RichTextBox), but it flickers madly when I update it. There have been numerous posts about this, but I have found no solution. Enabling double-buffering does not seem to help, so has anyone any idea how it can be done? TIA
0
1190
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. I had the following Build warning: "The referenced component 'MSComctlLib' has an updated custom wrapper available."
2
2091
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 the collection directly to the grid but occasionally I'll get cross thread errors (different threads updating the collection). Is there a way to get a live view of an array/list easily?
0
8788
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9476
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9335
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9263
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8210
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6053
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4825
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3279
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2193
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.