将C#控制台应用程序转换为Windows窗体应用程序 [英] Convert C# console application to a windows forms application

查看:191
本文介绍了将C#控制台应用程序转换为Windows窗体应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  class  SQLJob 
{


#region用户变量

const int timeoutInterval = 60 ; // 设置超时以秒为单位
静态 readonly string SqlServer = @ Server112 \Dev,20481; // 设置SqlServer,也可以使用实例/端口,例如:USPLSVUL156 \Operations,20481
static readonly string SqlAgentJobName = Some_SqlAgentJob_J250; // 设置要解雇的工作的名称

< span class =code-region> #endregion


static bool loopContinuity = false ;
静态计时器stateTimer;
static int CurrentRunRetryAttempt = 0 ;
static ServerConnection conn;
静态服务器服务器;
静态工作职位;

public static void Main( string [] args)
{
// < span class =code-comment>启用计时器
SetTimer();
try
{
conn = new ServerConnection(SqlServer); // 创建SQL server conn,Windows身份验证
server = 服务器(conn); // 连接SQL Server
job = server.JobServer.Jobs [SqlAgentJobName]; // 获取指定的作业
StartJob();
}
catch (例外情况)
{
SetTimer( true );
Console.WriteLine( 无法启动作业: + ex.Message);
throw ex;
}
最后
{
Destroyobjects();
}

}

静态 void Destroyobjects()
{
if (job!= null
job = null ;
if (server!= null
server = ;
if (conn!= null
{
conn。断开();
conn = null ;
}
}

私有 静态 void SetTimer( bool cancel = false
{
if (!cancel) // 启动
{
stateTimer = 计时器(timeoutInterval * 1000 ); // 将超时间隔设置为timeoutInterval
stateTimer.Enabled = true ; // 启用计时器
stateTimer .Elapsed + = new ElapsedEventHandler(Tick); // 设置计时器已用事件处理程序

}
else // 禁用计时器
{
if (stateTimer!= null
stateTimer.Dispose();

}
}

静态 public void Tick( object source,ElapsedEventArgs e)
{

loopContinuity = true ; // 正常停止.. 。
Console.WriteLine( string .Format( 超时达到{0} {1} CurrentRunRetryAttempt = {2},e.SignalTime,Environment.NewLine,CurrentRunRetryAttempt));
throw new 异常( string .Format( 超时时间达到{0} {1} CurrentRunRetryAttempt = {2},e .SignalTime,Environment.NewLine,CurrentRunRetryAttempt)); // 如果我们不想突然停止,请评论此行
}

静态 void StartJob()
{

尝试
{
,而 (loopContinuity == false // 等到工作闲置
{
job.Refresh();
if (job.CurrentRunStatus == JobExecutionStatus.Executing) // 检查工作状态并查找它是否正在运行
{
CurrentRunRetryAttempt ++;
// 我们还没准备好解雇工作
loopContinuity = ;
System.Threading.Thread.Sleep( 10 * 1000 ); // 等待10秒再继续检查。
}
else
{
// 我们准备解雇这份工作
loopContinuity = true ; // 设置循环退出
尝试
{
job.Start(); // 开始工作
SetTimer( true ); // 禁用计时器,如果我们能够开始工作,即启动工作时没有例外。
}
catch
{
loopContinuity = false ; // 无法启动,继续循环。
System.Threading.Thread.Sleep( 10 * 1000 ); // 无法启动,等待10秒再试一次
}

}
string s = CurrentRunStatus = + job.CurrentRunStatus.ToString();
Console.WriteLine(s); // 打印状态

}
}
catch
{
throw ;
}
}
}

})



我已将所有必需的名称空间添加到表单。

解决方案

首先,您需要转到Project Properties并将Output更改为Windows Application。



第二步 - 你需要添加一个表格。

在主入口点,加载新创建的表格。



取所有来自主入口点的代码并将其添加到form_load事件中。



如果您需要添加控件,您可以添加到win表单并更新代码。

编译,你就完成了。



祝你好运!



欢呼

class SQLJob
    {


        #region User Variables

        const int timeoutInterval = 60;//Set Timeout in seconds
        static readonly string SqlServer = @"Server112\Dev,20481"; //set SqlServer, may use instance/port too eg: USPLSVUL156\Operations,20481
        static readonly string SqlAgentJobName = "Some_SqlAgentJob_J250"; //set name of the job to fire

        #endregion


        static bool loopContinuity = false;
        static Timer stateTimer;
        static int CurrentRunRetryAttempt = 0;
        static ServerConnection conn;
        static Server server;
        static Job job;

        public static void Main(string[] args)
        {
            //Enable Timer
            SetTimer();
            try
            {
                conn = new ServerConnection(SqlServer); //Create SQL server conn, Windows Authentication
                server = new Server(conn); //Connect SQL Server
                job = server.JobServer.Jobs[SqlAgentJobName]; //Get the specified job
                StartJob();
            }
            catch (Exception ex)
            {
                SetTimer(true);
                Console.WriteLine("Failed to start the job :" + ex.Message);
                throw ex;
            }
            finally
            {
                Destroyobjects();
            }

        }

        static void Destroyobjects()
        {
            if (job != null)
                job = null;
            if (server != null)
                server = null;
            if (conn != null)
            {
                conn.Disconnect();
                conn = null;
            }
        }

        private static void SetTimer(bool cancel = false)
        {
            if (!cancel)//Initiate
            {
                stateTimer = new Timer(timeoutInterval * 1000);//Set Timeout interval as timeoutInterval
                stateTimer.Enabled = true;//Enable timer
                stateTimer.Elapsed += new ElapsedEventHandler(Tick); //Set timer elapsed event handler

            }
            else //Disable timer
            {
                if (stateTimer != null)
                    stateTimer.Dispose();

            }
        }

        static public void Tick(object source, ElapsedEventArgs e)
        {

            loopContinuity = true;//normal stop...
            Console.WriteLine(string.Format("Timeout reached at {0){1}CurrentRunRetryAttempt={2}", e.SignalTime, Environment.NewLine, CurrentRunRetryAttempt));
            throw new Exception(string.Format("Timeout reached at {0){1}CurrentRunRetryAttempt={2}", e.SignalTime, Environment.NewLine, CurrentRunRetryAttempt));// comment this line if we do not want an abrupt stop
        }

        static void StartJob()
        {

            try
            {
                while (loopContinuity == false) //Wait till the job is idle
                {
                    job.Refresh();
                    if (job.CurrentRunStatus == JobExecutionStatus.Executing) //Check Job status and find if it’s running now
                    {
                        CurrentRunRetryAttempt++;
                        //We are not ready to fire the job
                        loopContinuity = false;
                        System.Threading.Thread.Sleep(10 * 1000); //Wait 10 secs before we proceed to check it again.
                    }
                    else
                    {
                        //We are ready to fire the job
                        loopContinuity = true; //Set loop exit
                        try
                        {
                            job.Start();//Start the job
                            SetTimer(true);//disable timer if we are able to start the job, i.e. there’s no exception on starting the job.
                        }
                        catch
                        {
                            loopContinuity = false; //Fail to start, continue to loop.
                            System.Threading.Thread.Sleep(10 * 1000); //Fail to start, wait 10 seconds and try again
                        }

                    }
                    string s = "CurrentRunStatus=" + job.CurrentRunStatus.ToString();
                    Console.WriteLine(s); //Print status             

                }
            }
            catch
            {
                throw;
            }
        }
    }

})


I have added all required name spaces to the form.

解决方案

First you need to go to Project Properties and change the "Output" to Windows Application.

Second step - you need to add a Form.
In main entry point, load the newly created form.

Take all the codes from main entry point and add it in form_load event.

If you need tyo add control, you can add to win form and update the code.
Compile and you are done.

Best of luck!

cheers


这篇关于将C#控制台应用程序转换为Windows窗体应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