Programma hangt bij starten

Status
Niet open voor verdere reacties.

hitme007

Gebruiker
Lid geworden
16 jun 2004
Berichten
131
Ik heb de volgende code, maar zodra ik em run hangt ie meteen.
Processor wordt 100% belast, maar dat is ook de bedoeling... hij is priemgetallen aan het uitreken.
Maar hij moet elke ... milliseconden het laatste berekende priemgetal weergeven en dat doet ie niet :(

help... please :rolleyes:

PHP:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace PrimeFinder
{
	public class MainForm : System.Windows.Forms.Form
	{
		private System.Windows.Forms.RichTextBox textField1;
		private System.Windows.Forms.Button buttonCopy;
		private System.Windows.Forms.Button buttonStart;
		private System.Windows.Forms.Button buttonClear;
		private System.Windows.Forms.Button buttonAfsluiten;
		private System.Windows.Forms.Button buttonStop;
		private System.Windows.Forms.PictureBox pictureBox1;
		private System.Windows.Forms.Timer timer1;
		private System.ComponentModel.IContainer components;
		private System.Windows.Forms.Label label1;
		private long getal,priem;
		private int digits;
		bool stop;

		public MainForm()
		{
			InitializeComponent();
		}

		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code

		private void InitializeComponent()
		{
			this.components = new System.ComponentModel.Container();
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(MainForm));
			this.textField1 = new System.Windows.Forms.RichTextBox();
			this.buttonCopy = new System.Windows.Forms.Button();
			this.buttonStart = new System.Windows.Forms.Button();
			this.buttonClear = new System.Windows.Forms.Button();
			this.buttonAfsluiten = new System.Windows.Forms.Button();
			this.buttonStop = new System.Windows.Forms.Button();
			this.pictureBox1 = new System.Windows.Forms.PictureBox();
			this.timer1 = new System.Windows.Forms.Timer(this.components);
			this.label1 = new System.Windows.Forms.Label();
			this.SuspendLayout();
			// 
			// textField1
			// 
			this.textField1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.textField1.Cursor = System.Windows.Forms.Cursors.Default;
			this.textField1.ForeColor = System.Drawing.SystemColors.WindowText;
			this.textField1.ImeMode = System.Windows.Forms.ImeMode.NoControl;
			this.textField1.Location = new System.Drawing.Point(170, 5);
			this.textField1.Name = "textField1";
			this.textField1.ReadOnly = true;
			this.textField1.ScrollBars = System.Windows.Forms.RichTextBoxScrollBars.ForcedVertical;
			this.textField1.Size = new System.Drawing.Size(310, 255);
			this.textField1.TabIndex = 0;
			this.textField1.Text = "";
			// 
			// buttonCopy
			// 
			this.buttonCopy.BackColor = System.Drawing.SystemColors.InactiveBorder;
			this.buttonCopy.Location = new System.Drawing.Point(5, 165);
			this.buttonCopy.Name = "buttonCopy";
			this.buttonCopy.Size = new System.Drawing.Size(160, 25);
			this.buttonCopy.TabIndex = 2;
			this.buttonCopy.Text = "Kopieren";
			this.buttonCopy.Click += new System.EventHandler(this.buttonCopy_Click);
			// 
			// buttonStart
			// 
			this.buttonStart.BackColor = System.Drawing.SystemColors.InactiveBorder;
			this.buttonStart.Location = new System.Drawing.Point(5, 95);
			this.buttonStart.Name = "buttonStart";
			this.buttonStart.Size = new System.Drawing.Size(160, 25);
			this.buttonStart.TabIndex = 4;
			this.buttonStart.Text = "Start";
			this.buttonStart.Click += new System.EventHandler(this.buttonStart_Click);
			// 
			// buttonClear
			// 
			this.buttonClear.BackColor = System.Drawing.SystemColors.InactiveCaptionText;
			this.buttonClear.Location = new System.Drawing.Point(5, 200);
			this.buttonClear.Name = "buttonClear";
			this.buttonClear.Size = new System.Drawing.Size(160, 25);
			this.buttonClear.TabIndex = 5;
			this.buttonClear.Text = "Wissen";
			this.buttonClear.Click += new System.EventHandler(this.buttonClear_Click);
			// 
			// buttonAfsluiten
			// 
			this.buttonAfsluiten.BackColor = System.Drawing.SystemColors.InactiveBorder;
			this.buttonAfsluiten.Location = new System.Drawing.Point(5, 235);
			this.buttonAfsluiten.Name = "buttonAfsluiten";
			this.buttonAfsluiten.Size = new System.Drawing.Size(160, 25);
			this.buttonAfsluiten.TabIndex = 6;
			this.buttonAfsluiten.Text = "Afsluiten";
			this.buttonAfsluiten.Click += new System.EventHandler(this.buttonAfsluiten_Click);
			// 
			// buttonStop
			// 
			this.buttonStop.BackColor = System.Drawing.SystemColors.InactiveCaptionText;
			this.buttonStop.Location = new System.Drawing.Point(5, 130);
			this.buttonStop.Name = "buttonStop";
			this.buttonStop.Size = new System.Drawing.Size(160, 25);
			this.buttonStop.TabIndex = 7;
			this.buttonStop.Text = "Stop";
			this.buttonStop.Click += new System.EventHandler(this.buttonStop_Click);
			// 
			// pictureBox1
			// 
			this.pictureBox1.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("pictureBox1.BackgroundImage")));
			this.pictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.pictureBox1.Location = new System.Drawing.Point(5, 5);
			this.pictureBox1.Name = "pictureBox1";
			this.pictureBox1.Size = new System.Drawing.Size(160, 80);
			this.pictureBox1.TabIndex = 8;
			this.pictureBox1.TabStop = false;
			// 
			// timer1
			// 
			this.timer1.Interval = 1;
			this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
			// 
			// label1
			// 
			this.label1.BackColor = System.Drawing.SystemColors.ControlLight;
			this.label1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.label1.Location = new System.Drawing.Point(175, 235);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(283, 20);
			this.label1.TabIndex = 9;
			this.label1.Text = "Aantal Digits: 0";
			// 
			// MainForm
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.BackColor = System.Drawing.SystemColors.InactiveCaption;
			this.ClientSize = new System.Drawing.Size(484, 265);
			this.Controls.Add(this.label1);
			this.Controls.Add(this.pictureBox1);
			this.Controls.Add(this.buttonStop);
			this.Controls.Add(this.buttonAfsluiten);
			this.Controls.Add(this.buttonClear);
			this.Controls.Add(this.buttonStart);
			this.Controls.Add(this.buttonCopy);
			this.Controls.Add(this.textField1);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
			this.MaximizeBox = false;
			this.Name = "MainForm";
			this.Opacity = 0.9;
			this.Text = "Prime Finder | Jochem van Kessel | © 2005";
			this.Load += new System.EventHandler(this.MainForm_Load);
			this.ResumeLayout(false);

		}
		#endregion

		[STAThread]
		static void Main() 
		{
			Application.Run(new MainForm());
		}

		private void MainForm_Load(object sender, System.EventArgs e)
		{

		}

		private void buttonCopy_Click(object sender, System.EventArgs e)
		{
			textField1.SelectAll();
			textField1.Copy();
		}

		private void buttonClear_Click(object sender, System.EventArgs e)
		{
			textField1.Clear();
			getal = 0;
		}

		private void buttonAfsluiten_Click(object sender, System.EventArgs e)
		{
			Application.Exit();
		}

		private void buttonStart_Click(object sender, System.EventArgs e)
		{
			berekenPriem();
			timer1.Start();
		}

		private void buttonStop_Click(object sender, System.EventArgs e)
		{
			stop = true;
			timer1.Stop();
		}

		private void timer1_Tick(object sender, System.EventArgs e)
		{
			textField1.Text = ""+getPriem();
			if ( digits != textField1.TextLength ) 
			{
				digits = textField1.TextLength;
				label1.Text = "Aantal Digits: "+digits;
			}
		}

		private void berekenPriem() 
		{
			while ( !stop ) 
			{
				if ( getal % 2 != 0 ) 
				{ 
					setPriem(getal);
					getal++;
				}
				else 
				{
					getal++;
				}
			}
		}

		private long getPriem() 
		{
			return priem;
		}

		private void setPriem(long temp)
		{
			this.priem = temp;
		}
	}
}
 
