Inserire una progress bar indipendente in un winform

giovedì 22 febbraio 2007 - 09.50

andreapavia Profilo | Senior Member

ciao a tutti,
chiedo una cosa...

ho inserito una progressbar (in swf) indipendete (che mi ad esempio loading e si muove avanti e indietro) dalla durata dell'azione del codice...

in poce poche parole,,,

1) clicco sul bottone
2) la progress bar inizia a luppare
3) parte la select e la costruzione del datagrid
4) la progressbar sparisce

io ho gia' inserito la progressbar e funziona ma si blocca durante i punti 2 e 3 perche' tutte le risorse vengono prese sempre da 2 e 3

come faccio a rendere inidipendente la progress bar dall'elaborazione dati a livello di risorse????


grazie 1000

maurosegato Profilo | Newbie

Devi far girare la routine che muove la progressbar in un Thread differente..

'// A livello di istanza crei la variabile Thread
private _Thread as Threading.Thread = nothing
'// A livello di istanza crei la variabile che indica fin quando l'operazione è in esecuzione
private _MovingProgressBarEnable as boolean = false

'// Dalla routing del pulsante:
if me._Thread is nothing '// Controlla che non sia già in esecuzione
me._MovingProgressBarEnable = true

me._Thread = new Threading.Thread(addressof 'nome_tua_routine')
me._Thread.name = "Moving ProgressBar"
me._Thread .Start()
end if

'// Alla fine della routine che carica i dati:
me._MovingProgressBarEnable = false '// Fa terminare il Thread

'// Routine indipendente
private sub 'nome_tua_routine'() '// Senza argomenti
while me._MovingProgressBarEnable
'// Muove la progressbar
end while
me._Thread = nothing
end sub

ciao
Mauro Segato

andreapavia Profilo | Senior Member

ciao e grazie 1000 ma....

come faccio a creare dei thread in questo codice di test???,


Private Sub Form3_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load

Dim path As String
path = System.Environment.CurrentDirectory
path = path + "\titoloprovawinform.swf"
AxShockwaveFlash2.LoadMovie(0, path)

End Sub

Private Sub ButtonLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonLoad.Click
Dim path As String
path = System.Environment.CurrentDirectory
path = path + "\load.swf"
axShockwaveFlash1.LoadMovie(0, path)
selectAndBind()
End Sub

Private Sub selectAndBind()

Dim conStr As String = "Provider=SQLOLEDB;Data Source=localhost;Database=Northwind;Integrated Security=SSPI;"
Dim sqlStr As String = "SELECT * FROM orders"
Dim conn As OleDbConnection = New OleDbConnection(conStr)
Dim da As OleDbDataAdapter = New OleDbDataAdapter(sqlStr, conn)
Dim ds As DataSet = New DataSet
da.Fill(ds, "Employees")
conn.Close()

Dim conStr2 As String = "Provider=SQLOLEDB;Data Source=localhost;Database=Northwind;Integrated Security=SSPI;"
Dim sqlStr2 As String = "SELECT * FROM orders"

Dim conn2 As OleDbConnection = New OleDbConnection(conStr)
Dim da2 As OleDbDataAdapter = New OleDbDataAdapter(sqlStr, conn)
Dim ds2 As DataSet = New DataSet
da2.Fill(ds2, "Employees")
DataGrid2.DataSource = ds2.DefaultViewManager
conn.Close()

Dim conStr3 As String = "Provider=SQLOLEDB;Data Source=localhost;Database=Northwind;Integrated Security=SSPI;"
Dim sqlStr3 As String = "SELECT * FROM orders"
Dim conn3 As OleDbConnection = New OleDbConnection(conStr)
Dim da3 As OleDbDataAdapter = New OleDbDataAdapter(sqlStr, conn)
Dim ds3 As DataSet = New DataSet
da3.Fill(ds3, "Employees")

conn.Close()

Dim iii As Integer
Dim aaa As Integer

Dim ttt As Integer = 1000
For aaa = 0 To 60
For iii = 0 To ds2.Tables(0).Rows.Count - 1
ds.Tables(0).ImportRow(ds2.Tables(0).Rows(iii))
Next
For iii = 0 To ds3.Tables(0).Rows.Count - 1
ds.Tables(0).ImportRow(ds3.Tables(0).Rows(iii))
Next
Next
DataGrid1.DataSource = ds.DefaultViewManager
End Sub
End Class



