using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Data.SqlClient; using System.IO; using System.Threading; using System.Diagnostics;
namespace mook_AutoUpdate { public partial class Form1 : Form { private Thread myDownload; private string Constr = "server=localhost;uid=sa;pwd=1234;database=mook"; // This delegate enables asynchronous calls for setting // the text property on a TextBox control. delegate void SetProgCallback1( int totalLen, int Val); delegate void SetProgCallback2( int Val); delegate void SetProgCallback3();
public Form1() { InitializeComponent(); }
private void Exit() { // InvokeRequired required compares the thread ID of the // calling thread to the thread ID of the creating thread. // If these threads are different, it returns true. if (this.InvokeRequired) { SetProgCallback3 d = new SetProgCallback3(Exit); this.Invoke(d, new object[] {}); } else { this.Close(); } } private void DataDownLoading() { byte[] bytes = DatabaseDownload(1); var fs = new FileStream("mook_AutoMain.exe", FileMode.Create); fs.Write(bytes, 0, bytes.Length); fs.Close(); Exit(); //this.Close(); }
private void SetMax(int totalLen, int Val) { // InvokeRequired required compares the thread ID of the // calling thread to the thread ID of the creating thread. // If these threads are different, it returns true. if (this.ProgressBar.InvokeRequired) { SetProgCallback1 d = new SetProgCallback1(SetMax); this.Invoke(d, new object[] { totalLen, Val }); } else { this.ProgressBar.Maximum = totalLen; this.ProgressBar.Value = Val; } }
private void SetVal(int Val) { // InvokeRequired required compares the thread ID of the // calling thread to the thread ID of the creating thread. // If these threads are different, it returns true. if (this.ProgressBar.InvokeRequired) { SetProgCallback2 d = new SetProgCallback2(SetVal); this.Invoke(d, new object[] { Val }); } else { this.ProgressBar.Value = Val; } }
private byte[] DatabaseDownload(int M_Num) { using(var Conn = new SqlConnection(Constr)) { Conn.Open(); var sql = "select DataLength(M_File) From Table_File where M_NUM=@M_NUM"; SqlParameter param = new SqlParameter("@M_NUM", SqlDbType.Int); param.Value = M_Num; SqlCommand cmd = new SqlCommand(sql,Conn); cmd.Parameters.Add(param); int totalLength = Convert.ToInt32(cmd.ExecuteScalar()); //this.ProgressBar.Maximum = totalLength; //this.ProgressBar.Value = 0; this.SetMax(totalLength, 0); sql= "select M_File From Table_File where M_Num=@M_Num"; param = new SqlParameter("@M_Num", SqlDbType.Int); param.Value = M_Num; cmd = new SqlCommand(sql,Conn); cmd.Parameters.Add(param); SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess); reader.Read(); long remainder = totalLength; int bufferSize = 2048; if(totalLength < bufferSize) totalLength = bufferSize; byte[] buf = new byte[(int)totalLength]; int startIndex = 0; long retval = reader.GetBytes(0,startIndex, buf, 0, bufferSize); remainder -= retval; while(remainder >0) { startIndex += bufferSize; if(remainder < bufferSize) bufferSize = (int)remainder; retval = reader.GetBytes(0, startIndex, buf, startIndex, bufferSize); remainder -=retval; //this.ProgressBar.Value = startIndex; this.SetVal(startIndex); Thread.Sleep(100); } //this.ProgressBar.Value = totalLength; this.SetVal(totalLength); reader.Close(); return buf;
} }
private void Form1_Load(object sender, EventArgs e) { try { var tProcess = Process.GetProcessesByName("mook_AutoMain"); if (tProcess.Length == 1) tProcess[0].Kill(); } catch { } var fs = new FileStream("setup.txt", FileMode.Create); var sw = new StreamWriter(fs); sw.WriteLine(DataCheck()); sw.Close(); myDownload = new Thread(DataDownLoading); myDownload.Start(); }
private string DataCheck() { var Conn = new SqlConnection(Constr); Conn.Open(); var Comm = new SqlCommand("select * from Table_Update", Conn); var myRead = Comm.ExecuteReader(); if(myRead.Read()) { string Update = myRead["M_Date"].ToString(); myRead.Close(); Conn.Close(); return Update; } else { myRead.Close(); Conn.Close(); return "1"; } }
private void Form1_FormClosing(object sender, FormClosingEventArgs e) { if(this!=null) { myDownload.Abort(); var myProcess = new Process(); myProcess.StartInfo.FileName = "mook_AutoMain.exe"; myProcess.Start(); } } } } |