First in form1 top:
FileInfo[] allfiles;
string mainDirectory;
Then in form1 constructor:
mainDirectory = @"c:\temp\screens7\";
var directory = new DirectoryInfo(mainDirectory);
allfiles = directory.GetFiles();
trackBar1.Minimum = 0;
trackBar1.Maximum = allfiles.Length;
Then button click event:
private void button1_Click(object sender, EventArgs e)
{
if (allfiles.Length > 1)
{
backgroundWorker1.RunWorkerAsync();
}
}
And the backgroundworker dowork event:
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
CreateAvi.AviMovie(allfiles,backgroundWorker1);
}
Progresschanged event:
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
label9.Invoke(new MethodInvoker(delegate { label9.Text = allfiles[e.ProgressPercentage].FullName; }));
}
Then in another class i have this loop in a method:
public static void AviMovie(FileInfo[] FileNames,BackgroundWorker bgw1)
{
try
{
Bitmap bitmap = (Bitmap)Image.FromFile(FileNames[0].FullName);
AviManager aviManager =
new AviManager(@"c:\temp\new.avi", false);
VideoStream aviStream =
aviManager.AddVideoStream(false, 25, bitmap);
int count = 0;
for (int n = 0; n < FileNames.Length; n++)
{
if (FileNames[n].Length > 0)
{
bitmap =
(Bitmap)Bitmap.FromFile(FileNames[n].FullName);
aviStream.AddFrame(bitmap);
bitmap.Dispose();
count++;
int pctDone = count * 100 / FileNames.Length;
bgw1.ReportProgress(count);
}
}
aviManager.Close();
}
catch
{
string t = "error";
}
}
Some changes i did:
In the new class in the method AviMovie i added the bgw1 variable. And the reportprogress was: ReportProgress(pctDown)
In form1 in the progress changed event it was:
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e) { progressBar1.Value = e.ProgressPercentage; }
When it was like this the progressBar was getting to the end to 100% But then i wanted to report and display on label9 the files names each file name from the AviMovie method in the new class.
First i did:
label9.Text = allfiles[e.ProgressPercentage].FullName;
Then later i added the Invoke.
Now the problem was that it never showed the right files its all the time stopped on file 101 out of 167. And progressbar also stopped just a little before the end. And all this problems happened when i started to use the label9.
And when it was stop just a little before the end on file 101 after few seconds it throw exception:
System.Reflection.TargetInvocationException was unhandled
HResult=-2146232828
Message=Exception has been thrown by the target of an invocation.
Source=mscorlib
StackTrace:
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
at System.Reflection.RuntimeMethodInfo.UnsafeInvoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Delegate.DynamicInvokeImpl(Object[] args)
at System.Windows.Forms.Control.InvokeMarshaledCallbackDo(ThreadMethodEntry tme)
at System.Windows.Forms.Control.InvokeMarshaledCallbackHelper(Object obj)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Windows.Forms.Control.InvokeMarshaledCallback(ThreadMethodEntry tme)
at System.Windows.Forms.Control.InvokeMarshaledCallbacks()
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at WindowsFormsApplication1.Program.Main() in d:\C-Sharp\ReadWriteToMemory\WindowsFormsApplication1\WindowsFormsApplication1\Program.cs:line 19
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException: System.ArgumentOutOfRangeException
HResult=-2146233086
Message=Value of '101' is not valid for 'Value'. 'Value' should be between 'minimum' and 'maximum'.
Parameter name: Value
Source=System.Windows.Forms
ParamName=Value
StackTrace:
at System.Windows.Forms.ProgressBar.set_Value(Int32 value)
at WindowsFormsApplication1.Form1.backgroundWorker1_ProgressChanged(Object sender, ProgressChangedEventArgs e) in d:\C-Sharp\ReadWriteToMemory\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs:line 292
at System.ComponentModel.BackgroundWorker.OnProgressChanged(ProgressChangedEventArgs e)
at System.ComponentModel.BackgroundWorker.ProgressReporter(Object arg)
InnerException:
This is strange since in the form1 constructor i set the progreaaBar1 maximum to the allfile.Lenght And the minimum to 0. So i changed now in the form1 designer the progressBar1 to mnimum 0 and maximum 1000 i set it to 1000 just for the test.
When i set it in the designer to maximum 1000 it was getting to 167 like it was suppose to do but then in the end it throw another exception:
System.Reflection.TargetInvocationException was unhandled
HResult=-2146232828
Message=Exception has been thrown by the target of an invocation.
Source=mscorlib
StackTrace:
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
at System.Reflection.RuntimeMethodInfo.UnsafeInvoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Delegate.DynamicInvokeImpl(Object[] args)
at System.Windows.Forms.Control.InvokeMarshaledCallbackDo(ThreadMethodEntry tme)
at System.Windows.Forms.Control.InvokeMarshaledCallbackHelper(Object obj)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Windows.Forms.Control.InvokeMarshaledCallback(ThreadMethodEntry tme)
at System.Windows.Forms.Control.InvokeMarshaledCallbacks()
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at WindowsFormsApplication1.Program.Main() in d:\C-Sharp\ReadWriteToMemory\WindowsFormsApplication1\WindowsFormsApplication1\Program.cs:line 19
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException: System.IndexOutOfRangeException
HResult=-2146233080
Message=Index was outside the bounds of the array.
Source=System.Windows.Forms
StackTrace:
at System.Windows.Forms.Control.MarshaledInvoke(Control caller, Delegate method, Object[] args, Boolean synchronous)
at System.Windows.Forms.Control.Invoke(Delegate method, Object[] args)
at System.Windows.Forms.Control.Invoke(Delegate method)
at WindowsFormsApplication1.Form1.backgroundWorker1_ProgressChanged(Object sender, ProgressChangedEventArgs e) in d:\C-Sharp\ReadWriteToMemory\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs:line 293
at System.ComponentModel.BackgroundWorker.OnProgressChanged(ProgressChangedEventArgs e)
at System.ComponentModel.BackgroundWorker.ProgressReporter(Object arg)
InnerException:
This is line 292 in form1 :
progressBar1.Value = e.ProgressPercentage;
This is line 293 in form1:
label9.Invoke(new MethodInvoker(delegate { label9.Text = allfiles[e.ProgressPercentage].FullName; }));
Why i needed to change the progressBar1 maximum to 1000 in the designer if i changed it to allfiles.Lenght already in the constructor ? else it will stop at file 101 and throw the exception with mininmum and maximum.
What is the second exception ? And how can i fix all this label9 files names report ? In the AviMovie method in the new class i tried in this line:
bgw1.ReportProgress(count);
To report the count then the variable n then the pctDone. IN all cases it went only untill file 101 and throw the exception.
** Its a bit long post but its all connected tried to explain it more clearly.
backgroundWorker1_ProgressChanged
method and check what the values are. Check thetrackBar1.Maximum
value. Put a breakpoint where you initially set it, to make sure it gets set correctly. – Sheridan