问题描述
我有将一种格式的 csv 文件转换为另一种格式的 Windows 窗体项目。 我试图在转换时显示转换进度。 如果转换失败,转换方法会抛出异常。 转换失败时,我关闭 ConvertingScreen 但当我关闭 ConvertingScreen system.invalidOperationException 时发生。
在创建窗口句柄之前,不能在控件上调用 invoke 或 begininvoke
如果我在关闭之前添加一些等待,似乎永远不会发生。我曾尝试检查 HandleCreated,但效果不佳。我曾经通过在进行中仅添加可见标签来完成此类工作。这是我第一次这样做。这是不好的做法吗?如果不是,我该如何解决这个问题?下面是简单的代码,可以描述发生在我身上的一切。
// MainForm
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender,EventArgs e)
{
try
{
ConvertingScreen.ShowScreen();
// Converts here and results in failure so throwing an exception
throw new NotImplementedException();
}
catch (Exception)
{
ConvertingScreen.CloseForm();
MessageBox.Show("error");
}
}
}
// ConvertingScreen
public class ConvertingScreen : Form
{
public ConvertingScreen()
{
InitializeComponent();
}
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
// label1
this.label1.Font = new System.Drawing.Font("MS UI Gothic",11F,System.Drawing.FontStyle.Regular,system.drawing.graphicsUnit.Point,((byte)(128)));
this.label1.Location = new System.Drawing.Point(12,9);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(316,22);
this.label1.TabIndex = 0;
this.label1.Text = "";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
// ConvertingScreen
this.BackColor = System.Drawing.Color.LightGray;
this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center;
this.ClientSize = new System.Drawing.Size(340,40);
this.ControlBox = false;
this.Controls.Add(this.label1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.MaximizeBox = false;
this.MaximumSize = new System.Drawing.Size(340,40);
this.MinimizeBox = false;
this.MinimumSize = new System.Drawing.Size(340,40);
this.Name = "ConvertingScreen";
this.ShowIcon = false;
this.ShowInTaskbar = false;
this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
this.Text = "ConvertingScreen";
this.TopMost = true;
this.ResumeLayout(false);
}
private Label label1;
private delegate void CloseDelegate();
private delegate void UpdateTextDelegate(string text);
private static ConvertingScreen form;
public static void ShowScreen()
{
if (form != null) return;
form = new ConvertingScreen();
Thread thread = new Thread(new ThreadStart(ConvertingScreen.ShowForm));
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
}
private static void ShowForm()
{
if (form != null) Application.Run(form);
}
public static void CloseForm()
{
form?.Invoke(new CloseDelegate(ConvertingScreen.CloseFormInternal));
}
private static void CloseFormInternal()
{
if (form != null)
{
form.Close();
form = null;
}
}
public static void Update(string text)
{
if (form != null) form.UpdateLabel(text);
}
private void UpdateLabel(string text)
{
if (Invokerequired)
{
this.Invoke(new UpdateTextDelegate(UpdateLabel),new object[] { text });
return;
}
label1.Text = text;
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)