C# моя форма имеет очень большое отставание
извините за мой плохой английский,
У меня есть 8 игровых клиентов, и я показываю все игровые клиенты в одной форме с помощью "SetParent". я должен использовать приложение cpu save, потому что игровые клиенты используют очень большой процессор.Когда я запускаю свое приложение и показываю весь игровой клиент в одной форме , эта форма имеет очень большое отставание.Когда я пытаюсь переместить форму , она перемещается на 15 секунд позже. Как я могу решить эту проблему?
Что я уже пробовал:
<pre>using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Runtime.InteropServices; using System.Diagnostics; using System.Threading; namespace WindowsFormsApplication2 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private const int SW_SHOWNORMAL = 1; [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)] private static extern bool ShowWindow(IntPtr hwnd, int nCmdShow); System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(); [DllImport("user32.dll")] private static extern int SendMessage(int hWnd,uint Msg,int wParam,int lParam); [DllImport("user32.dll", SetLastError = true)] private static extern bool SetForegroundWindow(IntPtr hwnd); [DllImport("user32.dll")] static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent); [DllImport("user32.dll", SetLastError = true)] public static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint); IntPtr[] a = new IntPtr[8]; IntPtr[] b = new IntPtr[8]; [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)] private static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, string windowTitle); [DllImport("user32.dll", SetLastError = true)] private static extern IntPtr FindWindow(string lpClassName, string lpWindowName); [DllImport("User32.dll")] static extern IntPtr GetDC(IntPtr hwnd); [DllImport("User32.dll")] static extern int ReleaseDC(IntPtr hwnd, IntPtr dc); [DllImport("gdi32.dll")] static extern int GetDeviceCaps(IntPtr hdc, int nIndex); //---------- [DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)] static extern bool GetWindowRect(HandleRef hWnd, out RECT lpRect); [StructLayout(LayoutKind.Sequential)] public struct RECT { public int Left; // x position of upper-left corner public int Top; // y position of upper-left corner public int Right; // x position of lower-right corner public int Bottom; // y position of lower-right corner } public void SetForeground(Process p) { ShowWindow(p.MainWindowHandle, SW_SHOWNORMAL); SetForegroundWindow(p.MainWindowHandle); } public const int WM_SYSCOMMAND = 0x0112; public const int SC_CLOSE = 0xF060; public int[] w = new int[8]; public int[] h= new int[8]; Rectangle myRect = new Rectangle(); private void Form1_FormClosed(object sender, FormClosedEventArgs e) { // MoveWindow EskiHwnd(0), 0, 0, Me.Width, Me.Height, 1 SetParent EskiHwnd(i), Eskiyer(i) for (int i = 0; i < 8; i++) { MoveWindow(eskiyerler[i], 0, 0, WIDTH[i],HEIGHT[i], true); SetParent(eskiyerler[i],a[i]); } } private void Form1_Load(object sender, EventArgs e) { this.Text = textBox10.Text; } string[] clientisimleri = new string[8]; string[] acikuygulamaISIMleri = new string[8]; IntPtr[] eskiyerler = new IntPtr[8]; int[] acikuygulamaPIDleri = new int[8]; int[] WIDTH = new int[8]; int[] HEIGHT = new int[8]; int[] snCt = new int[8]; bool[] OtoLogin = new bool[8]; bool[] OtoLogin2 = new bool[8]; private void button4_Click(object sender, EventArgs e) { button4.Enabled = false; if (checkBox1.Checked == true) { clientisimleri[0] = textBox2.Text; textBox2.Enabled = false; checkBox1.Enabled = false; comboBox1.Items.Add(clientisimleri[0]); } else { clientisimleri[0] = ""; textBox2.Enabled = true; checkBox1.Enabled = true; } //--------------------------- if (checkBox2.Checked == true) { clientisimleri[1] = textBox3.Text; textBox3.Enabled = false; checkBox2.Enabled = false; comboBox1.Items.Add(clientisimleri[1]); } else { clientisimleri[1] = ""; textBox3.Enabled = true; checkBox2.Enabled = true; } //--------------------------- if (checkBox3.Checked == true) { clientisimleri[2] = textBox4.Text; textBox4.Enabled = false; checkBox3.Enabled = false; comboBox1.Items.Add(clientisimleri[2]); } else { clientisimleri[2] = ""; textBox4.Enabled = true; checkBox3.Enabled = true; } //--------------------------- if (checkBox4.Checked == true) { clientisimleri[3] = textBox5.Text; textBox5.Enabled = false; checkBox4.Enabled = false; comboBox1.Items.Add(clientisimleri[3]); } else { clientisimleri[3] = ""; textBox5.Enabled = true; checkBox4.Enabled = true; } //--------------------------- if (checkBox5.Checked == true) { clientisimleri[4] = textBox6.Text; textBox6.Enabled = false; checkBox5.Enabled = false; comboBox1.Items.Add(clientisimleri[4]); } else { clientisimleri[4] = ""; textBox6.Enabled = true; checkBox5.Enabled = true; } //--------------------------- if (checkBox6.Checked == true) { clientisimleri[5] = textBox7.Text; textBox7.Enabled = false; checkBox6.Enabled = false; comboBox1.Items.Add(clientisimleri[5]); } else { clientisimleri[5] = ""; textBox7.Enabled = true; checkBox6.Enabled = true; } //--------------------------- if (checkBox7.Checked == true) { clientisimleri[6] = textBox8.Text; textBox8.Enabled = false; checkBox7.Enabled = false; comboBox1.Items.Add(clientisimleri[6]); } else { clientisimleri[6] = ""; textBox8.Enabled = true; checkBox7.Enabled = true; } //--------------------------- if (checkBox8.Checked == true) { clientisimleri[7] = textBox9.Text; textBox9.Enabled = false; checkBox8.Enabled = false; comboBox1.Items.Add(clientisimleri[7]); } else { clientisimleri[7] = ""; textBox9.Enabled = true; checkBox8.Enabled = true; } //--------------------------- for (int i = 0; i < 8; i++) { OtoLogin[i] = false; } for (int i = 0; i < 8; i++) { OtoLogin2[i] = false; } for (int i = 0; i < 8; i++) { snCt[i] = 0; } baslat(); timer1.Enabled = true; timer2.Enabled = true; panel10.Enabled = true; } //İŞLEMLERİ BAŞLATIR private bool uygulamalarıtara(string clientisimi, int index) { Process[] allproc = Process.GetProcesses(); foreach (Process proc in allproc) { if (proc.MainWindowTitle == clientisimi ) { acikuygulamaISIMleri[index] = proc.MainWindowTitle; acikuygulamaPIDleri[index] = proc.Id; return true; } } return false; } private void onegetir(Process proc) { ShowWindow(proc.MainWindowHandle, SW_SHOWNORMAL); SetForegroundWindow(proc.MainWindowHandle); } private void pencereboyutlarınıal(Process proc, int index) { RECT rct; onegetir(proc); Thread.Sleep(100); if (!GetWindowRect(new HandleRef(proc, proc.MainWindowHandle), out rct)) { MessageBox.Show("ERROR"); return; } myRect.X = rct.Left; myRect.Y = rct.Top; myRect.Width = rct.Right - rct.Left + 1; myRect.Height = rct.Bottom - rct.Top + 1; if (myRect.Width < 170 | myRect.Height < 30) { Thread.Sleep(100); pencereboyutlarınıal(proc, index); } else { WIDTH[index] = myRect.Width; HEIGHT[index] = myRect.Height; } } private void paneleAt(Process proc,int index ) { eskiyerler[index] = proc.MainWindowHandle; switch (index+1) { case 1: a[index] = SetParent(proc.MainWindowHandle, panel1.Handle); break; case 2: a[index] = SetParent(proc.MainWindowHandle, panel2.Handle); break; case 3: a[index] = SetParent(proc.MainWindowHandle, panel3.Handle); break; case 4: a[index] = SetParent(proc.MainWindowHandle, panel4.Handle); break; case 5: a[index] = SetParent(proc.MainWindowHandle, panel5.Handle); break; case 6: a[index] = SetParent(proc.MainWindowHandle, panel6.Handle); break; case 7: a[index] = SetParent(proc.MainWindowHandle, panel7.Handle); break; case 8: a[index] = SetParent(proc.MainWindowHandle, panel8.Handle); break; default: break; } } private void penceresürükle(Process proc, int index) { MoveWindow(proc.MainWindowHandle, -WIDTH[index] / 2, -HEIGHT[index] / 2, WIDTH[index], HEIGHT[index], true); } private void panelboyutayarla(int index) { switch (index) { case 0: panel1.Width = WIDTH[index] / 2; panel1.Height = HEIGHT[index] / 2; break; case 1: panel2.Width = WIDTH[index] / 2; panel2.Height = HEIGHT[index] / 2; break; case 2: panel3.Width = WIDTH[index] / 2; panel3.Height = HEIGHT[index] / 2; break; case 3: panel4.Width = WIDTH[index] / 2; panel4.Height = HEIGHT[index] / 2; break; case 4: panel5.Width = WIDTH[index] / 2; panel5.Height = HEIGHT[index] / 2; break; case 5: panel6.Width = WIDTH[index] / 2; panel6.Height = HEIGHT[index] / 2; break; case 6: panel7.Width = WIDTH[index] / 2; panel7.Height = HEIGHT[index] / 2; break; case 7: panel8.Width = WIDTH[index] / 2; panel8.Height = HEIGHT[index] / 2; break; default: break; } } private void pencereyerlestir(int pid, int index) { Process proc = Process.GetProcessById(pid); pencereboyutlarınıal(proc, index); Thread.Sleep(100); a[index]=proc.MainWindowHandle; Thread.Sleep(100); paneleAt(proc, index); penceresürükle(proc, index); panelboyutayarla(index); } private void baslat() { for (int i = 0; i < 8; i++) { if (clientisimleri[i] != "") { if (uygulamalarıtara(clientisimleri[i], i) == true) pencereyerlestir(int.Parse(acikuygulamaPIDleri[i].ToString()), i); } } timer1.Enabled = true; timer2.Enabled = true; } private void timer1_Tick(object sender, EventArgs e) { for (int i = 0; i < 8; i++) { if (clientisimleri[i] !="" && OtoLogin[i] == false) { if (uygulamalarıtara(clientisimleri[i], i) == true) { OtoLogin[i] = true; richTextBox1.Text += clientisimleri[i] + " dc\n"; } } } } private void timer2_Tick(object sender, EventArgs e) { for (int i = 0; i < 8; i++) { if (clientisimleri[i] !="" && OtoLogin[i] == true) { snCt[i] += 1; label1.Text = clientisimleri[i] + " bekleniyor : " + snCt[i]; if (snCt[i]>30) { if (uygulamalarıtara(clientisimleri[i], i) == true) { snCt[i] = 0; OtoLogin[i] = false; pencereyerlestir(int.Parse(acikuygulamaPIDleri[i].ToString()), i); label1.Text = ""; richTextBox2.Text += clientisimleri[i] + " Toplandı.\n"; } } } } } private void checkBox9_CheckedChanged(object sender, EventArgs e) { if (comboBox1.Text != "") { if (checkBox9.Checked == true) { checkBox9.Text = "Küçült"; comboBox1.Enabled = false; for (int i = 0; i < 8; i++) { if (clientisimleri[i] == comboBox1.SelectedItem.ToString()) { MoveWindow(eskiyerler[i], 0, 0, WIDTH[i], HEIGHT[i], true); SetParent(eskiyerler[i], a[i]); Thread.Sleep(100); Process proc = Process.GetProcessById(acikuygulamaPIDleri[i]); SetParent(proc.MainWindowHandle, panel11.Handle); Thread.Sleep(50); tabControl1.SelectedTab = tabPage3; break; } } } else { checkBox9.Text = "Büyüt"; for (int i = 0; i < 8; i++) { if (clientisimleri[i] == comboBox1.SelectedItem.ToString()) { MoveWindow(eskiyerler[i], 0, 0, WIDTH[i], HEIGHT[i], true); SetParent(eskiyerler[i], a[i]); pencereyerlestir(acikuygulamaPIDleri[i], i); break; } } comboBox1.Enabled = true; } } }
Thomas Nielsen - getCore
Я еще не читал подробностей, но в основном, когда это происходит, это означает, что вы слишком часто обновляете пользовательский интерфейс или изменяете несколько вещей, которые приводят к тому, что макет становится недействительным и, следовательно, стоит в очереди на перекраску. Вы можете улучшить это поведение, используя SuspendLayout/ResumeLayout при изменении объекта на экране и уменьшении FPS, количества обновлений в секунду. Человеческий глаз не может легко видеть выше 30 в любом случае, я думаю, что вы делаете больше, чем это. И добавьте структуру ведения журнала (log4net) и начните сбрасывать строки, чтобы получить легкую индикацию частоты.
Мое лучшее предположение заключается в том, что ваш клиент съедает процессор, так что для другого приложения мало что остается, или ваш ход пытается нарисовать для каждого пикселя. Надеюсь, что это поможет, по крайней мере, в качестве направления.
BillWoodruff
Почему используется весь материал P/Invoke: я предлагаю вам добавить краткий обзор того, как ваша игра rins.