Hỏi đáp

Chia sẻ kiến thức, cùng nhau phát triển

Sử dụng Multi Thread

17:08 08-08-2021 520 lượt xem 2 bình luận 00:12 10-08-2021

A  Long! E sử dung multi thread mà vẫn bị dơ ứng dụng. Anh có cách nào làm hết đơ ko ạ?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using KAutoHelper;
using System.Threading;
using System.Windows;
using System.IO;
using System.Runtime.CompilerServices;

namespace auto
{
    public partial class Form1 : Form, INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
                handler(this, new PropertyChangedEventArgs(propertyName));
        }
        #region data
        public static List<string> devices_all = new List<string>();
        public static List<Bitmap> BMP_all = new List<Bitmap>();
        #endregion
        public static bool _isStart ;
        public bool isStart
        {
            get => _isStart;
            set
            {
                _isStart = value;
                OnPropertyChanged();
                Auto(devices_all, BMP_all);
            }
        }

        public Form1 DataContext { get; }

        delegate void ThreadToStart();
        Thread _CheckImage;
        public Form1()
        {
            InitializeComponent();
            //CheckForIllegalCrossThreadCalls = false;
            this.DataContext = this;
        }

        void StartThread(ref Thread thread, bool checkFlag, ThreadToStart func)
        {
            if (checkFlag)
            {
                if (thread != null)
                {
                    try
                    {
                        thread.Abort();
                    }
                    catch { }
                }
                thread = new Thread(() => { func(); });
                thread.IsBackground = true;
                thread.Start();
            }
            else
            {
                if (thread != null)
                {
                    try
                    {
                        thread.Abort();
                        thread = null;
                    }
                    catch { }
                }
            }
        }

        private static  List<Bitmap> LoadBitmap()
        {
            List<Bitmap> lstBitmap = new List<Bitmap>();
            string[] fileArray = Directory.GetFiles(@"..\Debug\Data\", "*.png", SearchOption.AllDirectories);

            foreach (var iem in fileArray )
            {
                lstBitmap.Add((Bitmap)Bitmap.FromFile(iem));
            };
           
            return lstBitmap;
        }
        private static List<string> layDevice()
        {
            List<string> devices = new List<string>();
            devices = KAutoHelper.ADBHelper.GetDevices();

            return devices;
        }
        
        private void Form1_Load(object sender, EventArgs e)
        {
            LoadListView();
            isStart = false;
        }

        private void btPlay_Click(object sender, EventArgs e)
        {
            isStart = !isStart;          
        }
        #region ham
        
     
        void LoadListView()
        {
            listView1.View = View.Details;
            listView1.GridLines = true;
            listView1.FullRowSelect = true;
            listView1.CheckBoxes = true;
            listView1.Columns.Add("Chon", 100);
            listView1.Columns.Add("Hoat dong", 100);
           
            ListViewItem itm;
            
            foreach (var deviceID in layDevice())
            {
                itm = new ListViewItem(deviceID);
                listView1.Items.Add(itm);
            }
        }      
        void Delay(int delay)
        {
            while (delay > 0)
            {
                Thread.Sleep(TimeSpan.FromSeconds(1));
                delay--;
                if (isStart)
                    break;
            }
        }
        #endregion
        private void btHuy_Click(object sender, EventArgs e)
        {
            isStart = false ;
           
        }        
        private void listView1_ItemChecked(object sender, ItemCheckedEventArgs e)
        {
            devices_all.Clear();
            ListView lsv = sender as ListView;
            if (lsv.CheckedItems.Count > 0)
            {
                foreach (ListViewItem itm in lsv.CheckedItems)
                {
                    devices_all.Add(itm.Text);
                }
            }
            BMP_all = LoadBitmap();
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            
        }

        void Auto(List<string> lstdevice, List<Bitmap> lstPMP)
        {

            StartThread(ref _CheckImage, isStart, () => {

                while (true)
                {
                    this.Invoke(new Action(() =>
                    {
                        foreach (var deviceID in lstdevice)
                        {
                            foreach (var bmp in lstPMP)
                            {
                                Click_BMP(deviceID, bmp);
                            }
                        }
                    }));
                    Thread.Sleep(10);
                }
        });
        }
        void Click_BMP(string deviceID, Bitmap bmp)
        {
            var screen = KAutoHelper.ADBHelper.ScreenShoot(deviceID);
            
            var topPoint = KAutoHelper.ImageScanOpenCV.FindOutPoint(screen, bmp);
            var aa = KAutoHelper.ImageScanOpenCV.Find(screen, bmp);
          
            if (topPoint != null)
            {
                KAutoHelper.ADBHelper.Tap(deviceID, topPoint.Value.X, topPoint.Value.Y);
            };

        }
    }
}

 

Bình luận

Để bình luận, bạn cần đăng nhập bằng tài khoản Howkteam.

Đăng nhập
K9 SuperAdmin, KquizAdmin, KquizAuthor đã bình luận 20:54 08-08-2021
bạn cho nguyên cái vòng lặp vào invoke nên nó đơ. invoke chỉ cái nào update UI thôi bạn

Câu hỏi mới nhất