vorweg: ich bin komplett neu in C#, und hab davon eig gar keine Ahnung.
Nun wollte ich gerne für mein P-Server Projekt einen Patcher verwenden, der Open Source ist in C#.
Funktioniert alles soweit Top, ausser das ab und zu der Fehler
Code:
System.InvalidOperationException wurde nicht behandelt.
HResult=-2146233079
Message=Das Objekt wird bereits an anderer Stelle verwendet.
Source=System.Drawing
StackTrace:
bei System.Drawing.Graphics.CheckErrorStatus(Int32 status)
bei System.Drawing.Graphics.DrawImage(Image image, Int32 x, Int32 y, Int32 width, Int32 height)
bei System.Drawing.Graphics.DrawImage(Image image, Rectangle rect)
bei System.Windows.Forms.PictureBox.OnPaint(PaintEventArgs pe)
bei System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer)
bei System.Windows.Forms.Control.WmPaint(Message& m)
bei System.Windows.Forms.Control.WndProc(Message& m)
bei System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
bei System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
bei System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
InnerException:
Um genau zu sein betrifft es die Progress Bar des Patchers (es gibt eine für die jeweilige heruntergeladene Datei und eine für den Gesamt-Status.)
Ich konnt soweit herausfinden, dass es irgendwas damit zu tun hat, das 2 Threads(?!) gleichzeitig auf die Bild-Resource zugreifen (bitte steinigt mich nicht, falls es nicht so ist).
Ich such jetzt einfach nur eine Lösung, wie das ganze nicht mehr passiert, da ich selber nicht zurecht komme.
Hier der generelle Code des Patchers:
Code:
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using AurumGuard.Properties;
using System.Drawing;
using System.Diagnostics;
using System.Threading;
using System.Net;
using System.IO;
using System.Security.Cryptography;
using System.ComponentModel;
namespace AurumGuard
{
public partial class Patcher : Form
{
#if CON
[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool AllocConsole();
#endif //CON
Image[] pgB1Image = new Image[23];
Image[] pgB2Image = new Image[23];
double downloads = 0,
cdownload = 0;
const string pListURL = "http://127.0.0.1/Patches/patch.txt",
PatchFile = "patch.txt",
VER = "VER: 2.0.0",
nPatcher = "http://127.0.0.1/blubb", // New Patcher Url
mURL = "http://127.0.0.1/Launcher.php", // Maintenance Site Url
News = "http://127.0.0.1/Launcher.php", // News Box Url
MAURL = "http://127.0.0.1/malive.html", // Maintenace-alive Url (if this files exists, it'll show the Maintenance Site
pURL = "http://127.0.0.1/Patches/", // Patch Url
Game = "game.exe", // Game
gArgument = "start"; // Argument for start the Game.
bool dCompleted = false;
StreamReader scanner;
WebClient wClient;
string cFile;
public Patcher()
{
//Directory.SetCurrentDirectory("Game"); // Use this if Neuz + Folders are placed in a subfolder so the users only starts the Patcher. (Subfolder = Game)
InitializeComponent();
#if CON
{
AllocConsole();
Console.SetWindowSize(50, 10);
Console.Title = "Aurum-Guard";
Console.CursorVisible = false;
Console.ForegroundColor = ConsoleColor.DarkRed;
Console.WriteLine("Aurum-Guard initialized");
}
#endif //CON
}
protected override void WndProc(ref Message m)
{
if (m.Msg == 0x84)
m.Result = (IntPtr)0x02;
else
base.WndProc(ref m);
}
private void btnExit_Click(object sender, EventArgs e)
{
Exit();
}
private void btnHome_Click(object sender, EventArgs e)
{
#if CON
Console.WriteLine("Aurum-Flyff Homepage opened");
#endif //CON
Process.Start(News);
}
private void btnMin_Click(object sender, EventArgs e)
{
WindowState = FormWindowState.Minimized;
}
private void btnSettings_Click(object sender, EventArgs e)
{
Form settings = new Settings();
settings.Show();
}
private void btnStart_Click(object sender, EventArgs e)
{
#if CON
Console.WriteLine("Game");
#endif //CON
StartGame();
Environment.Exit(0);
}
private void StartGame()
{
#if CON
Console.WriteLine("Game Initialising.");
#endif //CON
Process.Start(Game, gArgument);
}
private void Patcher_Load(object sender, EventArgs e)
{
HttpWebRequest HttpReq = (HttpWebRequest)WebRequest.Create(MAURL);
HttpWebResponse response;
try
{
response = (HttpWebResponse)HttpReq.GetResponse();
nBox.Navigate(mURL);
}
catch
{
HttpReq = (HttpWebRequest)WebRequest.Create(News);
try
{
response = (HttpWebResponse)HttpReq.GetResponse();
HttpReq = (HttpWebRequest)WebRequest.Create(pListURL);
response = (HttpWebResponse)HttpReq.GetResponse();
nBox.Navigate(News);
if (GetPatchList())
{
#if CON
Log("WaitInitialize_START");
#endif //CON
WaitInitialize.Start();
}
else
nBox.Navigate(mURL);
}
catch
{
nBox.Navigate(mURL);
}
}
#if CON
{
Thread ConsoleReader = new Thread(cRead);
ConsoleReader.Start();
}
#endif //CON
}
#if CON
private void cRead()
{
while (true)
{
Console.WriteLine("Waiting for Commands..{0}", Environment.NewLine);
string Command = Console.ReadLine().ToUpper();
Console.Clear();
if (Command == "CLOSE" || Command == "EXIT")
Exit();
else if (Command == "HOME" || Command == "HOMEPAGE")
btnHome.Invoke(new Action(() => btnHome.PerformClick()));
else if (Command == "START" || Command == "NEUZ" || Command == "GAME" || Command == "AURUM-GAME")
{
if (btnStart.Enabled)
StartGame();
else
Console.WriteLine("Game is still patching..");
}
else if (Command == "HELP" || Command == "COMMANDS")
Console.WriteLine("Commands{0}Exit{0}Homepage{0}Game{0}Upgradebar(UPGRADEBAR PGB1/PGB2 PERCENTAGE){0}Log (Log WriteLog){0}", Environment.NewLine);
else if (Command.Contains("UPGRADEBAR"))
{
if (Command.Contains("PGB1"))
{
Console.WriteLine("Upgradebar(" + Command.Replace("UPGRADEBAR", string.Empty).Replace(" ", string.Empty).Replace("PGB1", string.Empty) + ", true)");
try
{
Upgradebar(Convert.ToByte(Command.Replace("UPGRADEBAR", string.Empty).Replace(" ", string.Empty).Replace("PGB1", string.Empty)));
}
catch
{
Console.WriteLine("Wrong Parameter in Upgradebar( {0} ", Command);
}
}
else if (Command.Contains("PGB2"))
{
Console.WriteLine("Upgradebar(" + Command.Replace("UPGRADEBAR", string.Empty).Replace(" ", string.Empty).Replace("PGB2", string.Empty) + ", false)");
try
{
Upgradebar(Convert.ToByte(Command.Replace("UPGRADEBAR", string.Empty).Replace(" ", string.Empty).Replace("PGB2", string.Empty)), false);
}
catch
{
Console.WriteLine("Wrong Parameter in Upgradebar( {0} )", Command);
}
}
else
Console.WriteLine("Unknown PGB");
}
else if (Command.Contains("GETPATCHLIST"))
{
Console.WriteLine("GetPatchList()");
}
else if (Command.Contains("LOG"))
Log(Command.Replace("LOG", String.Empty));
else
Console.WriteLine("Unknown Command: {0}", Command);
}
}
#endif //CON
private void Upgradebar(double percentage, bool pgb1 = true)
{
try
{
if (percentage < 5)
{
if (pgb1)
pgBsFile.Image = Resources.pg_000;
else
pgBfState.Image = Resources.pg2_000;
}
else if (percentage < 10)
{
if (pgb1)
pgBsFile.Image = Resources.pg_005;
else
pgBfState.Image = Resources.pg2_005;
}
else if (percentage < 15)
{
if (pgb1)
pgBsFile.Image = Resources.pg_010;
else
pgBfState.Image = Resources.pg2_010;
}
else if (percentage < 20)
{
if (pgb1)
pgBsFile.Image = Resources.pg_015;
else
pgBfState.Image = Resources.pg2_015;
}
else if (percentage < 25)
{
if (pgb1)
pgBsFile.Image = Resources.pg_020;
else
pgBfState.Image = Resources.pg2_020;
}
else if (percentage < 30)
{
if (pgb1)
pgBsFile.Image = Resources.pg_025;
else
pgBfState.Image = Resources.pg2_025;
}
else if (percentage < 35)
{
if (pgb1)
pgBsFile.Image = Resources.pg_030;
else
pgBfState.Image = Resources.pg2_030;
}
else if (percentage < 40)
{
if (pgb1)
pgBsFile.Image = Resources.pg_035;
else
pgBfState.Image = Resources.pg2_035;
}
else if (percentage < 45)
{
if (pgb1)
pgBsFile.Image = Resources.pg_040;
else
pgBfState.Image = Resources.pg2_040;
}
else if (percentage < 50)
{
if (pgb1)
pgBsFile.Image = Resources.pg_045;
else
pgBfState.Image = Resources.pg2_045;
}
else if (percentage < 55)
{
if (pgb1)
pgBsFile.Image = Resources.pg_050;
else
pgBfState.Image = Resources.pg2_050;
}
else if (percentage < 60)
{
if (pgb1)
pgBsFile.Image = Resources.pg_055;
else
pgBfState.Image = Resources.pg2_055;
}
else if (percentage < 65)
{
if (pgb1)
pgBsFile.Image = Resources.pg_060;
else
pgBfState.Image = Resources.pg2_060;
}
else if (percentage < 70)
{
if (pgb1)
pgBsFile.Image = Resources.pg_065;
else
pgBfState.Image = Resources.pg2_065;
}
else if (percentage < 75)
{
if (pgb1)
pgBsFile.Image = Resources.pg_070;
else
pgBfState.Image = Resources.pg2_070;
}
else if (percentage < 80)
{
if (pgb1)
pgBsFile.Image = Resources.pg_075;
else
pgBfState.Image = Resources.pg2_075;
}
else if (percentage < 85)
{
if (pgb1)
pgBsFile.Image = Resources.pg_080;
else
pgBfState.Image = Resources.pg2_080;
}
else if (percentage < 90)
{
if (pgb1)
pgBsFile.Image = Resources.pg_085;
else
pgBfState.Image = Resources.pg2_085;
}
else if (percentage < 95)
{
if (pgb1)
pgBsFile.Image = Resources.pg_090;
else
pgBfState.Image = Resources.pg2_090;
}
else if (percentage < 100)
{
if (pgb1)
pgBsFile.Image = Resources.pg_095;
else
pgBfState.Image = Resources.pg2_095;
}
else if (percentage < 105)
{
if (pgb1)
pgBsFile.Image = Resources.pg_100;
else
pgBfState.Image = Resources.pg2_100;
}
else if (percentage < 110)
{
if (pgb1)
pgBsFile.Image = Resources.pg_105;
else
pgBfState.Image = Resources.pg2_105;
}
else if (percentage >= 110)
{
if (pgb1)
pgBsFile.Image = Resources.pg_110;
else
pgBfState.Image = Resources.pg2_110;
}
//else
// throw new ArgumentException();
}
catch { }
}
private bool GetPatchList()
{
if (File.Exists(PatchFile))
File.Delete(PatchFile);
WebClient wClient = new WebClient();
try
{
wClient.DownloadFile(pListURL, PatchFile);
return true;
}
#if CON
catch (Exception ex)
{
Log(ex.Message);
return false;
}
#else //CON
catch { return false; }
#endif //CON
}
#if CON
private void Log(string log)
{
Console.WriteLine(log);
}
#endif //CON
private void WaitInitialize_Tick(object sender, EventArgs e)
{
#if CON
Log("WaitInitialize_END");
#endif //CON
WaitInitialize.Stop();
sPatch();
}
private void sPatch()
{
if (checkVersion())
{
Thread patch = new Thread(download);
patch.Start();
}
}
private bool checkVersion()
{
if (File.Exists(PatchFile))
{
scanner = new StreamReader(PatchFile);
if (scanner.ReadLine() != VER)
{
scanner.Close();
#if CON
Log("VER != ACTUALVER");
#endif //CON
Process.Start(nPatcher);
Exit();
return false;
}
else
{
#if CON
Log(VER);
#endif //CON
scanner.Close();
return true;
}
}
else
{
#if CON
Log("checkVersion - " + PatchFile + " not found");
#endif //CON
return false;
}
}
private void Exit()
{
Environment.Exit(0);
}
private void download()
{
if (File.Exists(PatchFile))
{
scanner = new StreamReader(PatchFile);
scanner.ReadLine();
downloads = File.ReadAllLines(PatchFile).Length - 2;
string md5;
Upgradebar(0, true);
Upgradebar(0, false);
while (!scanner.EndOfStream)
{
dCompleted = false;
cdownload++;
double percentage = cdownload / downloads * 110;
Upgradebar(percentage, false);
cFile = scanner.ReadLine();
#if CON
Log("Patching: " + cFile);
#endif //CON
md5 = cFile.Replace(cFile.Substring(0, cFile.IndexOf("|")+1), "");
cFile = cFile.Substring(0, cFile.IndexOf("|"));
Thread pat = new Thread(Patch);
if (File.Exists(cFile.Replace(".gz", string.Empty)))
{
if (GetMD5Hash(cFile.Replace(".gz", string.Empty)) != md5)
{
#if CON
Log("Patch.Start(" + cFile + ")");
#endif //CON
pat.Start();
}
else
{
Upgradebar(110);
dCompleted = true;
}
}
else
pat.Start();
while (!dCompleted)
Thread.Sleep(250);
}
Upgradebar(110, false);
scanner.Close();
File.Delete(PatchFile);
btnStart.Invoke(new Action(() => btnStart.Enabled = true));
#if CON
Log("Patch successful");
#endif //CON
}
else
{
#if CON
Log("download - " + PatchFile + " not found");
#endif //CON
Exit();
}
}
private static string GetMD5Hash(string path)
{
FileStream FileCheck = File.OpenRead(path);
MD5 md5 = new MD5CryptoServiceProvider();
byte[] md5Hash = md5.ComputeHash(FileCheck);
FileCheck.Close();
return BitConverter.ToString(md5Hash).Replace("-", string.Empty).ToLower();
}
private void Patch()
{
if (cFile.Contains(@"\"))
{
string Folder = cFile.Substring(0, cFile.IndexOf(@"\"));
if (!Directory.Exists(Folder))
Directory.CreateDirectory(Folder);
string Folder2 = cFile.Replace(Folder + @"\", string.Empty);
if (Folder2.Contains(@"\"))
{
Folder2 = Folder2.Substring(0, Folder2.IndexOf(@"\"));
if (!Directory.Exists(Folder + @"\" + Folder2))
Directory.CreateDirectory(Folder + @"\" + Folder2);
if (Folder2.Contains(@"\"))
{
string Folder3 = Folder2.Substring(0, Folder2.IndexOf(@"\"));
if (!Directory.Exists(Folder + @"\" + Folder2 + @"\" + Folder3))
Directory.CreateDirectory(Folder + @"\" + Folder2 + @"\" + Folder3);
}
}
}
Download(pURL + cFile.Replace("\\", "/"), cFile);
}
private void Completed(object sender, AsyncCompletedEventArgs e)
{
Upgradebar(110);
GZip.DecompressFile(cFile, cFile.Replace(".gz", ""));
File.Delete(cFile);
#if CON
Log("Patched " + cFile);
#endif //CON
dCompleted = true;
}
private void ProgressChanged(object sender, ProgressChangedEventArgs e)
{
Upgradebar(e.ProgressPercentage * 1.1f);
}
private void Download(string urlAddress, string location)
{
using (wClient = new WebClient())
{
wClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
wClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);
Uri URL = new Uri(urlAddress);
try
{
wClient.DownloadFileAsync(URL, location);
}
#if CON
catch (Exception ex)
{
Log(ex.Message);
}
#else //CON
catch { }
#endif //CON
}
}
}
}
(Epvp-Thread) auch der Link zum kompletten Patcher.Vielen Dank schon mal, wenn mir irgendjemand helfen kann






