Улучшения кода - C#
Формулировка задачи:
Здравствуйте! не могли бы что то по советовать по улучшению данного кода начал изучать не так давно,буду очень вам благодарен !
for (int i = 0; i < textBox; i++)
{
idauth = textBox1.Lines[i].Split(':');
if (idauth.Count() != 2)
{
MessageBox.Show("Неверный Формат!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
Invoke((Action)delegate() { button2.Enabled = true; textBox1.ReadOnly = false; });
return;
}
for (j = 0, http = "0"; j < 3 && (http == "0"); j++)
{
http = get("http://" + soc + "/prison/universal.php?key=" + idauth[1] + "&method=getInfo&user=" + idauth[0]);
}
if (http == "0")
{
error++;
BeginInvoke((Action)delegate()
{label19.Text = "" +error;});
Thread.Sleep(29000);
continue;
}
if (http.IndexOf("<result>0</result>") != -1)
{
{
MessageBox.Show("Неверный id или Auth_Key ", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
Invoke((Action)delegate()
{ button2.Enabled = true; textBox1.ReadOnly = false; });
return;
}
}
milk = testint( Regex.Match(http, "<milk>(.*?)</milk>").Groups[1].Value);
milk1 += testint(Regex.Match(http, "<milk>(.*?)</milk>").Groups[1].Value);
pac = testint(Regex.Match(http, "<category id=\"1\">(.*?)</category>").Groups[1].Value);
pac1 += testint(Regex.Match(http, "<category id=\"1\">(.*?)</category>").Groups[1].Value);
blat = testint(Regex.Match(http, "<category id=\"2\">(.*?)</category>").Groups[1].Value);
blat1 += testint(Regex.Match(http, "<category id=\"2\">(.*?)</category>").Groups[1].Value);
avto = testint(Regex.Match(http, "<category id=\"3\">(.*?)</category>").Groups[1].Value);
avto1 += testint(Regex.Match(http, "<category id=\"3\">(.*?)</category>").Groups[1].Value);
skaner1++;
Invoke((Action)delegate()
{
dataGridView1.Rows.Add(idauth[0]+":"+idauth[1],pac.ToString(),blat.ToString(),avto.ToString(),milk.ToString());
label16.Text = "" + skaner1;
label14.Text = "" +milk1;
label11.Text = "" + pac1;
label12.Text = "" + blat1;
label13.Text = ""+avto1;
});
}Решение задачи: «Улучшения кода»
textual
Листинг программы
public partial class Form1 : Form
{
public Form1()
InitializeComponent();
radioButton10.Checked = true;
button5.Enabled = false;
button6.Enabled = false;
textBox1.Enabled = false;
button3.Enabled = false;
}
Thread potok;//Для Хранения Потока
string http;
string[] idauth;
int j = 0;//
private static object[] data = null;//Для комбо бокса
public string soc = "";
int error = 0;
string log = "";
string pok;//Для покупки
int ot = 0;
int skaner1 = 0;
int pac = 0;
int blat = 0;
int avto = 0;
int milk = 0;
int milk1 = 0;
int pac1 = 0;
int blat1 = 0;
int avto1 = 0;
int kupleno = 0;
private void button2_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text.Trim();
button2.Enabled = false;
if (textBox1.Text == "") { MessageBox.Show("Введите id:Auth_Key", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); button2.Enabled = true; return; }
textBox1.ReadOnly = true;
button5.Enabled = true;
obnulit();
dataGridView1.Rows.Clear();
potok = new Thread(skaner); potok.Start();
}
void skaner()
{
int textBox = textBox1.Lines.Count();
for (int i = 0; i < textBox; i++)
{
idauth = textBox1.Lines[i].Split(':');
if (idauth.Count() != 2)
{
MessageBox.Show("Неверный Формат!!! id:Auth_Key Пример: 248307947:dd267e861d17a3ad339191faf9ba8f79", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
Invoke((Action)delegate() { button2.Enabled = true; textBox1.ReadOnly = false; });
return;
}
for (j = 0, http = "0"; j < 3 && (http == "0"); j++)
{
http = get("http://" + soc + "/prison/universal.php?key=" + idauth[1] + "&method=getInfo&user=" + idauth[0]);
}
if (http == "0")
{
error++;
BeginInvoke((Action)delegate()
{label19.Text = "" +error;});
Thread.Sleep(29000);
continue;
}
if (http.IndexOf("<result>0</result>") != -1)
{
{
MessageBox.Show("Неверный id или Auth_Key ", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
Invoke((Action)delegate()
{ button2.Enabled = true; textBox1.ReadOnly = false; });
return;
}
}
milk1 += milk = testint(Regex.Match(http, "<milk>(.*?)</milk>").Groups[1].Value);
pac1 += pac = testint(Regex.Match(http, "<category id=\"1\">(.*?)</category>").Groups[1].Value);
blat1 += blat = testint(Regex.Match(http, "<category id=\"2\">(.*?)</category>").Groups[1].Value);
avto1 += avto = testint(Regex.Match(http, "<category id=\"3\">(.*?)</category>").Groups[1].Value);
skaner1++;
Invoke((Action)delegate()
{
dataGridView1.Rows.Add(idauth[0]+":"+idauth[1],pac.ToString(),blat.ToString(),avto.ToString(),milk.ToString());
label16.Text = "" + skaner1;
label14.Text = "" +milk1;
label11.Text = "" + pac1;
label12.Text = "" + blat1;
label13.Text = ""+avto1;
});
}
Invoke((Action)delegate()
{
button2.Enabled = true;
textBox1.ReadOnly = false;
button5.Enabled = false;
});
}
/// <Обнуляем переменные и лейбалы>
///
/// </summary>
void obnulit()
{
Invoke((Action)delegate()
{
label16.Text = "0";
label11.Text = "0";
label12.Text = "0";
label13.Text = "0";
label14.Text = "0";
label19.Text = "0";
label24.Text = "0";
label28.Text = "0";
});
skaner1 = 0;
pac = 0;
blat = 0;
avto = 0;
error = 0;
pac1 = 0;
blat1 = 0;
avto1 = 0;
milk = 0;
milk1 = 0;
ot = 0;
kupleno = 0;
}
/// <Тест инт>
///
/// </summary>
/// <param name="tom"></param>
/// <returns></returns>
private static int testint(string tom)
{
try
{
return Convert.ToInt32(tom);
}
catch { return 0; }
}