在两个不同的文本框中转换两个txt文件,从小写到大写。

问题描述 投票:-1回答:1

我正在尝试转换两个不同的 .txt 文件从小写到大写,主要目的是测量和显示执行时间。

如果文件以大写保存在我预定义的路径中,一切都很顺利,程序也会显示执行时间。然而在我的GUI中,由于文本框中的以下异常,文本无法转换。

System.InvalidOperationException: Cross-thread operation not valid: Control "textBox2" accessed from a thread other than the thread it was created on.

namespace Threads
{
    public partial class Form1 : Form
    {   String prim= @"C:\Users\Wheelz\Desktop\Laborator09\fis1.txt";
        String secund= @"C:\Users\Wheelz\Desktop\Laborator09\fis2.txt";
        public Form1()'
        {
            InitializeComponent();
        }
    private void button1_Click(object sender, EventArgs e)
    {
        var read = File.ReadAllText(prim);
        textBox1.Text = read;

    }

    private void button2_Click(object sender, EventArgs e)
    {
        var read = File.ReadAllText(secund);
        textBox2.Text = read;

    }
    private void modifica1()
    {
        var read = File.ReadAllText(prim);
        read = read.ToUpper();
        File.WriteAllText(@"C:\Users\Wheelz\Desktop\Laborator09\fis1upper.txt", read);
        textBox1.Text = textBox1.Text.ToUpper();

    }
    private void modifica2()
    {

        var read = File.ReadAllText(prim);
        read = read.ToUpper();
        File.WriteAllText(@"C:\Users\Wheelz\Desktop\Laborator09\fis2upper.txt", read);
        textBox2.Text = textBox2.Text.ToUpper() ;


    }

    private void timp_Click(object sender, EventArgs e)
    {
        Thread firstThread = new Thread(new ThreadStart(modifica1));
        Thread secondThread = new Thread(new ThreadStart(modifica2));
        var ceas= new Stopwatch();
        ceas.Start();
        firstThread.Start();
        secondThread.Start();
        ceas.Stop();

        if (ceas.ElapsedMilliseconds == 1)
        {
            cron.Text = ceas.ElapsedMilliseconds.ToString() + "  milisecundă";
        }
        else
        {
            if ((ceas.ElapsedMilliseconds < 20))

                cron.Text = ceas.ElapsedMilliseconds.ToString() + "   milisecunde";

            else
                cron.Text = ceas.ElapsedMilliseconds.ToString() + "  de milisecunde";
        }



    }
}

}

c# multithreading visual-studio textbox
1个回答
1
投票

是的,你不能将mainthread中调用的表单共享到一个子线程中。

你必须使用一个Delegate到mainthread来更新文本框。

阅读。Invoke(Delegate)

Windows窗体中的控件被绑定到一个特定的线程上,并且不是线程安全的。因此,如果您要从不同的线程调用控件的方法,您必须使用控件的一个 invoke 方法将调用调集到合适的线程。这个属性可以用来确定是否必须调用调用方法,如果你不知道哪个线程拥有一个控件,这个属性是很有用的。

你也可以使用这个属性来确定是否必须调用 invoke 方法,如果你不知道哪个线程拥有一个控件,那么这个属性就会很有用。背景工作者异步


0
投票

使用 "BeginInvoke "来更新线程中的控制值,比如......。

 private void modifica1()
    {
        var read = File.ReadAllText(prim);
        read = read.ToUpper();
        File.WriteAllText(@"C:\Users\Wheelz\Desktop\Laborator09\fis1upper.txt", read);
this.BeginInvoke(new MethodInvoker(() =>
{ 
textBox1.Text = textBox1.Text.ToUpper();
}));
    }
    private void modifica2()
    {

        var read = File.ReadAllText(prim);
        read = read.ToUpper();
        File.WriteAllText(@"C:\Users\Wheelz\Desktop\Laborator09\fis2upper.txt", read);
        this.BeginInvoke(new MethodInvoker(() =>
{ 
textBox2.Text = textBox2.Text.ToUpper();
}));
    }
© www.soinside.com 2019 - 2024. All rights reserved.