我正在申请一个朋友的生日,那里应该会弹出一个改变赞美的窗口。 然而,窗口冻结,标签不会改变。我用Google搜索,并阅读有关使用Backgroundworker的内容,以便从更改过程中分离GUI线程。 仍然无效。
这是我的代码:
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 System.Threading;
namespace ProjectL
{
public partial class Form1 : Form
{
private MessageHandler theHandler = new MessageHandler();
private BackgroundWorker theBackgroundWorker = new BackgroundWorker();
public Form1()
{
InitializeComponent();
}
private void StartButton_Click(object sender, EventArgs e)
{
StartButton.Visible = false;
theBackgroundWorker.DoWork += new DoWorkEventHandler(theBackgroundWorker_doYourWork);
//theBackgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(theBackgroundWorker_doYourWork);
theBackgroundWorker.RunWorkerAsync();
theHandler.RunMessage(hBDLabel, youAreLabel, mainLabel, this);
}
void theBackgroundWorker_doYourWork(object sender, DoWorkEventArgs e)
{
theHandler.RunMessage(hBDLabel, youAreLabel, mainLabel, this);
}
}
}
这应该是从背景中发生的,使用我命名为MessageHandler的类:
class MessageHandler
{
public List<String> GenerateComplimentTexts()
{
List<String> stringList = new List<String>();
//Adding a bunch of compliments into a List<String>
return stringList;
}
public void RunMessage(Label hBDLabel, Label youAreLabel, Label mainLabel, Form1 form)
{
List<String> stringList = GenerateComplimentTexts();
Thread.Sleep(2000);
form.Text = "Happy Birthday Goose!!!";
hBDLabel.Text = "Happy Birthday Goose!";
Thread.Sleep(3000);
youAreLabel.Text = "You are...";
Thread.Sleep(2000);
foreach (String e in stringList)
{
mainLabel.Text = e;
//form.Test = e
Thread.Sleep(1000);
}
Thread.Sleep(3000);
mainLabel.Text = "";
youAreLabel.Text = FinalMessage;
}
private String _finalMessage = "FINAL MESSAGE";
public String FinalMessage {get {return _finalMessage;}}
}
然而,我的窗户没有任何变化。如果我选择取消注释,除了表单顶部栏中的文本外,一切都被冻结了 form.Text = e;
有什么建议吗?
答案 0 :(得分:1)
您可以使用Timers而不是BackgroundWorker来实现它。
它可能看起来像这样:
MainForm:
public partial class Form1 : Form
{
private Timer timer;
private MessageHandler theHandler = new MessageHandler();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
timer = new Timer();
timer.Interval = 1000;
timer.Tick += TimerOnTick;
// Initialize the other labels with static text here
}
private void TimerOnTick(object sender, EventArgs eventArgs)
{
theHandler.ShowNext(label1);
}
private void StartButton_Click(object sender, EventArgs e)
{
timer.Start();
}
}
MessageHandler类:
public class MessageHandler
{
private List<String> compliments = new List<string>();
private int index = 0;
public MessageHandler()
{
GenerateComplimentTexts();
}
private void GenerateComplimentTexts()
{
List<String> stringList = new List<String>();
//Adding a bunch of compliments into a List<String>
compliments = stringList;
}
public void ShowNext(Label label)
{
label.Text = compliments.ElementAt(index);
index = (index >= compliments.Count - 1) ? 0 : index + 1;
}
}
答案 1 :(得分:0)
要更新UI,请使用后台工作程序的ReportProgress方法。 ReportProgress引发了ProgressChanged事件,您可以从那里更新UI。请务必将backgroundworker的WorkerReportsProgress属性设置为true。
答案 2 :(得分:0)
试试这个: 1.)将计时器复制到表格 2.)将以下代码复制到timer1_tick-Event。
private int _counter;
private int _currentIndex;
private void timer1_Tick(object sender, EventArgs e)
{
switch (_counter)
{
case 1:
form.Text = "Happy Birthday Goose!!!";
hBDLabel.Text = "Happy Birthday Goose!";
break;
case 2:
timer1.Interval = 3000;
break;
case 3:
youAreLabel.Text = "You are...";
break;
case 4:
if (stringlist.count = (_currentIndex -1))
{
timer1.Enabled = false;
}else {
mainLabel.Text = e;
timer1.Interval = 1000;
return;
}
break;
}
_counter ++;
}
不是最优雅的方式,但它应该有用。
答案 3 :(得分:0)
试试这段代码:
private void StartButton_Click(object sender, EventArgs e)
{
StartButton.Visible = false;
theBackgroundWorker.DoWork += new DoWorkEventHandler(theBackgroundWorker_doYourWork);
theBackgroundWorker.ProgressChanged += new ProgressChangedEventHandler(theBackgroundWorker_ProgressChanged);
theBackgroundWorker.RunWorkerAsync();
}
void theBackgroundWorker_doYourWork(object sender, DoWorkEventArgs e)
{
var w = sender as BackgroundWorker;
List<String> stringList = GenerateComplimentTexts();
Thread.Sleep(2000);
w.ReportProgress(0, "Happy Birthday Goose!!!");
w.ReportProgress(1, "Happy Birthday Goose!");
Thread.Sleep(3000);
w.ReportProgress(2, "You are...");
Thread.Sleep(2000);
foreach (String e in stringList)
{
w.ReportProgress(3, e);
Thread.Sleep(1000);
}
Thread.Sleep(3000);
w.ReportProgress(3, "");
w.ReportProgress(2, FinalMessage);
}
void theBackgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
var msg = e.UserState.ToString();
switch (e.ProgressPercentage)
{
case 0:
form.Text = msg;
break;
case 1:
hBDLabel.Text=msg;
break;
case 2:
youAreLabel.Text =msg;
break;
case 3:
mainLabel.Text=msg;
break;
}
}