Click here to Skip to main content
15,889,693 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
sorry about my bad english,
I have 8 game client and i show all game client in one form with "SetParent". i have to use cpu save app because game clients use very cpu.When i run my app and show all game client in one form , that form have very lag.When i try to move form , that moves 15 second later. How can i fix this problem.

What I have tried:

C#
<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;
                }
            }
            
        }
Posted
Updated 23-Nov-17 7:23am
v2
Comments
Thomas Nielsen - getCore 23-Nov-17 10:21am    
Haven't read the details yet, but mostly when these things happen, means you're updating the ui too frequently or modify several things that cause layout to be invalidated and hence queued for repaint. You can improve this behaviour by using SuspendLayout/ResumeLayout when modifying object on screen and reducing FPS, number of updates per second. Human eye cannot easily see higher than 30 anyway, i guess you're doing more than that. And add a logging framework (log4net) and start dropping lines in to get an easy indication of frequency.
My best guess is that your client eats the cpu so there's little left for the other application or your move tries to paint for each pixel. Hope this helps as a direction at least.
BillWoodruff 23-Nov-17 11:51am    
Why is all the P/Invoke stuff used: I suggest you add a brief overview of how your game rins.

1 solution

This is just a hint as my time is limited right now ...: WinForm GUI blocking typically happens when intense computations are done in the functions used directly from WinForm. Typically the WinForm "freezes" until such functions finish their work. You can test it by running the WinForm with some functions excluded... A standard solution is to move heavy computation to a BackgroundWorker or to threads, and to update WinForm GUI when needed.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900