IO中的跨线程错误 [英] Cross thread error in IO

查看:119
本文介绍了IO中的跨线程错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,伙计们!:玫瑰:
我在这里有一个问题;它是一个filewatcher程序:
您在文本框中输入路径,然后转到该路径并形成示例创建或删除文件夹,然后我要记录该路径
事件


hello good fellas!:rose:
I have a problem here ;it''s a filewatcher programm:
you enter a path in textbox,then go to that path and form example create or delete a folder then I want to log that
event


namespace filewatcherWFA
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        FileSystemWatcher fsw;

        public void WriteLog(string s)
        {
            // Get the Exact Location of the textFile
            string LogFilePath = Directory.GetParent(Application.ExecutablePath).ToString() + @"\Log.txt";

            //Create the StremWriter Object with FileName as Constuctor Argument and the Second Parameter is For

Weather Text is Appnded or Not
            StreamWriter sw = new StreamWriter(LogFilePath, true);

            sw.WriteLine(s);

            sw.Close();

          textBox2.Text = s+"\t";  //error occures here   ((Cross-thread)) !!

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {

            string path=textBox1.Text;
            fsw = new FileSystemWatcher(path);
            label1.Visible = true;
            label1.Text = "path entered !";
            fsw.EnableRaisingEvents = true;
            fsw.IncludeSubdirectories = true;
            fsw.Deleted += new FileSystemEventHandler(fsw_Deleted);
            fsw.Created += new FileSystemEventHandler(fsw_Created);
        }

        void fsw_Created(object sender, FileSystemEventArgs e)
        {
            WriteLog("created File :" + e.FullPath);
        }

        void fsw_Deleted(object sender, FileSystemEventArgs e)
        {
            WriteLog("Deleted File :" + e.FullPath);
        }



    }
}



但是当您要删除或创建文件夹时,它会显示::

<<跨线程操作无效:从创建该线程的线程之外的其他线程访问控件"textBox"

上.>>

如何解决该错误?:confused:



but when you are about to delete or create a folder it says::

<<Cross-thread operation not valid: Control ''textBox'' accessed from a thread other than the thread it was created

on.>>

how can I fix that error?:confused:

推荐答案

在创建FileSystemWatcher时添加行fsw.SynchronizingObject = this;.这将确保事件在您的Form线程上引发,并且跨线程错误将消失.

艾伦.
Add the line fsw.SynchronizingObject = this; when you create the FileSystemWatcher. This will ensure that the events are raised on your Form''s thread and the cross thread error will disappear.

Alan.


您可以使用委托从其他线程(而不是在其上创建的线程)访问控件.
参见 [
You can use a delegate to access controls from threads other than the one they were created on.
See this[^] simple example.


nimanaqipoor写道:
nimanaqipoor wrote:

<<跨线程操作=" not ="有效: =" control ="''文本框"="从="访问=""a ="线程=""other ="比=""the =""it =""was =" 已创建

on.=">>

我该如何解决该错误?

<<cross-thread operation="" not="" valid:="" control="" ''textbox''="" accessed="" from="" a="" thread="" other="" than="" the="" it="" was="" created

on.="">>

how can I fix that error?



不要访问来自其他线程的textbox.Text,因为您只能从创建它们的线程中访问控件. FileSystemWatcher使用不同的线程.

这意味着您将不得不在文本框中使用Invoke:
参见此处 [



Don''t access the textbox.Text from a different thread, because you can only access controls from the thread that created them. The FileSystemWatcher uses a different thread.

What that means is that you will have to use Invoke on the textbox:
see Here[^]


这篇关于IO中的跨线程错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