C# threading

sebax

Utente Attivo
1,460
406
CPU
Intel i7 2600K @ 4.7ghz -> 5.66Ghz bench
Dissipatore
EK Supreme HF FullNickel
Scheda Madre
Gigabyte P67A-UD5-B3
HDD
Samsung EVO 256 GB + 1TB HDD
RAM
G.Skill ECO 1600 CL7 + 16 GB Corsair 1600 @ 2260mhz 7-10-7-25-2T bench
GPU
ASUS ENGTX570 DirectCU II (mod bios) -> EK DCII FullNickel @ 1ghz bench
Audio
Integrato Realtek HD
Monitor
Samsung s22b150 1920x1080
PSU
Enermax Modu 82+ 625W
Case
DimasTech Easy 2.5 Black Graphite + Laing 500Plus + EK XtX 360 + Nanoxia Fx2000 + Rehobus
OS
Linux Mint
ciao a tutti, vorrei creare un thread secondario che esegua un ciclo molto impegnantivo (estrazione di numeri primi per esempio) senza che si blocchi l'interfaccia grafica. faccio una struct Estrai() con le istruzioni per l'estrazione nella classe Form1, nel metodo del click del bottone di avvio scrivo:


Thread th = new Thread(Estrai);
th.Start();
textBox2.Text = ris;

(ris è una variabile globale string col risultato) ma quando avvio il programma al clic sul bottone non succede niente... idee?
 

lorigio

Utente Attivo
817
238
CPU
Athlon II x3 435 With Noctua NH-D14
Scheda Madre
Asus M5A99X EVO AM3+
HDD
1TB + 160gb + WD My Passport Essential 500gb
RAM
Kingston ddr3 9-9-9-24 6gb 1333mhz
GPU
Gigabyte 5670 790mhz
Monitor
Samsung 22 pollici P2270HD
PSU
Corsair 400watt
Case
Cm 690 II advanced
OS
Windows 7 32 bit
Posta tutto il codice
 

sebax

Utente Attivo
1,460
406
CPU
Intel i7 2600K @ 4.7ghz -> 5.66Ghz bench
Dissipatore
EK Supreme HF FullNickel
Scheda Madre
Gigabyte P67A-UD5-B3
HDD
Samsung EVO 256 GB + 1TB HDD
RAM
G.Skill ECO 1600 CL7 + 16 GB Corsair 1600 @ 2260mhz 7-10-7-25-2T bench
GPU
ASUS ENGTX570 DirectCU II (mod bios) -> EK DCII FullNickel @ 1ghz bench
Audio
Integrato Realtek HD
Monitor
Samsung s22b150 1920x1080
PSU
Enermax Modu 82+ 625W
Case
DimasTech Easy 2.5 Black Graphite + Laing 500Plus + EK XtX 360 + Nanoxia Fx2000 + Rehobus
OS
Linux Mint
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 estr_primi
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

string ris = "";
int maxN, N = 0, ini;

private void button1_Click(object sender, EventArgs e)
{

int temp = Environment.TickCount;
try
{
maxN = Convert.ToInt32(textBox1.Text);
ini = Convert.ToInt32(TextBox3.Text);
}
catch (System.FormatException)
{
textBox2.Text = "Inserire un numero valido";
return;
}

Thread th = new Thread(Estrai);
th.Start();
textBox2.Text = ris;
toolStripStatusLabel1.Text = "Fino a " + textBox1.Text + " ci sono " + N +
" numeri primi. In " + (Environment.TickCount - temp) + " ms.";
N = 0;

}

void Estrai()
{
ris = "";
long i, p;
bool h = false;
for (p = ini; p <= maxN; p++)
{
h = false;
if ((p % 2 != 0 || p == 2) && (p % 3 != 0 || p == 3))
{
for (i = 3; i < p; i++)
{
if (p % i == 0)
{
h = true;
break;
}
}
if (h == false || p == 2 || p == 3)
{
ris += p + "\t";
N++;
}
}
}
}
}
}
 

lorigio

Utente Attivo
817
238
CPU
Athlon II x3 435 With Noctua NH-D14
Scheda Madre
Asus M5A99X EVO AM3+
HDD
1TB + 160gb + WD My Passport Essential 500gb
RAM
Kingston ddr3 9-9-9-24 6gb 1333mhz
GPU
Gigabyte 5670 790mhz
Monitor
Samsung 22 pollici P2270HD
PSU
Corsair 400watt
Case
Cm 690 II advanced
OS
Windows 7 32 bit
Tratto da wikipedia
C# [modifica]

Questo metodo scritto in linguaggio C# consente la stampa in output tutti i numeri primi inferiori al parametro "limite".
Si basa su un ciclo che scorre tutti i numeri inferiori al parametro e valuta tramite un secondo metodo di nome "primo" se ogni singolo numero è primo o meno.
using System;

namespace Numeri_Primi
{
class Program
{
static void Main(string[] args)
{
int limite;
Console.WriteLine("Scrivi Limite:");
limite = Convert.ToInt32(Console.ReadLine());
if (limite > 1)
{
int pos = 2;
while (pos <= limite)
{
if (primo(pos)) Console.WriteLine(pos);
pos++;
}
}
Console.Read();
}

static protected bool primo(int numero)
{
int pos = (numero + 1) / 2;
while (pos > 1)
{
if (numero % pos == 0 && pos != numero && pos != 1) return false;
pos--;
}
return true;
}
}
}
 

ypkdani

Utente Èlite
1,511
39
CPU
Intel Quad 6600
Scheda Madre
Asus P5ql/epu
HDD
500GB
RAM
2gb Geil Ultra 6400
GPU
Hd2600xt 512mb
Monitor
Acer
PSU
600w
OS
Windows 7 Pro x64
devi dare:
Thread thread1 = new Thread(delegate() { funzione tua(); });
thread1.Start();
 

Entra

oppure Accedi utilizzando
Discord Ufficiale Entra ora!