我有一个code
从文件中读取信息并将其显示给用户....现在我想在向用户显示信息后STOP
code
WAIT
以buttonpress
为event
启动我的textbox
,因为按钮会清除button
并返回用户/管理员的一些信息......
但我不知道如何打破我的代码运行并等待StringBuilder strbuildsection = new StringBuilder();
StringBuilder strbuildbetreff = new StringBuilder();
StringBuilder strbuildinhalt = new StringBuilder();
StringBuilder strbuilduser = new StringBuilder(System.Environment.UserName);
StringBuilder strbuildusercheck = new StringBuilder();
foreach (string Ccat in this.ini.IniGetCategories())
{
string readval = ini.IniReadValue(Ccat, "Read");
string usercheckvar = (this.ini.IniReadValue(Ccat, "SpecifyUser"));
string user = System.Environment.UserName;
if (readval == "0")
{
if (usercheckvar == user || usercheckvar.Equals("All"))
{
strbuildsection.Append(Ccat + Environment.NewLine);
foreach (string cat in this.ini.IniGetKeys(Ccat))
{
strbuildinhalt.Clear();
strbuildusercheck.Clear();
strbuildbetreff.Clear();
strbuildbetreff.Append(this.ini.IniReadValue(Ccat, "Betreff") + Environment.NewLine);
strbuildinhalt.Append(this.ini.IniReadValue(Ccat, "Inhalt") + Environment.NewLine);
}
textBox1.AppendText(strbuildsection.ToString() + strbuildbetreff.ToString() + strbuildinhalt.ToString() + strbuildusercheck.ToString() + Environment.NewLine);
strbuildsection.Clear();
// HERE I want to stop my process and wait until the User
// press the button and start my event
// but i don't know how to do this
// After this the loop continue and so on
}
private void BT_Bestaetigung_Click(object sender, EventArgs e)
{
CODE
}
被迫按下......
很多
button
所以如果{{1}}被按下,我想开始我的'事件',如果没有,代码应该等待这个
答案 0 :(得分:3)
因为您似乎将代码放在您向用户显示的表单中,这会阻止您停止等待用户响应,因为您仍在循环中。
解决方案是使用单独的模态对话框:
创建一个单独的Form
,它在循环中构建并在必要时显示给用户 - 等待表单关闭 - 处理结果并重复直到完成。
在这个新表单中,您可以放置用户需要与之交互的控件和按钮,并在向他显示之前填充它们。
frmConfirmationDialog myConfirmationDialog = new frmConfirmationDialog()
//Fill in information to show to the user
myConfirmationDialog.textBox1.AppendText(strbuildsection.ToString() + strbuildbetreff.ToString() + strbuildinhalt.ToString() + strbuildusercheck.ToString() + Environment.NewLine);
//Open Form modally (this will stop your loop until the dialog is closed)
DialogResult myResult = myConfirmationDialog.ShowDialog();
if (myResult == System.Windows.Forms.DialogResult.OK)
{
//Do Stuff here
}
else //catch further type of results here (you could also work with a switch statement
{
//Do Stuff here
}
BTW在关闭表单时获取DialogResult将Confirm或Cancel Buttons的DialogResult
属性设置为您喜欢的值。这将导致模式窗体使用按钮的DialogResult自动关闭。如果您需要在关闭表单之前捕获内容,可以为FormClosing
实现EventHandler或处理按钮的Click
事件。
答案 1 :(得分:0)
Random rnd = new Random();
bool stop = false;
Thread thread = null;
btnShow.Click += (source, e) =>
{
stop = true;
MessageBox.Show("Show something");
};
btnClose.Click += (source, e) =>
{
stop = false;
};
if (thread == null)
{
thread = new Thread(() =>
{
while (true) // your working loop
{
while (stop)
{}
// action of your loop
Console.WriteLine(rnd.Next());
}
});
thread.Start();
}
答案 2 :(得分:0)
你必须这样思考:你有连续操作,而你正在等待用户交互。
在winforms应用程序中,您无法在UI线程中执行连续操作(因为您的窗口需要不断地从操作系统接收消息,即用户输入或重新绘制请求并且必须对它们作出反应,否则您将 laggy < / em>甚至冻结 UI),典型的解决方案是:
所以,基本上,这样做(有一种模式可以避免使用带有委托步骤的开关,但为了简单起见,我们展示了经典方式):
_timer = new Timer();
_timer.Tick += timer_Tick;
...
// in timer tick
switch(_operation)
{
case operation1:
_operation = nextoperation; // _operation++
...
break;
case operation2:
// do nothing, wait for button press
if(_buttonPressed)
_operation = nextoperation;
break;
case operation3:
// continue after button press
..
...
}
或者这样
_thread = new Thread(...)
thread.Start();
...
// in thread
// start operation
...
// wait for button press
while(_buttonPressed)
Thread.Sleep(0);
// continue operation
...
两种情况
// in button click event
_buttonPressed = true;