0

this is my first question on stackoverflow, therefore errors in asking-style aren't on purpose.

I'm both, new to C# as to the concept of event-handling, so I would like to know, if there is any possibility to have the value of an ToolStripButtonItem-attribute changed by an EventHandler.

The context is the following:

The code starts by initializing the UI which contains some Windows.Forms- Elements. The ToolStripButtomItem that is of interest for me, has it's Enabled-attribute set to false as default-value. The functionality of this button is to switch into a comparision-view as soon as a certain reference file exists. This allready can be the case when the programm-start, otherwise the reference file might be created during runtime. Of course, you could perform

Button.Enabled=System.IO.File.Exists(Reference-File) 

with the initilization and than do something like

CreateFile(ReferenceFile){
    ...
    Button.Enabled = true;
}

but this seems rather crude to me.

Instead I would like to something like:

Button.Enabled = new System.EventHandler(this.EnableButton);

with

private void EnableButton(Object sender, EventArgs e){
  if(System.IO.FileExists(ReferenceFile)
  Button.Enabled = true; 
}  

What I intend is, to have the button get enabled as soon as the reference-file existst. There are multiple ways to create the reference-file, and there are goint to be even more in the future. To avoid setting the enable-value in each of those createReferenceFile()-Methods, the concept of EventHandling seems quite like the deal to me.

The program I'm trying to run is quite comprehensive, so "polling" is no option at this place.

krouch
  • 105
  • 9

1 Answers1

0

I suggest to use the FileSystemWatcher and set the enabled property every time it goes of.

    private void StartListening(string path)
    {
        var watch = new FileSystemWatcher();
        watch.Path = path;
        watch.Filter = "*.*";
        watch.Created += UpdateState;
        watch.Deleted += UpdateState;
    }

    void UpdateState(object sender, FileSystemEventArgs e)
    {
        MyButton.Enabled = File.Exists(@"C:\Folder\File.txt");
    }

PS: this is just some very basic example code, you will need to make sure that you have a reference to MyButton and have the correct path there as well ...

  • Hey, thanks for the answer. Hence the programm is quite comprehensive and performs bilateral changes in the file-code and the associated visualization as well, it would be interessting to know, how ressource-intensive your suggestion is? It looks a little bit like default "polling", which I would like to avoid at all. – krouch Jul 08 '15 at 12:02
  • Not at all, as far as I know it is pure event driver. So no polling. The only problem you may run into is if there would be to many changes in a short time, because then you could miss one. (Changes are buffered and handled one by one with a max of ~128 items in the buffer I think.) I do not know the details though, this is just something I remember ... PS, you should probably use `MyButton.InvokeRequired()` and `MyButton.Invoke(new Action(() => UpdateState(sender, e)));` because I think the events may originate from another thread. – Frederick Grumieaux Jul 08 '15 at 12:13
  • Seems to me, like `FileSystemWatcher` is pretty unstable and likely to fail, which would not be acceptable at this point. – krouch Jul 08 '15 at 13:51
  • And what makes you think that? – Frederick Grumieaux Jul 08 '15 at 15:13
  • The FileSystemWatcher was nothing I knew, until you proposed it as an probably fitting concept. So I did a little research and came up with this : http://stackoverflow.com/questions/239988/filesystemwatcher-vs-polling-to-watch-for-file-changes – krouch Jul 08 '15 at 15:16
  • Yep there are some issues. If you think it is realistic that lots of changes could happen in a short time you will need to combine it with a polling mechanism to be sure. Still it is the only way I know of that can give you real time & no polling results. (PS: about the leaking -> don't forget to unsubscribe from the events if you stop using it ! ) (And that is a general warning if you are working with events and not familiar with them) – Frederick Grumieaux Jul 08 '15 at 15:29