Hoi

Het probleem zit hem in de "while ( !stop )" in de methode berekenPriem().

Je maakt gebruik van een timer met een interval op 1 dus dan is een while lus overbodig. De while lus zoals je deze hebt vermeld zorgt ervoor dat de applicatie verder geen events kan afhandelen. Zou je nu in de methode opgeven: Application.DoEvents(); dan loopt het programma niet vast. Maar nog beter is om de while lus eruit te halen en gewoon van de timer gebruik te maken: (gemaakt met Visual Studio 2005)

PHP:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Priem
{
    public partial class Form1 : Form
    {
        private long getal;

        public Form1()
        {
            InitializeComponent();
            timer1.Interval = 1;
            timer1.Tick += new EventHandler(timer1_Tick);
        }

        void timer1_Tick(object sender, EventArgs e)
        {
            berekenPriem();
        }

        private void berekenPriem()
        {
            if (getal % 2 != 0)
            {
                textField1.Text = getal.ToString();
                label1.Text = "Aantal Digits: " + textField1.Text.Length.ToString();
                getal++;
            }
            else
            {
                getal++;
            }
        }

        private void buttonStart_Click(object sender, EventArgs e)
        {
            timer1.Start();
        }

        private void buttonStop_Click(object sender, EventArgs e)
        {
            timer1.Stop();
        }

        private void buttonCopy_Click(object sender, EventArgs e)
        {
            textField1.SelectAll();
            textField1.Copy();
        }

        private void buttonClear_Click(object sender, EventArgs e)
        {
            textField1.Clear();
            getal = 0;
        }

        protected override void OnClosed(EventArgs e)
        {
            timer1.Stop();
        }

        private void buttonAfsluiten_Click(object sender, EventArgs e)
        {
            timer1.Stop();
            Application.Exit();
        }
    }
}
 
Status
Niet open voor verdere reacties.
Steun Ons

Nieuwste berichten

Terug
Bovenaan Onderaan