Eccezione invoke

mercoledì 16 giugno 2010 - 09.41

streetman84 Profilo | Newbie

Ciao a tutti,
ho un' applicazione winform che deve aprire una progressbar, dati i problemi del gestire due thread diversi ho gestito i suoi valori così:

private delegate int getValueDelegate(); private delegate void setValueDelegate(int value); public int MaxValue { get { return (int)this.Invoke(new getValueDelegate(getMaxValue)); } set { this.Invoke(new setValueDelegate(setMaxValue), value); } } private int getMaxValue() { return progressBar1.Maximum; } private void setMaxValue(int value) { progressBar1.Maximum = value; }
ma mi restituisce questa eccezione:
{"Impossibile chiamare Invoke o BeginInvoke su un controllo finché non viene creato un handle di finestra."} System.SystemException {System.InvalidOperationException}

è come se non avesse caricato la finestra ma io il contenitore della barra lo vedo... qualcuno sa come risolvere?
Grazie in anticipo.

aiedail92 Profilo | Expert

Ciao,

Prova ad usare InvokeRequired per sapere se è necessario utilizzare Invoke, quindi agisci di conseguenza:

private delegate int getValueDelegate(); private delegate void setValueDelegate(int value); public int MaxValue { get { if(this.InvokeRequired) { return (int)this.Invoke(new getValueDelegate(getMaxValue)); } else { return getMaxValue(); } } set { if(this.InvokeRequired) { this.Invoke(new setValueDelegate(setMaxValue), value); } else { setMaxValue(value); } } } private int getMaxValue() { return progressBar1.Maximum; } private void setMaxValue(int value) { progressBar1.Maximum = value; }

Luca

streetman84 Profilo | Newbie

ora l'eccezione è cambiata..
MaxValue 'this.MaxValue' ha generato un'eccezione di tipo 'System.NullReferenceException' int {System.NullReferenceException}

e così per Value e MinValue...

aiedail92 Profilo | Expert

Sembra che progressBar1 non sia inizializzata...

Luca

streetman84 Profilo | Newbie

esatto.. se vuoi ti posto il codice della classe... è da ieri che sbatto la testa..
Marco

aiedail92 Profilo | Expert

Ok, prova

Luca

streetman84 Profilo | Newbie

namespace Newton.WorkFlow { /// <summary> /// Descrizione di riepilogo per WaitForm. /// </summary> public class WaitForm : System.Windows.Forms.Form { private System.Windows.Forms.ProgressBar progressBar1; private IContainer components = null; public WaitForm() { // // Necessario per il supporto di Progettazione Windows Form // InitializeComponent(); // // TODO: aggiungere il codice del costruttore dopo la chiamata a InitializeComponent // } /// <summary> /// Pulire le risorse in uso. /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if(components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Codice generato da Progettazione Windows Form /// <summary> /// Metodo necessario per il supporto della finestra di progettazione. Non modificare /// il contenuto del metodo con l'editor di codice. /// </summary> private void InitializeComponent() { this.progressBar1 = new System.Windows.Forms.ProgressBar(); this.SuspendLayout(); // // progressBar1 // this.progressBar1.Dock = System.Windows.Forms.DockStyle.Fill; this.progressBar1.Location = new System.Drawing.Point(0, 0); this.progressBar1.Name = "progressBar1"; this.progressBar1.Size = new System.Drawing.Size(592, 49); this.progressBar1.TabIndex = 0; // // WaitForm // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(592, 49); this.ControlBox = false; this.Controls.Add(this.progressBar1); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog; this.MaximizeBox = false; this.MinimizeBox = false; this.Name = "WaitForm"; this.ShowInTaskbar = false; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "Trasferimento dati in corso..."; this.TopMost = true; this.ResumeLayout(false); } #endregion private delegate void waitFormDelegate(int value); private delegate int getValueDelegate(); private delegate void setValueDelegate(int value); public int MaxValue { get { if (this.InvokeRequired) { return (int)this.Invoke(new getValueDelegate(getMaxValue)); } else { return getMaxValue(); } } set { if (this.InvokeRequired) { this.Invoke(new setValueDelegate(setMaxValue), value); } else { setMaxValue(value); } } } private int getMaxValue() { return progressBar1.Maximum; } private void setMaxValue(int value) { progressBar1.Maximum = value; } public int Value { get { if (this.InvokeRequired) { return (int)this.Invoke(new getValueDelegate(getValue)); } else { return getValue(); } } set { if (this.InvokeRequired) { this.Invoke(new setValueDelegate(setValue), value); } else { setValue(value) ; } } } private int getValue() { return progressBar1.Value; } private void setValue(int value) { progressBar1.Value = value; } public int MinValue { get { if (this.InvokeRequired) { return (int)this.Invoke(new getValueDelegate(getMinValue)); } else { return getMinValue(); } } set { if (this.InvokeRequired) { this.Invoke(new setValueDelegate(setMinValue), value); } else { setMinValue(value); } } } private int getMinValue() { return progressBar1.Value; } private void setMinValue(int value) { progressBar1.Value = value; } } }

aiedail92 Profilo | Expert

Non ho Visual Studio per fare dei test al momento, comqunque te la butto lì: prova a sostituire gli Invoke per le proprietà set, da così:

this.Invoke(new setValueDelegate(setMaxValue), value); this.Invoke(new setValueDelegate(setValue), value); this.Invoke(new setValueDelegate(setMinValue), value);

A così:

this.Invoke(new setValueDelegate(setMaxValue), new object[] { value }); this.Invoke(new setValueDelegate(setValue), new object[] { value }); this.Invoke(new setValueDelegate(setMinValue), new object[] { value });

Se non funziona, imposta un breakpoint in ogni funzione/proprietà e vedi dove viene sollevata esattamente l'eccezione.

Luca

streetman84 Profilo | Newbie

mi scatena sempre la stessa eccezione su maxValue, value, MinValue.
riferimento a un oggetto non impostato su un'istanza di oggetto.
cerco di essere più preciso...
questo form viene richiamato da una classe
public class DataSender: IDisposable { private static Hashtable threadList=new Hashtable(); private static int count=0; private static int runningId=0; private static WaitForm waitForm = new WaitForm(); Qui chiama il form

e subito dopo l'initializecomponent di WaitForm.cs che mi lancia l'eccezione di MaxValue,Value, MinValue

aiedail92 Profilo | Expert

Ciao, scusa ma sono stato via

Ti è possibile allegare il progetto? Solo con queste informazioni non mi viene in mente niente...

Luca
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