当我使用线程调用另一个表单并显示它时,组件(标签)无法正常工作
When I use Threads to Call Another Form and Show it, The Components (the label) doesn't work properly
因此,为了学习如何使用线程,我编写了一个小程序,该程序应该接收名称并将它们存储在列表中,然后以另一种形式一个接一个地显示名称。
所以,这是代码:
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 ListingThread
{
public partial class Form1 : Form
{
public Thread t;
public static int ShowNo = 0;
public Form1()
{
InitializeComponent();
GlobalForm = new Form();
GlobalForm.Show();
GlobalForm.Hide();
t = new Thread(ShowList);
}
public Form GlobalForm;
public static List<string> Names = new List<string>();
private void Form1_Load(object sender, EventArgs e)
{
}
private void btnAddName_Click(object sender, EventArgs e)
{
AddName(txtName.Text);
txtName.Text = null;
}
public void AddName(string x)
{
Names.Add(x);
}
private void btnShow_Click(object sender, EventArgs e)
{
Showw();
}
public void Showw()
{
t.Start();
}
public void ShowList()
{
if (ShowNo < Names.Capacity)
{
GlobalForm.Invoke((MethodInvoker)delegate ()
{
CustomMessageBox Msg = new CustomMessageBox();
try { Msg.lblName.Text = Names[ShowNo];
}
catch
{
t.Abort();
}
Msg.Show();
Thread.Sleep(500);
Msg.Close();
ShowNo++;
ShowList();
});
}
else
{
t.Abort();
}
}
}
}
Other 表单只有一个名为 lblName 的标签:
但是当我 运行 发生这种情况时 :/ :
如您所见,标签无法正常工作:/
它们显示如下:
我建议使用 Timer
而不是 Thread.Sleep
以免阻塞 UI 线程,因为休眠会阻止标签更新。
while (ShowNo < Names.Count)
{
CustomMessageBox Msg = new CustomMessageBox();
GlobalForm.Invoke((MethodInvoker)delegate()
{
try
{
Msg.lblName.Text = Names[ShowNo];
Msg.Show();
System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
timer.Interval = 2000;
timer.Tick += (s, e) => Msg.Close();
timer.Start();
}
catch
{
t.Abort();
}
ShowNo++;
});
}
顺便说一句,线程完成后无需中止线程 运行。我还会使用 while 循环而不是递归方法调用。
因此,为了学习如何使用线程,我编写了一个小程序,该程序应该接收名称并将它们存储在列表中,然后以另一种形式一个接一个地显示名称。 所以,这是代码:
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 ListingThread
{
public partial class Form1 : Form
{
public Thread t;
public static int ShowNo = 0;
public Form1()
{
InitializeComponent();
GlobalForm = new Form();
GlobalForm.Show();
GlobalForm.Hide();
t = new Thread(ShowList);
}
public Form GlobalForm;
public static List<string> Names = new List<string>();
private void Form1_Load(object sender, EventArgs e)
{
}
private void btnAddName_Click(object sender, EventArgs e)
{
AddName(txtName.Text);
txtName.Text = null;
}
public void AddName(string x)
{
Names.Add(x);
}
private void btnShow_Click(object sender, EventArgs e)
{
Showw();
}
public void Showw()
{
t.Start();
}
public void ShowList()
{
if (ShowNo < Names.Capacity)
{
GlobalForm.Invoke((MethodInvoker)delegate ()
{
CustomMessageBox Msg = new CustomMessageBox();
try { Msg.lblName.Text = Names[ShowNo];
}
catch
{
t.Abort();
}
Msg.Show();
Thread.Sleep(500);
Msg.Close();
ShowNo++;
ShowList();
});
}
else
{
t.Abort();
}
}
}
}
Other 表单只有一个名为 lblName 的标签:
但是当我 运行 发生这种情况时 :/ :
如您所见,标签无法正常工作:/
它们显示如下:
我建议使用 Timer
而不是 Thread.Sleep
以免阻塞 UI 线程,因为休眠会阻止标签更新。
while (ShowNo < Names.Count)
{
CustomMessageBox Msg = new CustomMessageBox();
GlobalForm.Invoke((MethodInvoker)delegate()
{
try
{
Msg.lblName.Text = Names[ShowNo];
Msg.Show();
System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
timer.Interval = 2000;
timer.Tick += (s, e) => Msg.Close();
timer.Start();
}
catch
{
t.Abort();
}
ShowNo++;
});
}
顺便说一句,线程完成后无需中止线程 运行。我还会使用 while 循环而不是递归方法调用。