3

i am not really sure how to ask this question properly. so apologies in advance.

but it's basically around how to make an existing app, with a UI, run as a scheduled task with no UI.

background..

I have a winforms app written in vs2012 with 2 forms.

the first form is the login, straight forward, but currently expects user interaction of their username and password.

the second is the main form. which does the main work when you hit the "start" button.

what I am trying to achieve it is for me to send it some command line parameters that would run it without any ui as a scheduled task.

so, I'm guessing, I need get rid of needing the user to input login details. and somehow trigger the "start download" button and make it invisible.

I've worked out how to send command line parameters and can see how to get the software to do something different if it hears /silent but how do I hide the forms?

I'm lost.

any help would be much appreciated!

4

4 回答 4

4

C# 仍然有一个Main()功能。在标准的 winforms 解决方案中,它所做的只是创建您的 Form1(或任何它被重命名的),并使用该表单启动应用程序事件队列。

例如,它应该类似于:

public static void Main()
{
     Application.Run(new Form1());
}

修改此功能。如果您看到命令行参数,请做任何您必须做的事情。如果没有,就让它对表格进行正常的魔法。当你完成后,它会是这样的:

public static void Main(string[] args)
{
    if (args.Length > 0) {
        // log in
        // do all the necessary stuff
    } else {
        Application.Run(new Form1());
    }
}
于 2013-11-14T20:15:54.527 回答
3

修改Main应该是应用程序入口点的方法。如果有任何参数,您不需要实例化和显示任何表单,只需运行代码即可在没有 UI 的情况下完成您的工作。

    [STAThread]
    static void Main(string[] args)
    {
        if (args.Length > 0)
        {
            // silent mode - you don't need to instantiate any forms
        }
        else
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
于 2013-11-14T20:19:53.057 回答
2

在您的解决方案中找到静态 Main 方法。在该方法中,您将拥有 Application.Run(new Form()) 或 (form.Show() 或 ShowDialog())。所以他们的关键是传递一个参数,告诉你现在调用这个方法(在表单上显示方法)

关键是将您的业务逻辑放在一个独立于您的类中,并在您想要 GUI 或计划任务时使用该类

于 2013-11-14T20:18:14.497 回答
1

前几天我回答了这个问题——关于这个问题

注意代码块之间的区别——第一个代码块是无形的,第二个代码块是标准的。

  if (ABCFile > 0)
  {
    var me = new MainForm(); // instantiate the form
    me.NoGui(ABCFile); // call the alternate entry point
    Environment.Exit(0);
  }
  else
  {
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new MainForm());
  }
于 2013-11-14T20:18:04.690 回答