C# 运行时发生System.InvalidOperationException错误

在调试程序时,在以下句子出现错误

private void th()
        {
            Speech speech = new Speech();
            speech.Save("1.mp3",textBox1.Text, trackBar1.Value, trackBar2.Value);
        }

错误提示:
System.InvalidOperationException:“Cross-thread operation not valid: Control ‘trackBar1’ accessed from a thread other than the thread it was created on.”
在这里插入图片描述
解决方法有两种:
1、在程序初始化模块

        public Form1()
        {
            InitializeComponent();
        }

输入以下语句:

CheckForIllegalCrossThreadCalls = false;

2、将出错语句放在下面代码的大括号里

this.BeginInvoke(new Action(delegate()
                {
                
                }));

猜你喜欢

转载自blog.csdn.net/weixin_46403483/article/details/107567919