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

邮差的信 提交于 2019-12-13 07:39:49

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/2490389/how-can-i-run-some-common-code-from-both-a-scheduled-via-windows-task-b-ma

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!