program stops responding when in a busy loop

Hi,

I have a program which inside a loop does some encryption/decryption till the battery level on my laptop goes down by some percentage. However, the problem is that it simply stops responding some time in between. I had a similar code for PocketPc which was fine. I believe it has something to do with the fact that my code is busy waiting(not actually waiting but doing computations in a loop) and would not respond to other events. I say so coz I got an error during one of the previous runs saying something similar. Please help. My code is as below:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.Security.Cryptography;

using System.IO;

using System.Runtime.InteropServices;

namespace WindowsApplication1

{

public partial class Form1 : Form

{

String plaintext = //some plaintext
String enctext = "";
String dectext = "";

SymmetricAlgorithm mCryptoService;

public Form1()

{

InitializeComponent();

mCryptoService = new DESCryptoServiceProvider();

mCryptoService.GenerateIV();

mCryptoService.GenerateKey();

}

private void start_Click(object sender, EventArgs e)

{

DateTime startTime, stopTime;

TimeSpan duration;

float decrease = 0.25F, initial = 0, start, end;

long counter = 0;

initial = SystemInformation.PowerStatus.BatteryLifePercent;

percent.Text = String.Format("{0}%", initial);

//wait till u go 1 percent down

status.Text = "computations will start...";

while (true)

{

if (SystemInformation.PowerStatus.BatteryLifePercent != initial)

break;

}

start = SystemInformation.PowerStatus.BatteryLifePercent;

percent.Text = String.Format("{0}%", start);

end = start - decrease;

status.Text = "computations started";

/* Read the initial time. */

startTime = DateTime.Now;

while (SystemInformation.PowerStatus.BatteryLifePercent != end)

{

enctext = "";

dectext = "";

if (Encrypt() != 0)

status.Text = "encryption error";

if (Decrypt() != 0)

status.Text = "decryption error";

enctext = "";

dectext = "";

if (Encrypt() != 0)

status.Text = "encryption error";

if (Decrypt() != 0)

status.Text = "decryption error";

counter = counter + 2;

count.Text = counter.ToString();

percent.Text = String.Format("{0}%", SystemInformation.PowerStatus.BatteryLifePercent);

Refresh();

}

status.Text = "Done";

stopTime = DateTime.Now;

duration = stopTime - startTime;

time.Text = duration.ToString();

}


private int Encrypt()

{
//some encryption
}

private int Decrypt()

{

//some decryption
}

}

}




Answer this question

program stops responding when in a busy loop

  • bdavidh

    Have a look at the BackgroundWorker component, it makes running multiple threads easy.

  • howle

    of course the other event and code wil be not triggered coz the you are still in the loop.. when the loop finishes then other code will be execute

    Try make that loop in another thread.. that should work and keep your UI responsive too


  • program stops responding when in a busy loop