ti rungrazio ancora

maurosegato Profilo | Newbie

Ti ho modificato il codice.. di seguito i commenti..
Ciao

Private Sub Form3_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load

Dim path As String
path = System.Environment.CurrentDirectory
path = path + "\titoloprovawinform.swf"
AxShockwaveFlash2.LoadMovie(0, path)

End Sub

Private Sub ButtonLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonLoad.Click
Dim path As String
path = System.Environment.CurrentDirectory
path = path + "\load.swf"
axShockwaveFlash1.LoadMovie(0, path)
'// Crea e avvia il Thread separato
me._Thread = new Threading.Thread(addressof AsyncMovingProgressBar) '// Crea un nuovo Thread
me._Thread.Name = "Moving ProgressBar" '// Assegna il nome al Thread (opzionale)
me._Thread.Start() '// Avvia il Thread
selectAndBind()
me._CancelMoving = True '// indica la fine del movimento della progressbar e del Thread
End Sub

'// Ruotine per il movimento della ProgressBar
private sub AsyncMovingProgressBar()
while not me._CancelMoving '// Finchè è in esecuzione le routine che ti elabora i dati.....
'// .. Codice per il movimento della ProgressBar
end while
me._Thread = nothing '// Rilascia il Thread che sta terminando
me._CancelMoving = false '// Imposta su false la variabile che indica la fine del movomento della ProgressBar
end sub

Private Sub selectAndBind()

Dim conStr As String = "Provider=SQLOLEDB;Data Source=localhost;Database=Northwind;Integrated Security=SSPI;"
Dim sqlStr As String = "SELECT * FROM orders"
Dim conn As OleDbConnection = New OleDbConnection(conStr)
Dim da As OleDbDataAdapter = New OleDbDataAdapter(sqlStr, conn)
Dim ds As DataSet = New DataSet
da.Fill(ds, "Employees")
conn.Close()

Dim conStr2 As String = "Provider=SQLOLEDB;Data Source=localhost;Database=Northwind;Integrated Security=SSPI;"
Dim sqlStr2 As String = "SELECT * FROM orders"

Dim conn2 As OleDbConnection = New OleDbConnection(conStr)
Dim da2 As OleDbDataAdapter = New OleDbDataAdapter(sqlStr, conn)
Dim ds2 As DataSet = New DataSet
da2.Fill(ds2, "Employees")
DataGrid2.DataSource = ds2.DefaultViewManager
conn.Close()

Dim conStr3 As String = "Provider=SQLOLEDB;Data Source=localhost;Database=Northwind;Integrated Security=SSPI;"
Dim sqlStr3 As String = "SELECT * FROM orders"
Dim conn3 As OleDbConnection = New OleDbConnection(conStr)
Dim da3 As OleDbDataAdapter = New OleDbDataAdapter(sqlStr, conn)
Dim ds3 As DataSet = New DataSet
da3.Fill(ds3, "Employees")

conn.Close()

Dim iii As Integer
Dim aaa As Integer

Dim ttt As Integer = 1000
For aaa = 0 To 60
For iii = 0 To ds2.Tables(0).Rows.Count - 1
ds.Tables(0).ImportRow(ds2.Tables(0).Rows(iii))
Next
For iii = 0 To ds3.Tables(0).Rows.Count - 1
ds.Tables(0).ImportRow(ds3.Tables(0).Rows(iii))
Next
Next
DataGrid1.DataSource = ds.DefaultViewManager
End Sub

'// Variabili a livello di istanza
private _Thread as Threading.Thread = nothing '// Thread nel quale far girare la routine che muove la progressbar
private _CancelMoving as boolean = false '// Indica quando terminare il Thread


Mauro Segato

andreapavia Profilo | Senior Member

ciao!!!,,, ti ringrazio vivamente ma mi anche col tuo codice non funziona...

il flash si inchioda quando parte il binding...


qui c'è il progetto completto (zip) con gia' le classi e i files flash importati,,, basta solo scampattarla e lanciala.... credo che possa essere un buon esempio per integrare i banner flash in una winform.... peccato che si inchiodi


http://www.scacco-mattoband.com/dwn.html

io uso il framework 1.1....



