Hjælp til tråde og callback
HejsaJeg har nu bakset med det her et par dage, og kan bare ikke få skidtet til at virke.
Jeg får en fejl
"System.InvalidOperationException was unhandled
Message="Invoke eller BeginInvoke kan ikke kaldes for et objekt, før der er oprettet en vindues-handle."
Source="System.Windows.Forms"
StackTrace:
ved System.Windows.Forms.Control.MarshaledInvoke(Control caller, Delegate method, Object[] args, Boolean synchronous)
ved System.Windows.Forms.Control.Invoke(Delegate method, Object[] args)
ved Photo_organizer.ProgressForm.SetlblNumberProcessed(Int32 i) i C:\Documents and Settings\Rasmus\Dokumenter\Visual Studio 2005\Projects\Photo organizer\Photo organizer\ProgressForm.cs:linje 417
ved Photo_organizer.FindExif.Run(Object status) i C:\Documents and Settings\Rasmus\Dokumenter\Visual Studio 2005\Projects\Photo organizer\Photo organizer\FindExif.cs:linje 49
ved System.Threading._ThreadPoolWaitCallback.WaitCallback_Context(Object state)
ved System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
ved System.Threading._ThreadPoolWaitCallback.PerformWaitCallback(Object state)
Her kommer koden (fylder en del efterhånden, men har fjernet det uvæsentlige
Jeg får min fejl til sidst i koden i public void SetlblNumberProcessed(int i)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;
using System.Configuration;
using JSG.PhotoPropertiesLibrary;
using FindAllFilesInFolders;
using System.Threading;
namespace Photo_organizer
{
public partial class ProgressForm : Form, IProgressCallBack
{
private System.Threading.ManualResetEvent initEvent = new System.Threading.ManualResetEvent(false);
private System.Threading.ManualResetEvent abortEvent = new System.Threading.ManualResetEvent(false);
private List<FileInfo> pictures;
private List<string> libraries;
private const string APPTITLE = "Photo Organizer";
/// <summary>The error title for this application.</summary>
private const string APPERRORTITLE = APPTITLE + " Error";
private List<Photo> picturelist;
private delegate bool InitializePhotoPropertiesDelegate(string xmlFileName);
public delegate void SetlblNumberProcessedInvoker(int i);
public delegate void SetProgressPerformStepInvoker();
private bool requiresClose = true;
public ProgressForm()
{
InitializeComponent();
_resultOptions = new JSG.PhotoPropertiesLibrary.ResultOptions();
//PhotoPropertiesLoad();
picturelist = new List<Photo>();
}
public void Run(List<string> libraries)
{
PhotoPropertiesLoad();
//Thread t = new Thread(new ThreadStart(GetEXIF));
this.libraries = libraries;
SearchForPictures();
GetEXIF();
}
#region overrides
protected override void OnLoad(System.EventArgs e)
{
base.OnLoad(e);
ControlBox = false;
initEvent.Set();
}
#endregion
internal void SearchForPictures()…
internal void GetEXIF()
{
FindExif fe;
this.lblStep.Text = "Step 2 of 4";
this.lblStatus.Text = "Getting EXIF data";
this.lblTextProcessed.Visible = true;
this.lblNumberProcessed.Visible = true;
this.Repaint();
this.progressBar1.Maximum = pictures.Count;
this.progressBar1.Step = 1;
int i = 1;
foreach (FileInfo fi in pictures)
{
fe = new FindExif(fi.FullName, _resultOptions, picturelist, _photoProps, i);
ProgressForm callback = new ProgressForm();
try
{
System.Threading.ThreadPool.QueueUserWorkItem(
new System.Threading.WaitCallback(fe.Run),
callback);
}
catch (System.Threading.ThreadAbortException)
{
// We want to exit gracefully here (if we're lucky)
}
catch (System.Threading.ThreadInterruptedException)
{
// And here, if we can
}
i++;
}
}
internal void SaveToDatabase()…
public void ShowMessage()…
#region EXIF
private void PhotoPropertiesLoad()…
private void InitializePhotoProperties(string initXmlFile)…
public void InitializePhotoPropertiesCompleted(IAsyncResult call)…
private void ContinueLoadingEvent()…
#endregion
#region IProgressCallBack Members
public void SetProgressPerformStep()
{
Invoke(new SetProgressPerformStepInvoker(DoProgressBar1PerformStep));
}
public void SetlblNumberProcessed(int i)
{
Invoke(new SetlblNumberProcessedInvoker(DoSetlblNumberProcessed), new object[] { i });
}
#endregion
#region Implementation members invoked on the owner thread
private void DoSetlblNumberProcessed(int i)
{
lblNumberProcessed.Text = i.ToString();
}
private void DoProgressBar1PerformStep()
{
this.progressBar1.PerformStep();
}
#endregion
}
}
----------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;
using System.Configuration;
using JSG.PhotoPropertiesLibrary;
using System.Threading;
namespace Photo_organizer
{
public class FindExif
{
private JSG.PhotoPropertiesLibrary.PhotoProperties _photoProps;
private string fileName;
private ResultOptions _resultOptions;
private List<Photo> picturelist;
private int i;
public FindExif()
{
}
//#region EXIF
public FindExif(string fileName, ResultOptions _resultOptions, List<Photo> picturelist, PhotoProperties _photoProps, int i)
{
this.fileName = fileName;
this._resultOptions = _resultOptions;
this.picturelist = picturelist;
this._photoProps = _photoProps;
this.i = i;
}
public void Run(object status)
{
IProgressCallBack callback = status as IProgressCallBack;
AnalyzeAndReport();
// make callback to update mainGUI
callback.SetlblNumberProcessed(i);
}
private void AnalyzeAndReport()…
private bool AnalyzeImageFile(string fileName, List<Photo> picturelist)…
private void ViewAnalysis(string filename, ResultOptions _resultOptions, List<Photo> picturelist)…
}
}
------------------------------------------------------------
I den sidste her, er det kaldet her callback.SetlblNumberProcessed(i);
Hvad er det lige jeg gør galt med min callback?