Sv Community El Salvador
Soporte y Tecnología => Programación => C/C++ => Mensaje iniciado por: Willy59 en abril 14, 2015, 09:05:12 pm
-
Hola, e vuelto de nuevo y esta vez pidiendo una super ayuda
Estoy haciendo un Programa que simule un juego de carreras con 4 PictureBox haciendo que los 2 de la izquierda corran a la derecha:
(http://i.imgur.com/YDSvcxj.jpg)
Pero al momento de correrlo me sucede esto:
(http://i.imgur.com/H9QOObf.jpg)
Este es el código:
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;
using System.Threading;
namespace carreras
{
public partial class Form1 : Form
{
delegate void dele(PictureBox pb, int laY, int velocidad);
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
CheckForIllegalCrossThreadCalls = false;
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
Pen lapiz = new Pen(Color.Blue, 1);
Graphics pintor = CreateGraphics();
pintor.DrawLine(lapiz, new Point(12, 75), new Point(500, 75));
pintor.DrawLine(lapiz, new Point(12, 175), new Point(500, 175));
}
private void button1_Click(object sender, EventArgs e)
{
button1.Enabled = false;
Thread[] hilos = new Thread[3];
for (int i = 0; i < hilos.Length; i++)
{
hilos = new Thread(metodo);
hilos.Name = "h" + i;
hilos.Start();
}
button1.Enabled = true;
}
public void metodo()
{
dele elDelegado = new dele(mover);
if (Thread.CurrentThread.Name.Equals("h0"))
{
elDelegado.Invoke(Carro1, Carro1.Location.Y, 20);
}
else if (Thread.CurrentThread.Name.Equals("h1"))
{
elDelegado.Invoke(Carro2, Carro2.Location.Y, 60);
}
}
public void mover(PictureBox pb, int laY, int velocidad)
{
for (int i = 0; i < 450; i++)
{
pb.Location = new Point(i, laY);
if (Thread.CurrentThread.Name.Equals("h0"))
{
Thread.Sleep(velocidad);
}
else if (Thread.CurrentThread.Name.Equals("h1"))
{
Thread.Sleep(velocidad);
}
}
}
private void Carro1_Click(object sender, EventArgs e)
{
}
}
}
Mi guía para hacerlo fue este vídeo: https://www.youtube.com/watch?v=uDwbpcmNH00 (https://www.youtube.com/watch?v=uDwbpcmNH00)
-
Es cuando asignas algo a un elemento que se creó bajo otro contexto. Veo que estás usando sub procesos...
public delegate void ControlMoverCarro(Control control, Point nuevoPunto);
public void MoverCarro(Control control, Point punto)
{
if (control.InvokeRequired)
control.Invoke(new ControlMoverCarro(MoverCarro), new object[] { control, punto });//Invocando a sí mismo
else
control.Location = punto;//La parte funcional ejecutandose en el hilo principal
}
Podes sustituir la línea en la que tenés error por la llamada del método "MoverCarro" :thumbsup:
-
:-/ Estas usando threads y eso es difícil, el error en si te dice que estas tratando de usar un objeto que otro thread lo tiene en lock porque lo esta usando, no podes modificar su bloque de memoria porque lo estan usando bla bla bla...
Usa timers mejor, sin tanta paja xD
Ah y tu código es de C#
(http://snag.gy/kJs8L.jpg)