QUESTION - How can I run some common code from both (a) scheduled via Windows Task & (b) manually from within WinForms app?
BACKGROUND:
- This follows on from the How can I schedule tasks in a WinForms app? thread
REQUIREMENTS
- C# .NETv3.5 project using VS2008
- There is an existing function which I want to run both (a) manually from within the WinForms application, and (b) scheduled via Windows Task.
APPROACHES
- So what I'm trying to understand is what options are there to make this work eg
- Is it possible for a windows task to trigger a function to run within a running/existing WinForms application? (doesn't so开发者_如何学运维und solid I guess)
- Split code out into two projects and duplicate for both console application that the task manager would run AND code that the winforms app would run
- Create a common library and re-use this for both the above-mentioned projects in the bullet above
- Create a service with an interface that both the task manager can access plus the winforms app can manage
Actually each of these approaches sounds quite messy/complex - would be really nice to drop back to have the code only once within the one project in VS2008, the only reason I ask about this is I need to have a scheduling function and the suggestion has been to use http://taskscheduler.codeplex.com/ as the means to do this, which takes the scheduling out of my VS2008 project...
thanks
I would definitely go for the approach to put the functionality into a common class library, and use that library in a console application (that is run as a scheduled task) and in a winforms application that can be run by the user. I have done this a numerous occasions, and I don't find it messy or complex at all. Quite the opposite; since you separate the functionality from how it interacts with the "user", you might even end up with cleaner code.
精彩评论