Im new to VS this is a .net windows application im using it as an mmorpg updater and launcher thing is it's UI freezes while downloading huge stuff i saw articles stating about DISPATCHER / BACKGROUND WORKER but as i mentioned im new to this and dont know where to add them or how every code in this has been got from hours of searching on google
using System;
using Microsoft.VisualBasic;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Xml;
using System.Net;
using System.IO;
using System.Threading;
using System.Globalization;
using System.ComponentModel;
namespace souloftalisman
{
public partial class Form1 : Form
{
protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
case 0x84:
base.WndProc(ref m);
if ((int)m.Result == 0x1)
m.Result = (IntPtr)0x2;
return;
}
base.WndProc(ref m);
}
public Form1()
{
this.TransparencyKey = Color.SaddleBrown;
this.BackColor = Color.SaddleBrown;
InitializeComponent();
}
private void timer1_Tick(System.Object sender, System.EventArgs e)
{
timer1.Enabled = false;
KillAppExe();
ProcessUpdate();
Environment.Exit(1);
}
int errorcount = 0;
string RemoteUri;
// the key used by the program when called back
string Key;
// to know that the program was launched by the
// Auto Update program
// the manifest file name
string ManifestFile;
// the User ID
// the command line passed to the original
string CommandLine;
string ExeFile;
public void runMain()
{
try
{
// Get the parameters sent by the application should be separated by “|”
ExeFile = "client.exe";
RemoteUri = "https://talisman-classic.in/updates";
ManifestFile = "/ServerManifest.xml";
// if Parameter omitted then application exit
if (string.IsNullOrEmpty(ExeFile) | string.IsNullOrEmpty(RemoteUri) | string.IsNullOrEmpty(ManifestFile))
Environment.Exit(1);
Application.DoEvents();
ProcessUpdate();
}
catch(WebException webex)
{
MessageBox.Show("Please Check Your Internet Connection And Restart Application");
Application.Exit();
this.Dispose();
this.Close();
}
catch (Exception ex)
{
MessageBox.Show("Unexpected Error Please Take a Screenshot And Report Error : "+ex.ToString());
Application.Exit();
}
}
public void ProcessUpdate()
{
WebClient myWebClient = new WebClient();
// Download manifest file
try
{
// get the update file content in manifest file
Label2.Text = "Connecting To Server ....Please Wait";
Application.DoEvents();
myWebClient.DownloadFile(RemoteUri + ManifestFile, Application.StartupPath + "\\" + ManifestFile);
Label2.Text = "Preparing For Download";
Application.DoEvents();
}
catch (WebException webex)
{
MessageBox.Show("Please Check Your Internet Connection And Restart Application");
Application.Exit();
this.Dispose();
this.Close();
}
catch (Exception ex)
{
MessageBox.Show("Unexpected Error Please Take a Screenshot And Report Error : "+ex);
Application.Exit();
this.Dispose();
this.Close();
}
try
{
XmlDocument m_xmld;
XmlNodeList m_nodelist;
//Create the XML Document
m_xmld = new XmlDocument();
//Load the Xml file
m_xmld.Load(Application.StartupPath + "\\" + ManifestFile);
//Get the list of name nodes
m_nodelist = m_xmld.SelectNodes("/update/name");
//Init progressbar
InitProgress(m_nodelist.Count);
DateTimeFormatInfo dtf = new DateTimeFormatInfo();
dtf.FullDateTimePattern = "dd/MM/yyyy h:mm:ss tt";
IFormatProvider ifp = dtf;
//Loop through the nodes
foreach (XmlNode m_node in m_nodelist)
{
Application.DoEvents();
//Get the file Attribute Value
string fileAttribute = m_node.Attributes.GetNamedItem("file").Value;
//Get the fileName Element Value
string fileNameValue = m_node.ChildNodes.Item(0).InnerText;
//Get the fileVersion Element Value
string fileVersionValue = m_node.ChildNodes.Item(1).InnerText;
//Get the fileLastModified Value
string fileLastModiValue = m_node.ChildNodes.Item(2).InnerText;
string fileSize = m_node.ChildNodes.Item(3).InnerText;
//Temp file name
string TempFileName = Application.StartupPath + "\\tmp\\" + DateTime.Now.TimeOfDay.TotalMilliseconds;
if (!Directory.Exists(Application.StartupPath + "\\tmp\\"))
{
Directory.CreateDirectory(Application.StartupPath + "\\tmp\\");
}
if (File.Exists(TempFileName))
{
TempFileName = Application.StartupPath + "\\tmp\\" + DateTime.Now.TimeOfDay.TotalMilliseconds+"123";
}
bool isToUpgrade = false;
string RealFileName = Application.StartupPath + "\\" + fileNameValue.Replace("\\patch", "");
System.DateTime LastModified = DateTime.ParseExact(fileLastModiValue, "dd/MM/yyyy h:mm:ss tt", ifp);
bool DirectoryExists = Directory.Exists(RealFileName.Substring(0, RealFileName.LastIndexOf("\\")));
bool FileExists = File.Exists(RealFileName);
if (!DirectoryExists)
{
Directory.CreateDirectory(RealFileName.Substring(0, RealFileName.LastIndexOf("\\")));
}
//If file not exist then download file
if (!FileExists)
{
isToUpgrade = true;
}
else if (!string.IsNullOrEmpty(fileVersionValue))
{
//verify the file version
FileVersionInfo fv = FileVersionInfo.GetVersionInfo(RealFileName);
Version v1 = new Version(GetVersion(fileVersionValue));
Version v2 = new Version(GetVersion(fv.FileMajorPart + "." + fv.FileMinorPart + "." + fv.FileBuildPart + "." + fv.FilePrivatePart));
isToUpgrade = (v1 > v2);
//check if version not upgrade then check last modified
if (!isToUpgrade)
{
isToUpgrade = (LastModified > File.GetLastWriteTimeUtc(RealFileName));
}
}
else if (!isToUpgrade)
{
//check last modified file
isToUpgrade = (Convert.ToDouble(fileSize) != new FileInfo(RealFileName).Length);
}
else
{
}
//Download upgrade file
if (isToUpgrade)
{
Label2.Text = fileNameValue.Replace("\\patch", "") + "...";
Label1.Text = "Updating...";
label3.Text = "Error Count" + errorcount;
Application.DoEvents();
// Download file and name it with temporary name
myWebClient.DownloadFile(RemoteUri + fileNameValue.Replace("\\", "/"), TempFileName);
// Rename temporary file to real file name
File.Copy(TempFileName, RealFileName, true);
// Set Last modified
File.SetLastWriteTimeUtc(RealFileName, LastModified);
// Delete temporary file
File.Delete(TempFileName);
//Process.Start(RealFileName, "");
}
IncrementProgress();
Application.DoEvents();
}
//Delete server manifest file
File.Delete(Application.StartupPath + "\\" + ManifestFile);
var dir = Directory.GetCurrentDirectory() + @"\tmp";
Directory.Delete(dir, true);
Label1.Text = "Client is Now Updated!";
Label2.Text = "";
Application.DoEvents();
Process.Start("client.exe", "ur;name=test;ip=192.241.235.212;port=8000");
Application.Exit();
this.Dispose();
this.Close();
}
catch (WebException webex)
{
errorcount++;
}
catch (IOException ioex)
{
errorcount++;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
Application.Exit();
this.Dispose();
this.Close();
}
}
private string GetVersion(string Version)
{
string[] x = Version.Split('.');
return string.Format("{0:00000}{1:00000}{2:00000}{3:00000}", Convert.ToInt32(x[0]), Convert.ToInt32(x[1]), Convert.ToInt32(x[2]), Convert.ToInt32(x[3]));
}
private void InitProgress(long lMax)
{
ProgressBar1.Value = 0;
ProgressBar1.Maximum = Convert.ToInt32(lMax);
Application.DoEvents();
}
private void IncrementProgress()
{
var _with1 = ProgressBar1;
if (_with1.Value < _with1.Maximum)
_with1.Value = _with1.Value + 1;
Application.DoEvents();
}
public void KillAppExe()
{
// Get MainApp exe name without extension
string AppExe = ExeFile.Replace(".exe", "");
Process[] local = Process.GetProcesses();
int i = 0;
// Search MainApp process in windows process
for (i = 0; i <= local.Length - 1; i++)
{
// If MainApp process found then close or kill MainApp
if (local[i].ProcessName.ToUpper() == AppExe.ToUpper())
{
local[i].CloseMainWindow();
}
}
}
private void button4_Click(object sender, EventArgs e)
{
//ThreadStart ts=new ThreadStart (runMain);
//Thread t = new Thread(ts);
//t.Start();
button4.Visible = false;
runMain();
}
private void button4_MouseEnter(object sender, EventArgs e)
{
button4.BackgroundImage = souloftalisman.Properties.Resources.btnUpdateHover;
}
private void button4_MouseLeave(object sender, EventArgs e)
{
button4.BackgroundImage = souloftalisman.Properties.Resources.btnUpdate;
}
private void button3_Click(object sender, EventArgs e)
{
Application.Exit();
this.Dispose();
this.Close();
}
private void button3_MouseEnter(object sender, EventArgs e)
{
// button3.BackgroundImage = souloftalisman.Properties.Resources.btnQuitHover;
}
private void button3_MouseLeave(object sender, EventArgs e)
{
// button3.BackgroundImage = souloftalisman.Properties.Resources.btnQuit;
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
System.Diagnostics.Process.Start("http://www.fb.com/GanstaEditor");
}
private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
System.Diagnostics.Process.Start("https://www.talisman-classic.in/");
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
}
private void label4_Click(object sender, EventArgs e)
{
Application.Exit();
this.Dispose();
this.Close();
}
}
}
Please let me know how to do it if any1 is willing to help me LIVE please give me a message on bbharat95@gmail.com