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,怎么可能报那样的错。

    评论

报告相同问题?

悬赏问题

  • ¥88 实在没有想法,需要个思路
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)