0

I am making an application that checks if there are files in a certain folder, if there is, it un-minimizes the application.

System.Timers.Timer aTimer = new System.Timers.Timer();
aTimer.Elapsed += new ElapsedEventHandler(tick);
aTimer.Interval = updateIterval*1000;
aTimer.Enabled = true;

 public void tick(object source, ElapsedEventArgs e)
    {
        update();
    }
 public void update()
    {
        MessageBox.Show("Tick");
        if (WorkingFiles.Length != 0)
        {
            this.WindowState = FormWindowState.Normal;
            MessageBox.Show("Normal");
        }
        else
        {
            this.WindowState = FormWindowState.Minimized;
            MessageBox.Show("Minimized");
        }
    }

I will only get the "TICK" message every 10 seconds, and I wont get either of the "Normal", or "Minimized" but if I call this function via button press it works perfectly fine. not sure if there is something inherently wrong with the way I am doing this, or if I could do it another way?

1
  • 3
    Just out of curiosity, do you know about the FileSystemWatcher class ? Commented Mar 18, 2014 at 1:20

3 Answers 3

3

You are trying to access the UI thread from a background thread. In your instance I would suggest using system.windows.forms.timer instead of System.Timers.Timer

Sign up to request clarification or add additional context in comments.

4 Comments

Good point. This explains why the OP's code works when clicking the button, but fails when using the timer. Nice catch. +1
but in the question it says, first tick message is shown. i.e. if this is the case, neither of the messages should be shown.
@Dreamer Its not the MessageBox call that is causing the error, it is him trying to change the window state.
k, you mean that statement itself through invalid operation exception. make sense then.
2

Regarding the functionality you want to achieve "I am making an application that checks if there are files in a certain folder" and because you have asked for other ways to do achieve that. I would suggest you using the FileSystemWatcher Class:

"Listens to the file system change notifications and raises events when a directory, or file in a directory, changes."

Code Example also extracted from the MSDN link and modified to your needs:

FileSystemWatcher watcher;

public void StartWatch()
{
    // Create a new FileSystemWatcher and set its properties.
    watcher = new FileSystemWatcher();
    watcher.Path = "Path to directory"; // Put the path to your directory here
    // Watch for changes in LastWrite
    watcher.NotifyFilter = NotifyFilters.LastWrite;

    // Add event handlers.
    watcher.Created += new FileSystemEventHandler(OnCreated);

    // Begin watching.
    watcher.EnableRaisingEvents = true;

}

// Define the event handlers. 
private static void OnCreated(object source, FileSystemEventArgs e)
{
    MessageBox.Show("A File has been created");
}

1 Comment

Thank you, I did not think there was a way to do this, I will look at dropping this into my code.
0

Most likely problem with WorkingFiles being null (or some other less obvious exception) - add try/catch and show exception information if one happens.

MessageBox.Show is blocking call - so second half of the function will not run till you "OK" the dialog box and as result it is not the best way to debug/trace. Consider using Debug.Trace to output messages to VS debug window.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.