Click here to Skip to main content
15,889,874 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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


C#
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);
        }



    }
}


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:
Posted

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.
 
Share this answer
 
You can use a delegate to access controls from threads other than the one they were created on.
See this[^] simple example.
 
Share this answer
 
nimanaqipoor wrote:
<<cross-thread operation="" not="" valid:="" control="" 'textbox'="" accessed="" from="" a="" thread="" other="" than="" the="" it="" was="" created

on.="">>

how can I fix that error?


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[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900