none
How to use thread safe calls when utilising the SystemWatcher class RRS feed

  • Question

  • Hi,

    I have a watcher class monitoring a folder for the arrival of new files when said files arrive I am reading them and sending certain data from the files to an SQL server database.

    private void btnStart_Click ( object sender , EventArgs e ) { SystemSounds.Beep.Play ( ); if ( _isWatching ) { lblStatus.ForeColor = Color.Red; lblStatus.Text = "RESTING"; _isWatching = false ; btnStart.Text = "Start Watching" ; } else { lblStatus.ForeColor = Color.Green; lblStatus.Text = "ACTIVE"; _isWatching = true ; btnStart.Text = "Stop Watching" ; RunWatcher ( ); } } public void RunWatcher( ) { FileSystemWatcher watcher = new FileSystemWatcher{Path = _dirName, Filter = "*.txt" } ; watcher.Created += NewFileArrived; watcher.EnableRaisingEvents = true; listBox1.Items.Insert( 0, "Watching" ) ; }

    public void NewFileArrived ( object sender, FileSystemEventArgs e )

    {

    //process the file and send data to database

    }

    The problem I have is, in the NewFileArrived method I am processing the file and want to update the listBox at various times during the processing. For example, if the file StartsWith(something) process it else{delete file} then listBox1.Items.Insert("File Deleted" + FileName);

    The error I get is:

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



    CuriousCoder


    Wednesday, July 11, 2018 12:19 PM

Answers

  • Usually this kind of problems can be solved with:

      listBox1.BeginInvoke( new Action( ( ) => listBox1.Items.Insert( 0, "Watching" ) ) );



    • Edited by Viorel_MVP Wednesday, July 11, 2018 3:59 PM
    • Marked as answer by CuriousCoder15 Thursday, July 12, 2018 10:47 AM
    Wednesday, July 11, 2018 3:59 PM

All replies

  • I'm thinking I could access the ListBox using a delegate.

    public delegate void myDelegate( string pMessage ) ;
    		public void NotifyMe( string message )
    		{
    			listBox1.Items.Insert (0, message ) ;
    			listBox1.Refresh( ) ;
    		}
    
    myDelegate MyDel = new myDelegate (NotifyMe) ;
     
    Does that seem possible?



    CuriousCoder

    Wednesday, July 11, 2018 3:35 PM
  • Usually this kind of problems can be solved with:

      listBox1.BeginInvoke( new Action( ( ) => listBox1.Items.Insert( 0, "Watching" ) ) );



    • Edited by Viorel_MVP Wednesday, July 11, 2018 3:59 PM
    • Marked as answer by CuriousCoder15 Thursday, July 12, 2018 10:47 AM
    Wednesday, July 11, 2018 3:59 PM
  • Hi,

    >>'Cross-thread operation not valid: Control 'listBox2' accessed from a thread other than the thread it was created on.'

    To make a thread-safe call to a Windows Forms control

        Query the control's InvokeRequired property.
        If InvokeRequired returns true, call Invoke with a delegate that makes the actual call to the control.
        If InvokeRequired returns false, call the control directly.

    The following documents may help you for more details:

    How to: Make Thread-Safe Calls to Windows Forms Controls

    Cross-thread operation not valid: Control '' accessed from a thread other than the thread it was created on [duplicate]

    Regards,

    Stanly


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Edited by Stanly Fan Thursday, July 12, 2018 7:17 AM
    Thursday, July 12, 2018 7:16 AM
  • The reply from Viorel takes care of the cross-thread problem, but you could possibly have another problem when processing your files. Depending on the size of the files being put in your folder, you might have file access problems (the file is still being copied, while you are trying to open it to process it). I have a blog post I posted a few years ago about a sub-class I wrote to handle this situation:

    https://geek-goddess-bonnie.blogspot.com/2014/08/filesystemwatcher-improvements.html


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Sunday, July 15, 2018 10:39 PM
    Moderator