bujia Profilo | Newbie

Ciao!

Anch'io usando il C++ .NET Framework 1.1 ho lo stesso problema...
Questo è il codice di un semplice esempio di prova... in una form con un solo Button di cui gestisco l'evento Click, il quale dovrebbe far partire la progressBar mentre la thread principale è in Sleep... invece la progressBar parte solo una volta che si è usciti dalla funzione che gestisce l'evento... (button1_Click) quindi quando ormai non mi interessa più... infatti nella mia applicazione io devo azionare la progressBar durante una fase lenta di download dati da un database, simulata in questo programma dalla Sleep.

Qualcuno mi sa aiutare?
Grazie mille in anticipo.
Ecco di seguito il codice del file Form.h

--------------------------------------------

#pragma once


namespace ProvaProgressBar
{
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace System::Threading;

/// <summary>
/// Riepilogo per Form1
///
/// AVVISO: se si modifica il nome della classe, sarà necessario modificare la
/// proprietà "Nome file di risorse" relativa allo strumento di compilazione delle risorse gestite
/// associate a tutti i file RESX da cui dipende la classe. In caso contrario,
/// le finestre di progettazione non saranno in grado di interagire correttamente con le
/// risorse localizzate associate al form.
/// </summary>
public __gc class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
}

protected:
void Dispose(Boolean disposing)
{
if (disposing && components)
{
components->Dispose();
}
__super::Dispose(disposing);
}
private: System::Windows::Forms::Button * button1;
private: System::Windows::Forms::ProgressBar * progressBar1;

private:
/// <summary>
/// Variabile di progettazione necessaria.
/// </summary>
System::ComponentModel::Container * components;

/// <summary>
/// Metodo necessario per il supporto della finestra di progettazione. Non modificare
/// il contenuto del metodo con l'editor di codice.
/// </summary>
void InitializeComponent(void)
{
this->button1 = new System::Windows::Forms::Button();
this->progressBar1 = new System::Windows::Forms::ProgressBar();
this->SuspendLayout();
//
// button1
//
this->button1->Location = System::Drawing::Point(64, 48);
this->button1->Name = S"button1";
this->button1->Size = System::Drawing::Size(136, 48);
this->button1->TabIndex = 0;
this->button1->Text = S"button1";
this->button1->Click += new System::EventHandler(this, button1_Click);
//
// progressBar1
//
this->progressBar1->Location = System::Drawing::Point(40, 128);
this->progressBar1->Name = S"progressBar1";
this->progressBar1->Size = System::Drawing::Size(216, 16);
this->progressBar1->TabIndex = 1;
//
// Form1
//
this->AutoScaleBaseSize = System::Drawing::Size(5, 13);
this->ClientSize = System::Drawing::Size(292, 266);
this->Controls->Add(this->progressBar1);
this->Controls->Add(this->button1);
this->Name = S"Form1";
this->Text = S"Form1";
this->ResumeLayout(false);

}
private: System::Void button1_Click(System::Object * sender, System::EventArgs * e)
{
progressBar1->Maximum = 100;
progressBar1->Value = 0;

Thread* t = new Thread(new ThreadStart(this,miafunzionelunga));
t->Priority = ThreadPriority::Lowest;
t->Start();

Thread::CurrentThread->Sleep(10000);
}

private: System::Void miafunzionelunga()
{
progressBar1->Value = 0;

for (int i=0; i<500; i++) {
Thread::CurrentThread->Sleep(10);
progressBar1->Invoke(new MethodInvoker(this,funzione));
}

}

private: System::Void funzione()
{
static flag = true;
if (flag) {
progressBar1->Value++;
if (progressBar1->Value==progressBar1->Maximum) flag = false;
}
else {
progressBar1->Value--;
if (progressBar1->Value==0) flag = true;
}
}

};
}


------------------------------------------------

Alessandro
Partecipa anche tu! Registrati!
Hai bisogno di aiuto ?
Perchè non ti registri subito?

Dopo esserti registrato potrai chiedere
aiuto sul nostro Forum oppure aiutare gli altri

Consulta le Stanze disponibili.

Registrati ora !
Copyright © dotNetHell.it 2002-2024
Running on Windows Server 2008 R2 Standard, SQL Server 2012 & ASP.NET 3.5