Usuario
un thread con numeros aleatorios

Pregunta
-
Hola como estan...
mi problema es este hasta ahora tengo mis funciones enviar y medicion como lo ven a continuacion:
using System;
using System.IO;
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;
using LabJack.LabJackUD;
using System.Windows.Forms.DataVisualization.Charting;
namespace Fix
{
public partial class Form1 : Form
{
private U3 u3;
double AIN0 = 0, AIN1 = 0, tiempo = 0, tiempo1 = 0, To = 1, entrada=3;
Thread h1, h2;
delegate void SetTextCallback(string text);
delegate void SetChartCallback(Chart chart);
public Form1()
{
InitializeComponent();
u3 = new U3(LJUD.CONNECTION.USB, "0", true);
LJUD.ePut(u3.ljhandle, LJUD.IO.PIN_CONFIGURATION_RESET, 0, 0, 0);
}
public void enviar()
{
while (true)
{
double dblValue0;
if (entrada == 0)
dblValue0 = 0;
else
dblValue0 = entrada;
int binary = 0;
LJUD.eDAC(u3.ljhandle, 0, dblValue0, binary, 0, 0);
Thread.Sleep(1000); // Detiene el hilo por 1 segundo este sera el tiempo de muestreo
}
}
public void Medicion()
{
while (true)
{
SetChart(chart1);
tiempo = tiempo + To;
//Take a single-ended measurement from AIN0.
double dblValue = 99999;
int binary = 0;
LJUD.eAIN(u3.ljhandle, 0, 31, ref dblValue, 0, 0, 0, binary);
AIN0 =(dblValue);
SetText(AIN0.ToString());
Thread.Sleep(1000); // Detiene el hilo por 1 segundo este sera el tiempo de muestreo
}
}
private void SetText(string text2)
{
if (this.textBox1.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetText);
this.Invoke(d, new object[] { text2 });
}
else
{
this.textBox1.Text = text2;
}
}
private void SetText2(string text)
{
if (this.textBox2.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetText2);
this.Invoke(d, new object[] { text });
}
else
{
this.textBox2.Text = text;
}
}
private void SetChart(Chart chart)
{
if (this.chart1.InvokeRequired)
{
SetChartCallback g = new SetChartCallback(SetChart);
this.Invoke(g, new object[] { chart });
}
else
{
chart1.Series["AIN0"].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
chart1.Series["AIN0"].Points.AddXY(tiempo, AIN0);
chart1.Series["AIN0"].ChartArea = "ChartArea1";
}
}
private void button1_Click(object sender, EventArgs e)
{
// Ref = Convert.ToDouble(textBox1.Text);
h1 = new Thread(new ThreadStart(Medicion));
h2 = new Thread(new ThreadStart(enviar));
h1.Start();
h2.Start();
}
}
}
La cuestion es que quiero crear otro thread que me permita generar valores aleatorios de la variable "entrada" de 0 a 5 cada 0.1 segundos, con un incremento en 0.1, es decir, 0, 0.1, 0.2,......4.9, 5. y de esta manera no tener siempre el valor 3 para "entrada" sino que cada vez que se corra el thread enviar agarre un valor aleatorio de la variable "entrada".
Todas las respuestas
-
hola
es un desarrollo web el que estas realizando ? lo pregunto porque has puesto la pregunta en un foro web y no en Windows Application
los valores aleatorios puedes generarlos con la clase Random
con esa clase podrias generar valores de 0 a 5, pero ojo aleatorio no es un incremento, o sea seria al azar, si quieres un incremento constante usa un for que recorra de 0.1 en 0.1 cada valor hasta el final
quizas el thread podria acceder a una variable qe este por fuera que contenga el valor de entrada que debe usar, y cada thread que toma un valor lo incrementa en 0.1
saludos
Leandro Tuttini
Blog
Buenos Aires
Argentina