vurdering/optimering af kode
HejJeg vil gerne have en vurdering/hjælp til optimering af min kode. Jeg synes at det virker lidt voldsomt, at min applikation bruger ca. 6 mb hukommelse, når den ikke skal udfører tunge opgaver.
Applikationen er Winforms app.
Kode er som følger:
Klasse til liste med SQL servers:
using System;
using System.Collections.Generic;
using System.ServiceProcess;
using Microsoft.Win32;
using System.Text;
namespace SQLCommander
{
class SQLList
{
public List<ServiceController> SQLsList()
{
List<ServiceController> listOfSQL = new List<ServiceController>();
//Get services
ServiceController[] services = ServiceController.GetServices();
List<ServiceController> mssqlList = new List<ServiceController>();
List<ServiceController> mysqlList = new List<ServiceController>();
List<ServiceController> oracleList = new List<ServiceController>();
foreach (ServiceController scTemp in services)
{
//get MSSQL servers
if (scTemp.ServiceName.Contains("MSSQL$"))
{
mssqlList.Add(scTemp);
}
}
//Get MySQL servers
foreach (string name in getMySQLd())
{
foreach (ServiceController scTemp in services)
{
if(scTemp.ServiceName.Contains(name))
mysqlList.Add(scTemp);
}
}
//Get ORACLEXE servers
foreach (ServiceController scTemp in services)
{
//get MSSQL servers
if (scTemp.ServiceName.Contains("OracleService"))
{
oracleList.Add(scTemp);
}
}
services = null;
//Add found sql server to collection
//mssqls
foreach (ServiceController scTemp in mssqlList)
listOfSQL.Add(scTemp);
//mysqls
foreach (ServiceController scTemp in mysqlList)
listOfSQL.Add(scTemp);
//oracle
foreach (ServiceController scTemp in oracleList)
listOfSQL.Add(scTemp);
//reset lists
mysqlList.Clear();
mysqlList = null;
mssqlList.Clear();
mssqlList = null;
oracleList.Clear();
oracleList = null;
return listOfSQL;
}
private List<string> getMySQLd()
{
List<string> displayName = new List<string>();
string[] st = Registry.LocalMachine.OpenSubKey(
"System\\CurrentControlSet\\Services").GetSubKeyNames();
StringBuilder sb = new StringBuilder();
foreach (string key in st)
{
object imagePath;
object DisplayName;
if ((imagePath = Registry.LocalMachine.OpenSubKey(
"System\\CurrentControlSet\\Services\\" + key).GetValue("ImagePath"))
== null)
continue;
if ((DisplayName = Registry.LocalMachine.OpenSubKey(
"System\\CurrentControlSet\\Services\\" + key).GetValue("DisplayName"))
== null)
DisplayName = "Unknow";
if (imagePath.ToString().Contains("mysqld"))
{
displayName.Add(DisplayName.ToString());
}
DisplayName = null;
imagePath = null;
}
st = null;
return displayName;
}
}
}
Form1.cs:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Xml;
using System.Windows.Forms;
using System.ServiceProcess;
namespace SQLServerCommander
{
public partial class Form1 : Form
{
private SQLList sl = new SQLList();
private Languages languages = new Languages();
private string language = "dansk";
string serviceName = null;
public Form1()
{
//getLanguage();
InitializeComponent();
initComponents();
initMenuItems();
}
private void setLanguage(string lang)
{
XmlTextWriter xtw = new XmlTextWriter("Language.xml", null);
xtw.WriteStartElement("Language");
xtw.WriteString(lang);
}
private void getLanguage()
{
XmlDocument doc = new XmlDocument();
doc.Load("Language.xml");
XmlElement root = doc.DocumentElement;
XmlNodeList nodes = root.SelectNodes("/Languages");
foreach (XmlNode node in nodes)
{
language = node["Language"].InnerText;
}
}
private void initComponents()
{
bindingComboBox();
setLblSelectServerText(language);
setBtnStartText(language);
setBtnStopText(language);
setBtnRestartText(language);
}
private void initMenuItems()
{
//Toplevel menuitems
languagesToolStripMenuItem.Text = languages.languagesStripText(language);
filesToolStripMenuItem.Text = languages.filesStripText(language);
helpToolStripMenuItem.Text = languages.helpStripText(language);
//Files menuitem
exitToolStripMenuItem.Text = languages.exitStripText(language);
//Languages menuitems
englishToolStripMenuItem.Text = languages.englishStripText(language);
danishToolStripMenuItem.Text = languages.danishStripText(language);
//Help menuitems
aboutToolStripMenuItem.Text = languages.aboutStripText(language);
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void bindingComboBox()
{
List<string> listToBind = new List<string>();
BindingList<string> bList = new BindingList<string>();
int ln = 0;
foreach (ServiceController str in sl.SQLsList())
{
string scName = str.ServiceName.ToString();
if (scName.Length > ln)
ln = scName.Length;
bList.Add(scName);
}
comboBox1.Width = ln*9;
comboBox1.DataSource = bList;
serviceName = comboBox1.SelectedValue.ToString();
serviceStatus(serviceName);
}
private void serviceStatus(string sn)
{
string status = null;
foreach (ServiceController sc in sl.SQLsList())
{
string name = sc.ServiceName.ToString();
if (name.Equals(sn))
{
status = sc.Status.ToString();
}
}
if (status.Equals("Running"))
{
btn_Start.Enabled = false;
btn_Stop.Enabled = true;
btn_Restart.Enabled = true;
}
if(status.Equals("Stopped"))
{
btn_Start.Enabled = true;
btn_Stop.Enabled = false;
btn_Restart.Enabled = false;
}
//return status;
}
private void setLblSelectServerText(string lang)
{
lbl_SelectServer.Text = languages.lblSelectServer(lang);
}
private void setBtnStartText(string lang)
{
btn_Start.Text = languages.btnStart(lang);
}
private void setBtnStopText(string lang)
{
btn_Stop.Text = languages.btnStop(lang);
}
private void setBtnRestartText(string lang)
{
btn_Restart.Text = languages.btnRestart(lang);
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
serviceName = comboBox1.SelectedValue.ToString();
serviceStatus(serviceName);
}
private void btn_Start_Click(object sender, EventArgs e)
{
foreach (ServiceController sc in sl.SQLsList())
{
string name = sc.ServiceName.ToString();
if (name.Equals(serviceName))
{
sc.Start();
sc.WaitForStatus(ServiceControllerStatus.Running);
serviceStatus(serviceName);
initiateTimer();
tlStrStat_ServerName.Text = serviceName;
tlStrStat_Activity.Text = languages.statusRunning(language);
}
}
}
private void initiateTimer()
{
timer1.Tick += new EventHandler(timer1_Tick);
timer1.Interval = 1800;
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
timer1.Stop();
tlStrStat_ServerName.Text = "";
tlStrStat_Activity.Text = "";
}
private void btn_Stop_Click(object sender, EventArgs e)
{
foreach (ServiceController sc in sl.SQLsList())
{
string name = sc.ServiceName.ToString();
if (name.Equals(serviceName))
{
sc.Stop();
sc.WaitForStatus(ServiceControllerStatus.Stopped);
serviceStatus(serviceName);
initiateTimer();
tlStrStat_ServerName.Text = serviceName;
tlStrStat_Activity.Text = languages.statusStopped(language);
}
}
}
private void btn_Restart_Click(object sender, EventArgs e)
{
foreach (ServiceController sc in sl.SQLsList())
{
string name = sc.ServiceName.ToString();
if (name.Equals(serviceName))
{
sc.Stop();
sc.WaitForStatus(ServiceControllerStatus.Stopped);
sc.Start();
sc.WaitForStatus(ServiceControllerStatus.Running);
serviceStatus(serviceName);
initiateTimer();
tlStrStat_ServerName.Text = serviceName;
tlStrStat_Activity.Text = languages.statusRestarted(language);
}
}
}
private void filerToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void afslutToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Dispose();
}
private void englishToolStripMenuItem_Click(object sender, EventArgs e)
{
language = "english";
initMenuItems();
initComponents();
}
private void danishToolStripMenuItem_Click(object sender, EventArgs e)
{
language = "dansk";
initMenuItems();
initComponents();
}
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
AboutBox1 a = new AboutBox1();
a.Show();
}
private void Form1_Resize(object sender, System.EventArgs e)
{
if (FormWindowState.Minimized == WindowState)
Hide();
}
private void notifyIcon1_DoubleClick(object sender,
System.EventArgs e)
{
Show();
WindowState = FormWindowState.Normal;
}
private void toolStripMenuItem1_Click(object sender, EventArgs e)
{
Show();
WindowState = FormWindowState.Normal;
}
private void closeSQLCommanderToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Dispose();
}
private void startMenuNotifyContextMenu()
{
foreach (ServiceController str in sl.SQLsList())
{
string scName = str.ServiceName.ToString();
ToolStripMenuItem menuItm = new ToolStripMenuItem(scName, null, new EventHandler(btn_Start_Click));
menuItm.Name = scName;
startToolStripMenuItem.DropDownItems.Add(menuItm);
}
}
}
}
//hellfishdk