本文介绍了我如何开发自动关闭的Winform应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好



我想设计一个Windows窗体应用程序,完成任务后,自动关闭。



我不喜欢用户在表单构造函数中使用bellow代码,因为我需要表单加载并在执行任务之后以及显示结果之后将关闭:

Hi everybody

I want design a windows form app that after complete task, Automatically will be close.

I don't like user from bellow code in form constructor, because I need form be load and after execute tasks as well as show result after that will be close:

Application.Exit();





有关详细信息,此应用的任务是连接以指定Web服务并将数据发送到此。



坦克很多



For more information the task of this app is connecting to specify web service and sending data to this.

Tanks a lot

推荐答案

static class Program
{
     static void Main()
     {
          Application.EnableVisualStyles();
          Application.SetCompatibleTextRenderingDefault(false);
          MainForm mainForm = new MainForm();
          Application.Run(mainForm);
          mainForm.Close();
     }
}



完成任务后我需要从mainform退出。

此应用每小时执行一次和系统管理员必须看到这个结果,但有些时候可能是管理员不存在服务器前面。因此我需要自动关闭应用程序。




它不能像那样工作 - 你不能从Main方法关闭表单作为行


I need exit from mainform after finish task.
this app execute every hours and system admin must see this result, But some times may be admin doesn't exist front of server. therefore i need close app automatically."


It doesn't work like that - you can't close the form from the Main method as the line

Application.Run(mainForm);

在表单关闭之前不返回!



您必须从表单内部关闭表单 - 在任务完成时,或在任务完成时启动计时器,并在超时结束时调用Close(到如果他在那里,让操作员说10秒看结果)



我不能更准确,因为我不知道你是如何执行任务的表单。





我在表单构造函数中调用3个方法来执行任务,因此我知道任务何时完成。

但是我不能在构造函数中使用Close,因为在这种情况下完成任务后立即表单将被关闭。如果我在构造函数中关闭之前使用thread.sleep,那么在完成睡眠时间时不会显示表单。

我希望在完成该表单10秒后关闭后立即显示表单。





对!好吧,这取决于你的任务需要多长时间 - 如果它是几秒或更短,那么保持它如果没有,那么你应该考虑将处理代码移动到BackgroundWorker线程中以允许UI完成。这很容易做到:

Does not return until the form is closed!

You have to close the form from within the form itself - either when the task is finished, or by starting a timer when the task is complete, and calling Close when the time out has expired (to give the operator say 10 seconds to see the results if he is there)

I can't be more precise, because I have no idea how you execute your task within the form.


"I call 3 methods in form constructor for do tasks, therefore i know when tasks was finished.
But i can't use Close in constructor because in this situation after finish task immediately form will be closed. and if i use from thread.sleep before close in constructor therefore form doesn't display while finished sleep time.
I want display form immediately after finished that form close after 10 second.
"


Right! OK, this depends on how long your task will take - if it is a few seconds or less, then keep it as it is. If not, then you should look at moving the processing code into a BackgroundWorker thread to allow the UI to complete. It's pretty easy to do:

    BackgroundWorker worker = new BackgroundWorker();
    worker.DoWork += new DoWorkEventHandler(worker_DoWork);
    worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
    worker.RunWorkerAsync();
    }

void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
    myLabel.Text = "Completed sucessfully";
    Timer tim = new Timer();
    tim.Interval = 10000;       // 10 seconds in milliseconds
    tim.Tick += new EventHandler(tim_Tick);
    tim.Start();
    }

void tim_Tick(object sender, EventArgs e)
    {
    Close();
    }

void worker_DoWork(object sender, DoWorkEventArgs e)
    {
    // Call your methods here.
    }



如果您的任务很快,那么您可以立即从 worker_RunWorkerCompleted 方法中放入代码您的任务方法调用。


If your task is quick, then you could just put the code from the worker_RunWorkerCompleted method immediately after your task method calls.


{
    //TODO
    this.close();
}



GoodLuck,

z3ngew


GoodLuck,
z3ngew


private void Form1_Shown(object sender, EventArgs e)
{
   //your function
   //...

   this.Text = "close"; //replace text Form1 as the trigger closing application

}

private void Form1_TextChanged(object sender, EventArgs e)
{
    this.Close();
}


这篇关于我如何开发自动关闭的Winform应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 08:54