Download multithread
// 28/2/2004 - Salvatore Meschini
// Il progetto è compilabile da riga di comando con csc.exe
// csc /target:winexe nomefile.cs
// Il codice mostra come visualizzare un'immagine in un controllo
// PictureBox scaricandola da Internet (in modalità multithread).
// In questo modo l'utente può interagire con l'applicazione
// anche durante il download delle immagini...
using System;
using System.Windows.Forms;
using System.Net;
using System.Drawing;
using System.Threading;
namespace MyFormProject
{
class MainForm : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox Indirizzo;
private System.Windows.Forms.Button CaricaBtn;
private System.Windows.Forms.PictureBox MioPicBox;
public MainForm()
{
InitializeComponent();
}
void InitializeComponent() {
this.MioPicBox = new System.Windows.Forms.PictureBox();
this.CaricaBtn = new System.Windows.Forms.Button();
this.Indirizzo = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// MioPicBox
//
this.MioPicBox.Location = new System.Drawing.Point(24, 16);
this.MioPicBox.Name = "MioPicBox";
this.MioPicBox.Size = new System.Drawing.Size(248, 136);
this.MioPicBox.TabIndex = 0;
this.MioPicBox.TabStop = false;
//
// CaricaBtn
//
this.CaricaBtn.Location = new System.Drawing.Point(104, 192);
this.CaricaBtn.Name = "CaricaBtn";
this.CaricaBtn.TabIndex = 1;
this.CaricaBtn.Text = "Carica";
// Gestore evento "Clicca sul tasto"
this.CaricaBtn.Click += new System.EventHandler(this.CaricaBtnClick);
//
// Indirizzo
//
this.Indirizzo.Location = new System.Drawing.Point(16, 160);
this.Indirizzo.Name = "Indirizzo";
this.Indirizzo.Size = new System.Drawing.Size(256, 20);
this.Indirizzo.TabIndex = 2;
this.Indirizzo.Text = "http://localhost/apache_pb.gif";
//
// MainForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 230);
this.Controls.Add(this.Indirizzo);
this.Controls.Add(this.CaricaBtn);
this.Controls.Add(this.MioPicBox);
this.Name = "MainForm";
this.Text = "Carica Immagine da URL";
this.ResumeLayout(false);
}
[STAThread]
public static void Main(string[] args)
{
Application.Run(new MainForm());
}
// Classe per la gestione del download (accetta parametri)
public class ThreadScaricamento
{
private string pURL;
private PictureBox pPicBox;
private bool Esito = false;
// Imposta i parametri (PictureBox per riferimento)
public ThreadScaricamento(ref PictureBox PicBox, string URL)
{
pURL = URL;
pPicBox = PicBox;
}
// Esegue le operazioni più importanti
public void Scarica()
{
try
{
// Prova a caricare l'immagine da un URL
pPicBox.Image = Image.FromStream(((WebRequest.Create(pURL)).GetResponse()).GetResponseStream());
}
catch (WebException)
// Si possono gestire anche altre eccezioni
{
pPicBox.Image = null; // In caso di errore
}
Esito = (pPicBox.Image != null); // True o False
HoFinito();
}
// Il metodo viene richiamato quando il thread termina
// Da modificare in base alle proprie esigenze
private void HoFinito()
{
if (Esito)
MessageBox.Show("TUTTO OK!"); else MessageBox.Show("ERRORE!");
}
} // Fine Classe ThreadScaricamento
// Alla pressione del tasto:
void CaricaBtnClick(object sender, System.EventArgs e)
{
if (Indirizzo.Text != "") // Indirizzo è un controllo EditBox
{
// Passa i parametri
ThreadScaricamento TS = new ThreadScaricamento(ref MioPicBox, Indirizzo.Text);
Thread T = new Thread(new ThreadStart(TS.Scarica)); // Crea il Thread
T.Start(); // Avvia il thread
}
else MessageBox.Show("Devi fornire un indirizzo...");
}
}
}
Click here - Clicca qui