views:

22

answers:

1

Hi,

QUESTION - How can I run some common code from both (a) scheduled via Windows Task & (b) manually from within WinForms app?

BACKGROUND:

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 sound 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

+3  A: 

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.

Fredrik Mörk
thanks Fredrik - so the common point here for me would be the sqlite database, so I guess with this approach then I wouldn't need WCS or remoting then? I would just put the code to use the database in the common class library and the database would be the common point?
Greg
do you think it's worth going down this path (breaking up my existing one winforms app) just to allow me to use the Schedule Task Manager to do scheduling (c.f. using the Timer class and writing my own date/time checks for scheduling)
Greg
@Greg; yes I think it would be worth it. When I write software (even for my own use) I tend to split the solutions up into logical assemblies in similar ways.
Fredrik Mörk
A great thing when splitting up the 'logical assemblies' as @Fredrik mentions is that you can probably reuse the code from one part in some other project later, in effect creating modules that you can pick from which will reduce your " *copy paste coding* ".
Patrick