freedom_wings 2016-04-12 13:52 采纳率: 21.1%
浏览 1386

c#,winform跨线程更改ui组件,出错,求大神帮帮忙

以下是截图:
图片说明

源码:

 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 WindowsFormsApplication1
{


    public partial class Form1 : Form
    {
        private delegate void d(int a, int b);
        d dt;
        public Form1()
        {
            InitializeComponent();

        }

        private void button1_Click(object sender, EventArgs e)
        {
            int a = int.Parse(textBox1.Text);
            int b =int.Parse(textBox2.Text);
            Thread t = new Thread(()=>ThreadTest(a,b));
            t.Start();

            textBox1.Text = "a";
        }

        private void Result(int a,int b)
        {
            label1.Visible = true;
            label1.Text = Convert.ToString(a, b);
        }

        private void ThreadTest(int a,int b)
        {
            if (label1.InvokeRequired)
            {
                while (!label1.IsHandleCreated)
                {
                    if (label1.Disposing || label1.IsDisposed)
                        return;

                }
                dt = new d(Result);
                label1.Invoke(dt, new object[] { a, b });
            }
            else label1.Text = Convert.ToString(a + b);

        }
    }


}

运行的时候
图片说明

按了button后:
图片说明
一直这样,有时还会在int a=Convert.ToInt32(textBox1.Text)报字符串转换为datetime应先分析什么什么的,我这里根本就没转为字符串格式。
求大神指点指点

  • 写回答

2条回答 默认 最新

  • threenewbee 2016-04-12 15:36
    关注

    贴出报错信息,按照你的图看,textBox1.Text如果是a的话,那么肯定不能转换为Int32
    但是你的代码根本没有什么DateTime,怎么可能报那样的错。

    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?