using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.IO; using System.Threading; namespace Leikur_FK09 { public partial class Form1 : Form { public Form1() { string ResourcePath = Application.ExecutablePath.Remove(Application.ExecutablePath.LastIndexOf("bin")) + "Resources\\"; InitializeComponent(); if (File.Exists(ResourcePath + "Maps\\Temp.mp")) File.Delete(ResourcePath + "Maps\\Temp.mp"); } private void button1_Click(object sender, EventArgs e) { string s = Application.ExecutablePath; s = s.Remove(s.LastIndexOf('\\')); s = s.Remove(s.LastIndexOf('\\')); s = s.Remove(s.LastIndexOf('\\')); s += "\\Resources\\"; int Size = 0; StreamWriter sw = new StreamWriter(s + "Difficulty", false); foreach (Control c in this.Controls) { if (c.Name.Substring(0, 5) == "Radio") { RadioButton r = (RadioButton)c; if (r.Checked) { sw.Write(c.Name.Substring(c.Name.Length - 1)); sw.Flush(); sw.Close(); } } else if (c.Name.Substring(0, 5) == "radio") { RadioButton r = (RadioButton)c; if (r.Checked) Size = Convert.ToInt32(r.Name.Substring(r.Name.Length - 1, 1)) - 1; } } sw = new StreamWriter(s + "Size", false); sw.Write((Size * 5 + 10).ToString()); sw.Flush(); sw.Close(); ThreadStart job = new ThreadStart(GameThread); Thread gamethread = new Thread(job); gamethread.Start(); Thread.Sleep(1); Application.ExitThread(); //Application.Run(new Game()); } private void DismissException(Exception e) { //Do Nothing } static void GameThread() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Thread.Sleep(10); Thread.CurrentThread.SetApartmentState(ApartmentState.STA); Application.Run(new Game()); } private void radioButton3_CheckedChanged(object sender, EventArgs e) { radioButton1.Checked = false; radioButton2.Checked = false; radioButton3.Checked = true; } private void radioButton2_CheckedChanged(object sender, EventArgs e) { radioButton1.Checked = false; radioButton2.Checked = true; radioButton3.Checked = false; } private void radioButton1_CheckedChanged(object sender, EventArgs e) { radioButton1.Checked = true; radioButton2.Checked = false; radioButton3.Checked = false; } private void Radio2_CheckedChanged(object sender, EventArgs e) { Radio0.Checked = false; Radio1.Checked = false; Radio2.Checked = true; } private void Radio1_CheckedChanged(object sender, EventArgs e) { Radio0.Checked = false; Radio1.Checked = true; Radio2.Checked = false; } private void Radio0_CheckedChanged(object sender, EventArgs e) { Radio0.Checked = true; Radio1.Checked = false; Radio2.Checked = false; } } }
Þetta er bútur úr kóða sem, eins og sjá má lokar eigin þræði en opnar annan.
Þessi nýi þráður opnast alveg og allt í góðu, þangað til ég ætla að opna FileOpenDialog, en þá frýs forritið, ekkert message eða neitt. Þegar ég debugga kemur hins vegar up ThreadStateException
Current thread must be set to single thread apartment (STA) mode before OLE calls can be made. Ensure that your Main function has STAThreadAttribute marked on it. This exception is only raised if a debugger is attached to the process.
Hugmyndir um hvernig væri hægt að laga þetta væru vel þegnar.
The cake is a Pie