12 Replies - 909 Views - Last Post: 11 October 2012 - 10:21 PM Rate Topic: -----

#1 November-06  Icon User is offline

  • D.I.C Regular

Reputation: 46
  • View blog
  • Posts: 391
  • Joined: 04-January 11

Scheduled Run of Windows App Method

Posted 29 August 2012 - 06:24 PM

I am not sure whether I should ask this here in the C# forum or in the SQL forum but my situation is this...

I created a small windows application with 4 buttons which generate XML files when clicked by the user. So, this should be manually triggered.

Now, the application is only to be used when a problem occurred in the automatic generation of files.

In the automatic generation, there will be no interaction from the user. The application is supposed to be scheduled to automatically run 4-5 times a day at specific times. During each run, the application should be able to perform all tasks performed by the 4 buttons so I thought of creating a method that will perform all their functions. For example, let's call it GenerateFiles().

Now, is there a way that I can run my Windows Application in a scheduled job specifically executing a method in it? In this example, how can I run my method, GenerateFiles(), with a scheduled task?

I searched in google but most say that I have to create a Console Application. I need to have the windows application since there will be manual usage of the program in case automation fails and its such a hassle in case of code changes to have two different applications for the manual and the automated.

This post has been edited by November-06: 29 August 2012 - 06:38 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Scheduled Run of Windows App Method

#2 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3469
  • View blog
  • Posts: 10,691
  • Joined: 05-May 12

Re: Scheduled Run of Windows App Method

Posted 29 August 2012 - 06:56 PM

They lied to you about needing a console program.

When your WinForms program runs, check the command line arguments. If your current Main() method doesn't take arguments, you can change the declaration to: void Main(string [] args). If you see particular command line, then call your GenerateFiles() method, and then exit. Obviously, you'll have to refactor your code such that GenerateFiles() lives in a class that is independent of your UI. But then you were practicing good engineering and keep UI and business logic separate, right?

And then the other part of the puzzle is creating a scheduled task. You can do that manually, by going to the Start Menu, typing in "Task Scheduler", and then Action.Create Basic Task... or Action.Create Task... The idea is to have the scheduled task run your application passing in the magic command line parameter.

There are Win32 COM API's for creating the tasks programatically. I don't know if there are managed wrappers for those APIs in the Windows API CodePack. I know at the time I wrote code to do something like this I had to resort to some COM interop and PInvoke because I also had to run on XP. I think that the Task Scheduler 2.0 object model should make InterOp a lot easier if your just targeting Win7 and above.

This post has been edited by Skydiver: 29 August 2012 - 07:02 PM

Was This Post Helpful? 2
  • +
  • -

#3 November-06  Icon User is offline

  • D.I.C Regular

Reputation: 46
  • View blog
  • Posts: 391
  • Joined: 04-January 11

Re: Scheduled Run of Windows App Method

Posted 29 August 2012 - 07:27 PM

My Data Access Layer and Business Logic Layer are placed in separate class libraries.

The GenerateFiles() method is inside the form.

Do you mean that I should create a new class and place my method there instead?
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3469
  • View blog
  • Posts: 10,691
  • Joined: 05-May 12

Re: Scheduled Run of Windows App Method

Posted 29 August 2012 - 07:50 PM

Yes.

Then the form code and the Main() code can both call the methods of that class. Something like:
static void Main(string [] args)
{
    var generator = new Generator();
    if (args.Contains("magic parameter"))
    {
        generator.GenerateFiles();
    }
    else
    {
        Application.Run(new MainForm(generator));
    }
}

:

partial class MainForm : Form
{
    public Generator _generator = null;

    public MainForm(Generator generator)
    {
        _generator = generator;

        InitializeComponents();
    }

    public MainForm()
    {
        // back up code to keep VS Designer users happy.
        _generator = new Generator();

        InitializeComponents();
    }

    :

    void btnGenerateFiles_Click(object sender, EventArgs e)
    {
        _generator.GenerateFiles();
    }
}


Was This Post Helpful? 1
  • +
  • -

#5 November-06  Icon User is offline

  • D.I.C Regular

Reputation: 46
  • View blog
  • Posts: 391
  • Joined: 04-January 11

Re: Scheduled Run of Windows App Method

Posted 29 August 2012 - 08:33 PM

What is the magic parameter for?

Is the Application.Run() the one executed on a scheduled job?
Was This Post Helpful? 0
  • +
  • -

#6 November-06  Icon User is offline

  • D.I.C Regular

Reputation: 46
  • View blog
  • Posts: 391
  • Joined: 04-January 11

Re: Scheduled Run of Windows App Method

Posted 29 August 2012 - 09:50 PM

Where should I place the static void main?

It has error when placed outside a class and when I created a class with the same name
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3469
  • View blog
  • Posts: 10,691
  • Joined: 05-May 12

