gpt4 book ai didi

c# - n 单击一个按钮给出 n 个输出。需要的只是一次点击产生的一个输出

转载 作者:太空宇宙 更新时间:2023-11-03 23:36:16 25 4
gpt4 key购买 nike

好的.. 所以我不知道为什么会这样。我猜我在 Java 中看到了类似的问题.. 但我不明白这些东西。我正在使用 C#,其中有一个 listview,它会在单击按钮时使用远程服务器更新状态进行更新。我有为此工作的代码。但是当我再次点击同一个按钮时,输出显示两次。如果我再点击一次,输出会显示三次,依此类推!例如:第一次点击:

xxx     login to server failed
yyy login to server failed

第二次点击:

xxx     login to server failed
xxx login to server failed
yyy login to server failed
yyy login to server failed

我正在使用一个 backgroundworker 和一个并行的 foreach 循环。我已经把所有涉及的函数放在这里,所以那里有所有的信息。抱歉如果那太多了!这是我的代码:

    private void button1_Click(object sender, EventArgs e)                        //get update button
{
GlobalVariables._count = 0;
Status.statusProgress obj1 = new Status.statusProgress();
if (getupdate_button.Text == "Stop")
{
backgroundWorker1.CancelAsync();
getupdate_button.Enabled = false;
}
else
{
getupdate_button.Text = "Stop";
listView1.Items.Clear();
//do stuff
backgroundWorker1.ProgressChanged += backgroundWorker1_ProgressChanged;
}
if (backgroundWorker1.IsBusy)
{
if (backgroundWorker1.CancellationPending != true)
{
MessageBox.Show("Please wait till the pervious operation is completed!");
}
else
{
Complete_label.Text = "Cancelling...";
Complete_label.Visible = true;
}
}
else
{
backgroundWorker1.RunWorkerAsync(obj1); //calling background worker
}
}

做工作:

    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)                         //the boss-- the background worker
{
System.ComponentModel.BackgroundWorker worker;
worker = (System.ComponentModel.BackgroundWorker)sender;
Status obj1 = new Status();
Status.statusProgress obj = new Status.statusProgress();

if ((backgroundWorker1.CancellationPending == true))
{
e.Cancel = true;
}
else
{
obj1.Calculation(worker, e);
}
e.Result = obj;
}

计算方法:

    public void Calculation(System.ComponentModel.BackgroundWorker worker, System.ComponentModel.DoWorkEventArgs e)
{
Status.statusProgress obj = (Status.statusProgress)e.Argument;
//stuff

var file = File.ReadAllLines(obj.SourceFile);

List<string> namelist = null;
namelist = new List<string>(file);
Parallel.ForEach(namelist, /*new ParallelOptions { MaxDegreeOfParallelism = 4 }, */line =>
//foreach (string line in namelist)
{
var progress = new statusProgress();
progress.TotalSysCount = obj.TotalSysCount;
Status.statusProgress result = new Status.statusProgress();
if (worker.CancellationPending)
{
e.Cancel = true;
worker.ReportProgress(result.SysCount, result);
}
else
{
//this.SystemName = line;//file.ReadLine();
progress.SystemName = line;
//result = progress;
result = OneSystem(line);
//work with result
}
count1 = Interlocked.Increment(ref count);
//stuff
worker.ReportProgress(count1, progress);
Thread.Sleep(200);

});

}
}

报告进度方法:

     private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
try
{
var newItem = listView1.Items.Add("");
newItem.SubItems.Add(result.SystemName);
newItem.SubItems.Add(result.StatusString);
newItem.SubItems.Add(result.AvailableUpdatesCount.ToString());
}
catch (Exception ex)
{}

update_text(result);
progressBar1.Maximum = 100;
int perc = (int)((result.SysCount * 100) / result.TotalSysCount);
progressBar1.Value = perc;
Complete_label.Text = perc.ToString()+"%";
if (progressBar1.Value == 100)
{
Complete_label.Text = "Complete!";
getupdate_button.Enabled = false;
}
}

最佳答案

我想问题出在这里:

backgroundWorker1.ProgressChanged += backgroundWorker1_ProgressChanged;

如果每次执行整个单击 操作时都添加一个ProgressChanged 事件处理程序,则每次单击后都会增加处理该事件的次数。

归根结底,您应该在某些应用程序的初始化代码或您的类构造函数中添加事件处理程序,而不是在单击事件处理程序中这样做。也就是说,您将确保在每个应用程序生命周期中添加一次 backgroundWorker1_ProgressChanged 事件处理程序。

关于c# - n 单击一个按钮给出 n 个输出。需要的只是一次点击产生的一个输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30475298/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com