Here is specifically what I am seeking to accomplish:
Using .Net 3.5, I have a Windows Service, within which I have a WCF Service running. I have a client, which requests a file from the WCF Service (Net.Tcp binding), which is then copied by the WCF service to a specified location, where it can then be edited/modified by the client. The WCF Service is operating in InstanceContextMode.PerSession mode.
While the file is being edited, I wish to lock the file on the machine where the WCF Service is hosted. Once the file is returned, I wish to then unlock the file again. I do not believe that I can lock the file within the WCF Service as the service session ends once the file has been copied and I assume that the lock on the file would not be retained at that point.
One solution (which I am not excited about), is to create a thread within the Windows service, which can maintain a List<> of files and lock/unlock these as required. Of course access to this list must be synchronized for multi-threaded access. This solution would require constant polling of lists to detect new files being added and files getting removed.
A preferred approach in my mind would be to trigger an event from within the WCF Service whenever a lock/unlock is required. As opposed to constant polling. The execution of the lock/unlock task would however need to occur on a different thread (e.g. a thread running within the context of the Windows service).
We do this with Windows Forms, where from a background thread we can invoke a method to be executed on the UI messaging thread. Is there a way to "invoke" a method on another thread (not the UI messaging thread)?
Any suggestions on how to best approach this?