» »

POMOČ: C# Spremenitev spremenljivke medtem ko teče ukaz while

POMOČ: C# Spremenitev spremenljivke medtem ko teče ukaz while

lifestreamer ::

Delam progrm za upravljanje led diodad priklopljenih na LPT(printer) port.
Nastavil sem kako naj se led diode prižigajo in vmes dodal 100ms intervale.
Naredil sem da se ponavlja vendar je problem v tem da se program(forma) neha odzivati in tako ne morem spremeniti spremenljivke a na vrednost 1, da bi se zanka ustavila.
Prosim za pomoč.


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.Collections;
using System.Threading;
using System.Timers;

namespace TEST
{
public partial class Form1 : Form
{
int a = 0;
public Form1()
{
InitializeComponent();
PortAccess.Output(888, 0);
}
//reset
private void button1_Click(object sender, EventArgs e)
{
a = 1;
PortAccess.Output(888, 0);
}
//set
private void button2_Click(object sender, EventArgs e)
{
PortAccess.Output(888, 255);
}
//rider
private void button3_Click(object sender, EventArgs e)
{
a = 0;
while (a == 0)
{
PortAccess.Output(888, 4);
Thread.Sleep(100);

PortAccess.Output(888, 10);
Thread.Sleep(100);

PortAccess.Output(888, 17);
Thread.Sleep(100);

PortAccess.Output(888, 27);
Thread.Sleep(100);

PortAccess.Output(888, 31);
Thread.Sleep(100);

PortAccess.Output(888, 30);
Thread.Sleep(100);

PortAccess.Output(888, 28);
Thread.Sleep(100);

PortAccess.Output(888, 24);
Thread.Sleep(100);

PortAccess.Output(888, 16);
Thread.Sleep(100);

PortAccess.Output(888, 8);
Thread.Sleep(100);

PortAccess.Output(888, 4);
Thread.Sleep(100);

PortAccess.Output(888, 2);
Thread.Sleep(100);

PortAccess.Output(888, 1);
Thread.Sleep(100);

PortAccess.Output(888, 3);
Thread.Sleep(100);

PortAccess.Output(888, 7);
Thread.Sleep(100);

PortAccess.Output(888, 15);
Thread.Sleep(100);

PortAccess.Output(888, 31);
Thread.Sleep(100);

PortAccess.Output(888, 14);
Thread.Sleep(100);
if (a == 1)
{
PortAccess.Output(888, 0);
break;
}
}
}

}
}


LP in hvala.
lifestreamer

Monster ::

hm .. naredi to proceduro kot ločen thread ...
Ka zaboga...

Tutankhamun ::

Kaj si pa to ustvaru?
volatile int a; to napiš namesto samo int a;
pa naredi tako, kot je Monster reku, ločen thread.
Pa še neki:
const int [] arr = {4,10,17,27,31,30,28,24,16,8,4,2,1,3,7,15,31,14};
foreach (int i in arr)
{
   PortAccess.Output(888, i);
   Thread.Sleep(100);
}

Namesto da maš 100 enakih vrstic kode!!!!!
AMD Phenom QUAD 9950 Black Edition, 8GB

lifestreamer ::

Problem, je v tem da ne znam delati s threadi.
Prosim nekoga ki ima preveč časa in se trenutno dolgočasi, če lahko napiše kodo :|.
lifestreamer

Monster ::

.. se pa nauči ... pomojem ti bo vzelo manj kot dan časa ;) ... kr hrabro dalje!
Ka zaboga...

Tutankhamun ::

To pa res ni težko!

Nared eno funkcijo, ki se bo izvajala neodvisno od drugih.

void funThread ()
{
   while (pogoj)
   {
       // tuki not je koda od threada
   }

   // thread se bo ubil, ko se funkcija funThread zaključi
}


Potem je pa potrebno samo še kodo, da se ta funkcija zažene kot thread.

// kreiraš thread. Kot argument podaš ime funkcije, ki si jo določu da bo thread!
Thread t1 = new Thread (new ParameterizedThreadStart (funThread));

t1.Start (); // pol ga pa še zaženeš, funkcija pa dela kar pač dela


Za kej več, ti pa predlagam da pogledaš na net. Kako se thread suspenda ustavi... Mogoče boš rabu tudi kšnp sihronizacijo, mogoče kšn lock, al pa mutex, semafor.....
AMD Phenom QUAD 9950 Black Edition, 8GB

lifestreamer ::

const int [] arr = {4,10,17,27,31,30,28,24,16,8,4,2,1,3,7,15,31,14};
foreach (int i in arr)
{
   PortAccess.Output(888, i);
   Thread.Sleep(100);
}


Error 1 'arr' is of type 'int[]'. A const field of a reference type other than string can only be initialized with null.

Bral sem da moraš nekaj readonly narest, pa ne razumem prav dobr.


ZA THREAD:

Sem skopiral kodo in je prišlo do napake:
Error 1 No overload for 'funThread' matches delegate 'System.Threading.ParameterizedThreadStart'

Našel neko stvar ki je nekomu pomagala:

public void funThread(object uri)
{
Uri funThreadUri = uri as Uri;
funThread(uri);
}

public void funThread(Uri uri)
{
PortAccess.Output(888, 4);
Thread.Sleep(100);

PortAccess.Output(888, 10);
Thread.Sleep(100);

PortAccess.Output(888, 17);
Thread.Sleep(100);
}

Ko zaženbem program pride do napake in se pojavi okno za pošli al i ne pošlji poročilo napake.
lifestreamer

Zgodovina sprememb…

Tutankhamun ::

	public partial class Form1 : Form
	{
		public Form1()
		{
			InitializeComponent();
		}

		private readonly int[] arr = { 4, 10, 17, 27, 31, 30, 28, 24, 16, 8, 4, 2, 1, 3, 7, 15, 31, 14 };

		private void Form1_Load (object sender, EventArgs e)
		{
			Init ();
		}

		private void Init ()
		{
			Thread t1 = new Thread (new ParameterizedThreadStart (funThread));
			t1.Start ();
		}

		private void funThread ()
		{
			foreach (int i in arr)
			{
				//PortAccess.Output (888, i);
				Thread.Sleep (100);
			}
		}
	}
AMD Phenom QUAD 9950 Black Edition, 8GB

Zgodovina sprememb…

mHook ::

Lahko uporabiš tudi Application.DoEvents

Ericssony ::

Ali pa BackgroundWorker glede na to, da (še) ne obvladaš thread-ov.


Vredno ogleda ...

TemaSporočilaOglediZadnje sporočilo
TemaSporočilaOglediZadnje sporočilo
»

Keylogger

Oddelek: Programska oprema
352615 (1342) Blisk
»

[C#] pixel scan z Thread

Oddelek: Programiranje
121164 (946) perooo25
»

Java pomoč / Runnable problemi

Oddelek: Programiranje
7818 (683) Serial
»

[Visual C#] zaradi while zanke se ne pokaže okno

Oddelek: Programiranje
8984 (861) Fresh-Grass
»

c# ... kako je to mozno da Windows.Forms.Timer na istem threadu vec msg boxov pozene

Oddelek: Programiranje
51317 (1276) Vapo1

Več podobnih tem