Re: Scheduled Run of Windows App Method

Posted 29 August 2012 - 10:25 PM

Have you never looked inside the Program.cs that is part of your WinForms program before?
Was This Post Helpful? 0
  • +
  • -

#8 November-06  Icon User is offline

  • D.I.C Regular

Reputation: 46
  • View blog
  • Posts: 391
  • Joined: 04-January 11

Re: Scheduled Run of Windows App Method

Posted 30 August 2012 - 01:22 AM

I already created the separate class.

I just want to ask what is the magic parameter for? From my scheduled task, can I pass a parameter to my application?

Can you also tell me how to schedule the task? I was trying to create a schedule but an error is occurring after I click Finish. If I am not an admin in my computer, is it impossible for me to schedule tasks?
Was This Post Helpful? 0
  • +
  • -

#9 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3469
  • View blog
  • Posts: 10,691
  • Joined: 05-May 12

Re: Scheduled Run of Windows App Method

Posted 30 August 2012 - 02:02 AM

The magic parameter is something like "-q", "--GenerateFiles", "--DoNoHarm--", "Abracadabra", or "4-8-15-16-23-42". It's whatever you chose it to be.

Yes, you can pass a parameter to your application from a Scheduled Task.

What error are you getting after your click "Finish"? What did you enter for your scheduled task?

I know that I was able to create tasks not being an administrator using the COM API. I don't know if the GUI lets you.
Was This Post Helpful? 1
  • +
  • -

#10 November-06  Icon User is offline

  • D.I.C Regular

Reputation: 46
  • View blog
  • Posts: 391
  • Joined: 04-January 11

Re: Scheduled Run of Windows App Method

Posted 30 August 2012 - 02:19 AM

The error I get was:

The new task could not be created. Access is denied. Try using the Task page Browse button to locate the application.

I first tried running an executable file I created with VB.Net before and then tried to schedule launch of the usual applications (like Excel and reader) but I got the same error.

So maybe, it's really about the access?
Was This Post Helpful? 0
  • +
  • -

#11 November-06  Icon User is offline

  • D.I.C Regular

Reputation: 46
  • View blog
  • Posts: 391
  • Joined: 04-January 11

Re: Scheduled Run of Windows App Method

Posted 31 August 2012 - 01:07 AM

Thanks a lot Skydiver.

I created a setup project and installed the application in another computer and tried to schedule the task.

I passed a parameter just as you said and it works perfectly. Now, I have the option to run it automatically or manually.

Thank you very much. You have been a really great help.
Was This Post Helpful? 0
  • +
  • -

#12 November-06  Icon User is offline

  • D.I.C Regular

Reputation: 46
  • View blog
  • Posts: 391
  • Joined: 04-January 11

Re: Scheduled Run of Windows App Method

Posted 11 October 2012 - 06:19 PM

I have been able to run this program successfully for more than a month now thanks to Skydiver's help. But I have a new problem.

As I said before, I have the option to run the application manually and to run it automatically via Windows task Scheduler.

But just last week, there was a system slow down at the other server and the jobs that is supposed to run before mine wasn't completed so when mine was run, no data was retrieved.

The scenario is this.

Starting from 12 midnight, a job which is not mine will run and fill a table with data. Let's call that table as tblTransaction. The job will run every 4 hours.

About an hour after that job is run, tblTransaction is expected to be updated. We give another 30 minutes allowance after the expected data availability to run my job. So my job runs at 1:30 in the morning.

My job will check first if data is updated. To see, we check the date and time last modified. We put a condition on a range of time.

If the expected runtime of the first job is 12:00, we can run our job within the 4-hour time frame and the last date and time modified should be within that time frame.

So if I run on 1:30AM and the last update was 11:00PM last night, then the process will stop. No data will be retrieved since there is no new updates to tblTransaction. If the last modified is around 1:00AM or 1:15AM, then I can continue with my process.

So during slowdown what happens is that it may get updated later than 1:30 or later than any of the scheduled time. My job also runs every 4 hours after 1:30AM. It means that update can be around 2PM in which case, we are to trigger process manually to get the data.

Now, what happens is that they asked me to convert it to a windows service instead of a windows application. They said that they are going to set it so that after their job (which is the first to run) is executed, they will just call my service to be executed afterwards.

I am not sure how to do this. So will it mean that I have to create a separate service for the automatic running and a separate windows application to trigger process manually?

Will it be best to do separate programs that does almost the same things? How about maintenance? Is this the best advise for this kind of problem? I am not sure how to go with it so I really need some advice here.
Was This Post Helpful? 0
  • +
  • -

#13 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Scheduled Run of Windows App Method

Posted 11 October 2012 - 10:21 PM

Put all the functionality into a DLL. Create your service that calls the methods in the DLL, create your application to manually call the methods in the DLL. If you have to change the methods, then you only need to do it once (for the DLL).
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1