I'm building an auto clicker application that makes the mouse click at a set interval of time.
Notice the interval configuration area. I have tried to write some logic that automatically simplifies the amount of time entered. The minutes box goes up to 60, the seconds box goes up to 60, and the milliseconds box goes up to 1000. I have a class set up to handle that logic, but it's probably not the right way to do that (I'm still new at programming).
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace AutoClicker {
public static class TotalTime {
public static int Interval(NumericUpDown m_, NumericUpDown s_, NumericUpDown ms_) {
int m = (int)m_.Value;
int s = (int)s_.Value;
int ms = (int)ms_.Value;
int total = 0;
total = total + (m * 60000);
total = total + (s * 1000);
total = total + ms;
return total;
}
public static void ChangeLogic(NumericUpDown m_, NumericUpDown s_, NumericUpDown ms_) {
int interval = Interval(m_, s_, ms_);
if (ms_.Value == 1000)
{
ms_.Value = 500;
s_.UpButton();
ms_.Value = 0;
}
if (s_.Value == 60 && m_.Value < 60)
{
if (ms_.Value == 0)
{
ms_.Value = 1;
s_.Value = 0;
m_.UpButton();
ms_.Value = 0;
}
else
{
s_.Value = 0;
m_.UpButton();
if (ms_.Value == 1)
ms_.DownButton();
}
}
if (ms_.Value == -1)
{
ms_.Value = 999;
s_.DownButton();
}
if (s_.Value == -1 & m_.Value > 0)
{
s_.Value = 59;
m_.DownButton();
if (ms_.Value == 1)
ms_.DownButton();
}
}
}
}
The ChangeLogic method is called each time the value in any of the boxes is updated.
There's lots of bugs in the selection logic. For example, when the up button for seconds is held down, the program crashes. If seconds = 59 and minutes = 0, and the seconds up button is pressed, the warning "you can't have a time less than 250 milliseconds" shows up and minutes is adjusted to 2.
I'm really confused. Can anyone help me? Thanks!
So, it looks like your intent is to have it so that when you up the milliseconds it then rolls up to the seconds once it hits 1000, then when your seconds hit 60 it rolls up to your minutes, etc etc.
Here is how I would accomplish this. First set all of your NUDs to max value of 99999. Then use code that looks something like this:
(Note the use of the intervalUpdating variable. This will make it so that you can hold that up button and while the intervalChanged() method is executing it wont try to execute it again.)
private void nudSeconds_ValueChanged(object sender, EventArgs e)
{
intervalChanged();
}
private void nudMilliseconds_ValueChanged(object sender, EventArgs e)
{
intervalChanged();
}
private bool intervalUpdating = false;
private void intervalChanged()
{
if (intervalUpdating)
{
return;
}
intervalUpdating = true;
if (nudMilliseconds.Value >= 1000)
{
var val = (int)nudMilliseconds.Value / 1000;
nudSeconds.Value += val;
nudMilliseconds.Value = (nudMilliseconds.Value - (val * 1000));
}
if (nudSeconds.Value >= 60)
{
var val = (int)nudSeconds.Value / 60;
nudMinutes.Value += val;
nudSeconds.Value = (nudSeconds.Value - (val * 60));
}
intervalUpdating = false;
}
Related
I am trying to plot a line chart with real time data for every one second in my windows application. For that I need to set minimum (0 seconds) and maximum (10 minutes) values for the chart . After 10 minutes, minimum value is 10 minutes and maximum value is 20 minutes. So I have to display,10 minutes data every time. I need to display previous data with scrollbar from the very beginning.
I tried the following code but I am able to fit first two frames of the chart. That means for example 0-10 minutes and 10-20 minutes.After that I am unable to fit the min and max values(while debugging,getting correct values but chart is not updating with recent min max values).I am not understanding where I did the mistake.I am very new to the MS chart control. Please guide me.
int i1=0,i2=120,count=0;
double min,max;
private void timer1_Tick(object sender, EventArgs e)
{
chart1.Invoke(addDataDel);
count++;
}
public void AddData()
{
xvalue = getX(count);
yvalue = getY(count + 1);
foreach (Series ptSeries in chart1.Series)
{
AddNewPoint(timeStamp, ptSeries,xvalue,yvalue);
}
}
public void AddNewPoint(DateTime timeStamp, System.Windows.Forms.DataVisualization.Charting.Series ptSeries, double xvalue, double yvalue)
{
// Add new data point to its series.
ptSeries.Points.AddXY(timeStamp.ToOADate(), xvalue);
ptSeries.Points.AddXY(timeStamp.ToOADate(), yvalue);
if ((count% 600)==0)
{
double removeBefore = timeStamp.AddSeconds((double)(600) * (-1)).ToOADate();
if (ptSeries.Points[0].XValue < removeBefore)
{
i2 += 120;
i1 += 120;
chart1.ChartAreas[0].CursorX.AutoScroll = true;
chart1.ChartAreas[0].AxisX.ScaleView.Zoomable = true;
chart1.ChartAreas[0].AxisX.ScaleView.Size = 120;
min = ptSeries.Points[0].XValue;
chart1.ChartAreas[0].AxisX.Minimum = ptSeries.Points[0].XValue;
chart1.ChartAreas[0].AxisX.Maximum = DateTime.FromOADate(ptSeries.Points[0].XValue).AddMinutes(2).ToOADate();
//chart1.ChartAreas[0].AxisX.ScaleView.Zoom(chart1.ChartAreas[0].AxisX.Minimum, chart1.ChartAreas[0].AxisX.Maximum);
min = chart1.ChartAreas[0].AxisX.Minimum;
max = chart1.ChartAreas[0].AxisX.Maximum;
}
}
if (count >= 600)
{
if ((count >= i1) || (count <= i2))
{
chart1.ChartAreas[0].AxisX.LabelStyle.Enabled = true;
chart1.ChartAreas[0].AxisX.ScaleView.Zoom(min, max);
chart1.ChartAreas[0].AxisX.ScaleView.Position = max;
}
}
chart1.Update();
chart1.ChartAreas[0].RecalculateAxesScale();
}
I have this Form containing 4 labels. I want these labels to blink with a specified frequency, like 12.5, 10, 8 and 4 HZ. I used a Timer, but it won't work correctly, They blink with much lower frequency, I know it's because nested ifs in the freqMethod below. How should I solve this issue?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Timers;
using System.Threading;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
System.Timers.Timer mainTimer;
private int counter = 0;
Color lColor = Color.FromArgb(255, 192, 128);
bool buttonPressed = false;
public Form1()
{
InitializeComponent();
label1.BackColor = lColor;
label2.BackColor = lColor;
label3.BackColor = lColor;
label4.BackColor = lColor;
mainTimer = new System.Timers.Timer(1);
mainTimer.Elapsed += new ElapsedEventHandler(timerElapsed);
}
private void button2_Click(object sender, EventArgs e)
{
if (buttonPressed)
{
mainTimer.Enabled = false;
buttonPressed = !buttonPressed;
counter = 0;
}
else
{
mainTimer.Enabled = true;
buttonPressed = !buttonPressed;
counter = 0;
}
}
//Frequency Method
public void freqMethod()
{
if (counter % 80 == 0)
if (label4.backColor == lColor)
label4.backColor = Color.black;
else
label4.backColor = lColor;
if (counter % 100 == 0)
if (label3.backColor == lColor)
label3.backColor = Color.black;
else
label3.backColor = lColor;
if (counter % 125 == 0)
if (label2.backColor == lColor)
label2.backColor = Color.black;
else
label2.backColor = lColor;
if (counter % 250 == 0)
if (label1.backColor == lColor)
label1.backColor = Color.black;
else
label1.backColor = lColor;
}
private void timerElapsed(object source, ElapsedEventArgs e) {
counter++;
freqMethod();
}
}
}
Given the following values (and if you want to synchronized them using one timer) the common interval they can have is 5ms. So you need to tick the timer 5ms each and check the frequency. But take note on using the timer as explained below:
12.5hz = 80ms
10hz = 100ms
8hz = 125ms
4hz = 250ms
Remarks from MSDN using System.Timers.Timer (https://msdn.microsoft.com/en-us/library/system.timers.timer.interval(v=vs.110).aspx) See remarks section.
You use the Interval property to determine the frequency at which the
Elapsed event is fired. Because the Timer class depends on the system
clock, it has the same resolution as the system clock. This means that
the Elapsed event will fire at an interval defined by the resolution
of the system clock if the Interval property is less than the
resolution of the system clock. The following example sets the
Interval property to 5 milliseconds. When run on a Windows 7 system
whose system clock has a resolution of approximately 15 milliseconds,
the event fires approximately every 15 milliseconds rather than every
5 milliseconds
But if you can use multiple timer for each, then you can set each interval of the timer as Yeldar mentioned.
You don't need timer to iterate every second as you skip it's every n iterations while they take too much resources. You can just manipulate with Timer's Interval value to get the required frequency with adeqate performance.
For example, for frequency 8 Hz you only need timer to fire an event every 125 ms (8 times per second).
I will provide an example with a double frequency in order to make it work with intervals < 1. For example, if you set frequency to 0.5, the color will change every 2 seconds.
Example:
public Form1()
{
double frequencyInHz = 8.0; // here goes your frequency
int interval = (int)Math.Round(1000.0 / frequencyInHz); // 125ms in this case
mainTimer = new Timer(interval);
mainTimer.Elapsed += new ElapsedEventHandler(timerElapsed);
}
private void timerElapsed(object source, ElapsedEventArgs e) {
if (label2.BackColor == lColor)
label2.BackColor = Color.Black;
else
label2.BackColor = lColor;
}
If you need several labels to change their colors with different, you will need to make several timers in order to get a good performance.
Try this (in addition to the timer interval modification proposed by Joel or Yeldar) :
if (counter % 80 == 0)
{
label4.backColor = label4.backColor == lColor ? Color.black : lColor;
label4.Refresh() ;
}
I am using C# and the WebClient class.
This is the code I am using, inspired by another post here on SO. This code worked well for large files, it accurately displayed the download speed. However, there is now the limitation of downloading individual files, many of which are small, with small being .5-5 MB. This has caused the speed counter to skyrocket, often into the hundreds of thousands of KBps. I'm not sure what else to try to combat this. I added a second progress bar showing individual file downloads which helps improve the image a bit, but the download speed counter should really be fixed. Is there a different class to use that would solve this problem?
The WebClient in this code is disposed of properly elsewhere.
private class NetSpeedCounter
{
private double[] DataPoints;
private DateTime LastUpdate;
private int NumCounts = 0;
private int PrevBytes = 0;
public double Speed { get; private set; }
public NetSpeedCounter(WebClient webClient, int maxPoints = 10)
{
DataPoints = new double[maxPoints];
Array.Clear(DataPoints, 0, DataPoints.Length);
webClient.DownloadProgressChanged += (sender, e) =>
{
var msElapsed = DateTime.Now - LastUpdate;
int curBytes = (int)(e.BytesReceived - PrevBytes);
PrevBytes = (int)e.BytesReceived;
double dataPoint = ((double)curBytes) / msElapsed.TotalSeconds;
DataPoints[NumCounts++ % maxPoints] = dataPoint;
Speed = DataPoints.Average();
};
}
public void Reset()
{
PrevBytes = 0;
LastUpdate = DateTime.Now;
}
}
I download the files with this code, which is started afterwards by a call to DownloadFileAsync. This code just downloads them in a chain, one after another, asynchronously.
This is setting up for starting the download
Queue recordQ = new Queue(files);
progressBar.Value = 0;
progressBar.Maximum = recordQ.Count;
UpdateStatusText("Downloading " + recordQ.Count + " files");
var record = recordQ.Dequeue();
speedUpdater.Start();
CheckAndCreate(record.AbsolutePath);
Adding the event handler
wc.DownloadFileCompleted += (sender, e) =>
{
var nr = recordQ.Dequeue();
CheckAndCreate(nr.AbsolutePath);
this.Invoke((MethodInvoker)delegate
{
UpdateStatusText("Downloading " + recordQ.Count + " files", lblStatusR.Text);
});
counter.Reset();
// download the next one
wc.DownloadFileAsync(nr.DownloadPath, nr.AbsolutePath);
}
counter.Start();
wc.DownloadFileAsync(record.DownloadPath, record.AbsolutePath);
This last call is what starts everything off.
DateTime.Now is not accurate enough for some scenarios where timespans are recorded frequently(Eric Lippert mentions that they have a precision of 30 ms here), since DateTime.Now will return the previously used DateTime.Now when called quickly in succession. This might result in discrepencies in your speed counter due to inaccurate increases when downloads are finished very quickly. I'd recommend using StopWatch API for that purpose.
EDIT
I have created the following test Winforms application based on your code that works fine for small files. I'm getting a reasonable 200 kbps over my intranet for 5 files that are about 2MB each. Just make sure you're calling the stopwatch classes at the right places.
To replicate, create a winforms app, create 3 labels of Id lblSpeed, lblStatus, lblFile and copy\paste the code and rename the URI's below to the files you want to test on.
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
Queue<Record> recordQ;
WebClient wc;
NetSpeedCounter counter;
//We store downloaded files in C:\TestDir (hardcoded in the Record class below)
public Form1()
{
InitializeComponent();
recordQ = new Queue<Record>();
//replace the URI string below. Nothing else to replace.
//recordQ.Enqueue(new Record(#"URI1", "SQLtraining.exe"));
//recordQ.Enqueue(new Record(#"URI2", "Project Mgmt.pptx"));
//first uri to process. Second param is the file name that we store.
Record record = new Record(#"URI0","Agile.pptx"); // replace the URI
//Initialize a webclient and download the first record
using (wc = new WebClient())
{
counter = new NetSpeedCounter(wc);
wc.DownloadFileCompleted += (sender, e) =>
{
if (recordQ.Count == 0)
{
UpdateStatusText("Done");
return;
}
var nr = recordQ.Dequeue();
//just create directory. the code uses the same directory
CheckAndCreate(nr.Directory);
//need not even use invoke here. Just a plain method call will suffice.
this.Invoke((MethodInvoker)delegate
{
UpdateStatusText("Left to process: " + recordQ.Count + " files");
});
counter.Reset();
counter.Start();
//continue with rest of records
wc.DownloadFileAsync(nr.DownloadPath, nr.GetFullPath());
this.lblFile.Text = nr.DownloadPath.OriginalString;
};
//just update speed in UI
wc.DownloadProgressChanged += wc_DownloadProgressChanged;
counter.Start();
//display URI we are downloading
this.lblFile.Text = record.DownloadPath.OriginalString;
//start first download
wc.DownloadFileAsync(record.DownloadPath, record.GetFullPath());
}
}
void wc_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
this.lblSpeed.Text = counter.Speed.ToString();
}
public void UpdateStatusText(string msg)
{
this.lblStatus.Text = msg;
}
public void CheckAndCreate(string absPath)
{
if (!Directory.Exists(absPath))
Directory.CreateDirectory(absPath);
}
}
public class NetSpeedCounter
{
private int NumCounts = 0;
private int PrevBytes = 0;
private Stopwatch stopwatch;
public double Speed { get; private set; }
double[] DataPoints;
public NetSpeedCounter(WebClient webClient, int maxPoints = 10)
{
DataPoints = new double[maxPoints];
stopwatch = new Stopwatch();
Array.Clear(DataPoints, 0, DataPoints.Length);
webClient.DownloadProgressChanged += (sender, e) =>
{
var msElapsed = DateTime.Now - LastUpdate;
stopwatch.Stop();
int curBytes = (int)(e.BytesReceived - PrevBytes);
PrevBytes = (int)e.BytesReceived;
//record in kbps
double dataPoint = (double)curBytes / (stopwatch.ElapsedMilliseconds);
DataPoints[NumCounts++ % maxPoints] = dataPoint;
//protect NumCount from overflow
if (NumCounts == Int32.MaxValue)
NumCounts = 0;
Speed = DataPoints.Average();
stopwatch.Start();
};
}
public void Start()
{
stopwatch.Start();
}
public void Reset()
{
PrevBytes = 0;
stopwatch.Reset();
}
}
public class Record
{
public string Directory;
public string File;
public Uri DownloadPath;
public Record(string uriPath, string fileOutputName)
{
this.Directory = #"C:\TestDir\";
this.DownloadPath = new Uri(uriPath);
this.File = fileOutputName;
}
public string GetFullPath()
{
return this.Directory + this.File;
}
}
}
I have come up with a different way of doing it.
Instead of adding a data point for each ProgressChanged event, I now increment a counter with the amount of bytes. This counter stays across multiple files, and then I simply take a datapoint using a timer, and get the time in between using a System.Diagnostic.Stopwatch
It works very good, the accuracy is much much better, I would highly recommend doing it this way.
Here is some code
class NetSpeedCounter
{
private Stopwatch watch;
private long NumCounts = 0;
private int PrevBytes = 0;
private double[] DataPoints;
private long CurrentBytesReceived = 0;
public double Speed { get; private set; }
private System.Timers.Timer ticker = new System.Timers.Timer(100);
public NetSpeedCounter(WebClient webClient, int maxPoints = 5)
{
watch = new System.Diagnostics.Stopwatch();
DataPoints = new double[maxPoints];
webClient.DownloadProgressChanged += (sender, e) =>
{
int curBytes = (int)(e.BytesReceived - PrevBytes);
if (curBytes < 0)
curBytes = (int)e.BytesReceived;
CurrentBytesReceived += curBytes;
PrevBytes = (int)e.BytesReceived;
};
ticker.Elapsed += (sender, e) =>
{
double dataPoint = (double)CurrentBytesReceived / watch.ElapsedMilliseconds;
DataPoints[NumCounts++ % maxPoints] = dataPoint;
Speed = DataPoints.Average();
CurrentBytesReceived = 0;
watch.Restart();
};
}
public void Stop()
{
watch.Stop();
ticker.Stop();
}
public void Start()
{
watch.Start();
ticker.Start();
}
public void Reset()
{
CurrentBytesReceived = 0;
PrevBytes = 0;
watch.Restart();
ticker.Start();
}
}
public PbsWheel(AnimatedPictureBox.AnimatedPictureBoxs[] pbs, AnimatedPictureBox.AnimatedPictureBoxs pb, int delta,Label label2)
{
for (int i = 0; i < pbs.Length; i++)
{
if (delta > 0)
{
pbs[i].AnimateRate += 1/60 * 1000;
1/60 * 1000 is 60 frames per second ?
This is how i animate the pictureBoxes the images inside. Im using timer for each picturebox:
public class AnimatedPictureBoxs : PictureBox
{
public static bool images;
List<string> imageFilenames;
Timer t = new Timer();
public AnimatedPictureBoxs()
{
images = false;
AnimateRate = 100; //It's up to you, the smaller, the faster.
t.Tick += Tick_Animate;
}
public int AnimateRate
{
get { return t.Interval; }
set { t.Interval = value; }
}
public void Animate(List<string> imageFilenames)
{
this.imageFilenames = imageFilenames;
t.Start();
}
public void StopAnimate()
{
t.Stop();
i = 0;
}
int i;
private void Tick_Animate(object sender, EventArgs e)
{
if (images == true)
{
imageFilenames = null;
}
if (imageFilenames == null)
{
return;
}
else
{
try
{
if (i >= imageFilenames.Count)
{
i = 0;
}
else
{
Load(imageFilenames[i]);
i = (i + 1) % imageFilenames.Count;
}
}
catch (Exception err)
{
}
}
}
The rate is set to 100 what i want to do is to display and when i move the mouse wheel up down to change the speed of the images animate by frames per second.
pbs is array of pictureBoxes.
pbs[i].AnimateRate += 1/60 * 1000;
Now, AnimateRate is an integer property. It is very badly named. It is not a rate. It is a timer interval. In mathematical terms it is a period. Naming it rate makes it sound as though it will be a rate, or a frequency.
The mathematical relationship between period T and frequency f is:
T = 1/f
So, here's what you should do:
Rename the property as AnimationInterval.
When you need to convert a frequency (i.e. frame rate) to an interval use the formula above.
Note that you need to account for the fact that your frequencies are measured in frames per second, but your intervals are measured in milli-seconds. So your code should be:
pbs[i].AnimationInterval += 1000/60;
That looks very similar to what you had but there is a subtle difference. In mathematics, the formulae are identical. But in C#, the behaviour of the / operator depends on the types of its operands. You supply two integers and so / is integer division. And the result of 1/60 is zero. So your code does not modify the property.
I do think that you will need to modify your logic a little. As it stands, your raw data is an interval. But actually what you wish to control if frame rate. So I believe that you should maintain a variable that holds the frame rate. If you want to modify it, then make the modifications to the frame rate variable. And then set the interval like this:
pbs[i].AnimationInterval = 1000/frameRate;
I wrote this for a meteorology class. For some reason, the textboxes aren't updating on the GUI correctly for large numbers (large numbers of photons). The calculation completes, but the textboxes don't update.
I suspect the problem is with calling Invoke(), but I can't for the life of me see what's going wrong. I've tried using both Invoke() and BeginInvoke() with similar results.
Can anyone help figure out where I'm going wrong?
Thanks!
PS> Please forgive the global variables. Was planning on cleaning them up later...
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace CloudTransmittance
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void buttonCalculate_Click(object sender, EventArgs e)
{
Thread t = new Thread(calculateModel);
t.Start();
}
//number of photons that have gone to albedo, direct, or diffuse transmittance
private ulong top = 0;
private ulong direct = 0;
private ulong diffuse = 0;
private ulong absorbed = 0;
private ulong failed = 0;
private ulong photons = 0;
private void calculateModel()
{
//model variables
double theta = 0;
double tauStar = 0;
double omega = 0;
double g = 0;
photons = 0;
//Get data from form
theta = Convert.ToDouble(textBoxTheta.Text);
tauStar = Convert.ToDouble(textBoxTau.Text);
omega = Convert.ToDouble(textBoxOmega.Text);
g = Convert.ToDouble(textBoxG.Text);
photons = Convert.ToUInt64(textBoxPhotons.Text);
//Clear the progress bar and set its limits
this.progressBar1.BeginInvoke(
(MethodInvoker)delegate()
{
this.progressBar1.Minimum = 0;
this.progressBar1.Value = 0;
this.progressBar1.Maximum = (int)photons;
this.progressBar1.Step = 1;
});
//Clear the text boxes
this.textBoxAlbedo.Invoke(
(MethodInvoker)delegate()
{
this.textBoxAlbedo.Text = "";
});
this.textBoxDirect.Invoke(
(MethodInvoker)delegate()
{
this.textBoxDirect.Text = "";
});
this.textBoxDiffuse.Invoke(
(MethodInvoker)delegate()
{
this.textBoxDiffuse.Text = "";
});
this.textBox1.Invoke(
(MethodInvoker)delegate()
{
this.textBox1.Text = "";
});
this.textBox2.Invoke(
(MethodInvoker)delegate()
{
this.textBox2.Text = "";
});
//convert theta to radians from degrees
theta *= Math.PI / 180;
//number of photons that have gone to albedo, direct, or diffuse transmittance
top = 0;
direct = 0;
diffuse = 0;
absorbed = 0;
failed = 0;
//Random number generator
Random r = new Random();
double randomValue = 0;
int count = 1000; //number of iterations of the problem...
double delta = 0.00001; //close enough to "1" for calculations, since C# random goes from [0, 1) instead of [0, 1]
//Calculate transmittance
for (ulong photonCount = 0; photonCount < photons; photonCount++)
{
bool scattered = false;
double newTheta = theta; //needed for looping
int i = 0; //counting variable used to prevent infinite looping
for (i = 0; i < count; i++)
{
double length = calculateTauP(); //length of the photon's travel
double newTau = calculateTau(newTheta, length);
if (newTau < 0)
{
top++; //photon has exited through the top
break; //move to the next photon
}
else if (newTau > tauStar)
{
//exited through the bottom of the cloud
if (scattered == false)
{
//direct transmittance
direct++;
}
else
{
//diffuse transmittance
diffuse++;
}
break;
}
else
{
//photon is either scattered or absorbed
randomValue = r.NextDouble();
if (randomValue >= omega) // || ((omega == 1) && (randomValue >= (omega - delta)) )
{
//photon absorbed, no longer of interest
absorbed++;
break;
}
else
{
//photon scattered, determine direction
scattered = true;
newTheta = calculateNewAngle(newTau, newTheta, g, randomValue);
}
}
}
if (i >= count)
{
failed++;
}
this.progressBar1.BeginInvoke(
(MethodInvoker)delegate()
{
this.progressBar1.PerformStep();
});
}
//Update Form values
displayData();
}
private void displayData()
{
if (this.textBoxAlbedo.InvokeRequired)
{
this.textBoxAlbedo.Invoke(
(MethodInvoker)delegate()
{
this.textBoxAlbedo.Text = ((double)top / (double)photons).ToString();
});
}
else
{
textBoxAlbedo.Text = ((double)top / (double)photons).ToString();
}
if (this.textBoxDirect.InvokeRequired)
{
this.textBoxDirect.Invoke(
(MethodInvoker)delegate()
{
this.textBoxDirect.Text = ((double)direct / (double)photons).ToString();
});
}
else
{
textBoxDirect.Text = ((double)direct / (double)photons).ToString();
}
if (this.textBoxDiffuse.InvokeRequired)
{
this.textBoxDiffuse.Invoke(
(MethodInvoker)delegate()
{
this.textBoxDiffuse.Text = ((double)diffuse / (double)photons).ToString();
});
}
else
{
textBoxDiffuse.Text = ((double)diffuse / (double)photons).ToString();
}
if (this.textBox1.InvokeRequired)
{
this.textBox1.Invoke(
(MethodInvoker)delegate()
{
this.textBox1.Text = absorbed.ToString();
});
}
else
{
textBox1.Text = absorbed.ToString();
}
if (this.textBox2.InvokeRequired)
{
this.textBox2.Invoke(
(MethodInvoker)delegate()
{
this.textBox2.Text = failed.ToString();
});
}
else
{
textBox2.Text = failed.ToString();
}
}
private double calculateNewAngle(double length, double angle, double g, double randomNumber)
{
double newAngle = 0;
double cos = (1 / (2 * g)) * (1 + Math.Pow(g, 2) - Math.Pow(((1 - Math.Pow(g, 2)) / (1 + g * (2 * randomNumber - 1))), 2));
newAngle += angle + cos;
while (newAngle >= 2 * Math.PI)
{
newAngle -= 2 * Math.PI; //normalize the angle to 0 <= angle < 2PI
}
return newAngle;
}
private double calculateTauP()
{
Random r = new Random();
double distance = -1 * Math.Log(1 - r.NextDouble());
return distance;
}
private double calculateTau(double angle, double tauP)
{
double tau = tauP * Math.Cos(Math.PI/2 - angle);
return tau;
}
}
}
Stop using Invoke and BeginInvoke to update the UI. Despite what you may have been told it is not that great of solution. Actually, in situations like these where all you want to to do is update the UI with progress information it is probably the worst solution. Instead, have your worker thread publish its progress information to an immutable data structure that can be shared with the UI thread. Then have your UI thread poll for it on a reasonable interval using a System.Windows.Forms.Timer.
public class YourForm : Form
{
private class ProgressInfo
{
public ProgressInfo(ulong top, ulong direct, ulong diffuse, ulong absorbed, ulong failed, ulong photons)
{
// Set properties here.
}
public ulong Top { get; private set; }
public ulong Direct { get; private set; }
public ulong Diffuse { get; private set; }
public ulong Dbsorbed { get; private set; }
public ulong Failed { get; private set; }
public ulong Photons { get; private set; }
}
private volatile ProgressInfo progress = null;
private void calculateModel()
{
for (ulong photonCount = 0; photonCount < photons; photonCount++)
{
// Do your calculations here.
// Publish new progress information.
progress = new ProgressInfo(/* ... */);
}
}
private void UpdateTimer_Tick(object sender, EventArgs args)
{
// Get a local reference to the data structure.
// This is all that is needed since ProgressInfo is immutable
// and the member was marked as volatile.
ProgressInfo local = progress;
this.textBoxAlbedo.Text = ((double)local.Top / (double)local.Photons).ToString();
this.textBoxDirect.Text = ((double)local.Direct / (double)local.Photons).ToString();
this.textBoxDiffuse.Text = ((double)local.Diffuse / (double)local.Photons).ToString();
this.textBox1.Text = local.Absorbed.ToString();
this.textBox2.Text = local.Failed.ToString();
}
Notice several things here.
The code is a lot easier to understand and follow.
The UI thread gets to decide when and how often its controls should be updated.
You get more throughput on the worker thread since it does not have to wait for a response from the UI as would occur with Invoke.
I rip on these Invoke and BeginInvoke solutions a lot because in many cases they are terrible solutions. Using a BackgroundWorker is a little better, but it still forces you into the push method of updating the UI (via the same marshaling techniques behind the scenes nonetheless). The pull method can be (and often is) a more elegant solution and usually more efficient.