c# - Why when using backgroundworker and progressBar and label im getting exceptions and wrong progress report? -



c# - Why when using backgroundworker and progressBar and label im getting exceptions and wrong progress report? -

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 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 class have loop in method:

public static void avimovie(fileinfo[] filenames,backgroundworker bgw1) { seek { 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; (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(); } grab { string t = "error"; } }

some changes did:

in new class in method avimovie added bgw1 variable. , reportprogress was: reportprogress(pctdown)

in form1 in progress changed event was:

private void backgroundworker1_progresschanged(object sender, progresschangedeventargs e) { progressbar1.value = e.progresspercentage; }

when progressbar getting end 100% wanted study , display on label9 files names each file name avimovie method in new class.

first did:

label9.text = allfiles[e.progresspercentage].fullname;

then later added invoke.

now problem never showed right files time stopped on file 101 out of 167. , progressbar stopped little before end. , problems happened when started utilize label9.

and when stop little before end on file 101 after few seconds throw exception:

system.reflection.targetinvocationexception unhandled hresult=-2146232828 message=exception has been thrown target of invocation. source=mscorlib stacktrace: @ system.runtimemethodhandle.invokemethod(object target, object[] arguments, signature sig, boolean constructor) @ system.reflection.runtimemethodinfo.unsafeinvokeinternal(object obj, object[] parameters, object[] arguments) @ system.reflection.runtimemethodinfo.unsafeinvoke(object obj, bindingflags invokeattr, binder binder, object[] parameters, cultureinfo culture) @ system.delegate.dynamicinvokeimpl(object[] args) @ system.windows.forms.control.invokemarshaledcallbackdo(threadmethodentry tme) @ system.windows.forms.control.invokemarshaledcallbackhelper(object obj) @ system.threading.executioncontext.runinternal(executioncontext executioncontext, contextcallback callback, object state, boolean preservesyncctx) @ system.threading.executioncontext.run(executioncontext executioncontext, contextcallback callback, object state, boolean preservesyncctx) @ system.threading.executioncontext.run(executioncontext executioncontext, contextcallback callback, object state) @ system.windows.forms.control.invokemarshaledcallback(threadmethodentry tme) @ system.windows.forms.control.invokemarshaledcallbacks() @ system.windows.forms.control.wndproc(message& m) @ system.windows.forms.control.controlnativewindow.onmessage(message& m) @ system.windows.forms.control.controlnativewindow.wndproc(message& m) @ system.windows.forms.nativewindow.debuggablecallback(intptr hwnd, int32 msg, intptr wparam, intptr lparam) @ system.windows.forms.unsafenativemethods.dispatchmessagew(msg& msg) @ system.windows.forms.application.componentmanager.system.windows.forms.unsafenativemethods.imsocomponentmanager.fpushmessageloop(intptr dwcomponentid, int32 reason, int32 pvloopdata) @ system.windows.forms.application.threadcontext.runmessageloopinner(int32 reason, applicationcontext context) @ system.windows.forms.application.threadcontext.runmessageloop(int32 reason, applicationcontext context) @ system.windows.forms.application.run(form mainform) @ windowsformsapplication1.program.main() in d:\c-sharp\readwritetomemory\windowsformsapplication1\windowsformsapplication1\program.cs:line 19 @ system.appdomain._nexecuteassembly(runtimeassembly assembly, string[] args) @ system.appdomain.executeassembly(string assemblyfile, evidence assemblysecurity, string[] args) @ microsoft.visualstudio.hostingprocess.hostproc.runusersassembly() @ system.threading.threadhelper.threadstart_context(object state) @ system.threading.executioncontext.runinternal(executioncontext executioncontext, contextcallback callback, object state, boolean preservesyncctx) @ system.threading.executioncontext.run(executioncontext executioncontext, contextcallback callback, object state, boolean preservesyncctx) @ system.threading.executioncontext.run(executioncontext executioncontext, contextcallback callback, object state) @ system.threading.threadhelper.threadstart() innerexception: system.argumentoutofrangeexception hresult=-2146233086 message=value of '101' not valid 'value'. 'value' should between 'minimum' , 'maximum'. parameter name: value source=system.windows.forms paramname=value stacktrace: @ system.windows.forms.progressbar.set_value(int32 value) @ windowsformsapplication1.form1.backgroundworker1_progresschanged(object sender, progresschangedeventargs e) in d:\c-sharp\readwritetomemory\windowsformsapplication1\windowsformsapplication1\form1.cs:line 292 @ system.componentmodel.backgroundworker.onprogresschanged(progresschangedeventargs e) @ system.componentmodel.backgroundworker.progressreporter(object arg) innerexception:

this unusual since in form1 constructor set progreaabar1 maximum allfile.lenght , minimum 0. changed in form1 designer progressbar1 mnimum 0 , maximum 1000 set 1000 test.

when set in designer maximum 1000 getting 167 suppose in end throw exception:

system.reflection.targetinvocationexception unhandled hresult=-2146232828 message=exception has been thrown target of invocation. source=mscorlib stacktrace: @ system.runtimemethodhandle.invokemethod(object target, object[] arguments, signature sig, boolean constructor) @ system.reflection.runtimemethodinfo.unsafeinvokeinternal(object obj, object[] parameters, object[] arguments) @ system.reflection.runtimemethodinfo.unsafeinvoke(object obj, bindingflags invokeattr, binder binder, object[] parameters, cultureinfo culture) @ system.delegate.dynamicinvokeimpl(object[] args) @ system.windows.forms.control.invokemarshaledcallbackdo(threadmethodentry tme) @ system.windows.forms.control.invokemarshaledcallbackhelper(object obj) @ system.threading.executioncontext.runinternal(executioncontext executioncontext, contextcallback callback, object state, boolean preservesyncctx) @ system.threading.executioncontext.run(executioncontext executioncontext, contextcallback callback, object state, boolean preservesyncctx) @ system.threading.executioncontext.run(executioncontext executioncontext, contextcallback callback, object state) @ system.windows.forms.control.invokemarshaledcallback(threadmethodentry tme) @ system.windows.forms.control.invokemarshaledcallbacks() @ system.windows.forms.control.wndproc(message& m) @ system.windows.forms.control.controlnativewindow.onmessage(message& m) @ system.windows.forms.control.controlnativewindow.wndproc(message& m) @ system.windows.forms.nativewindow.debuggablecallback(intptr hwnd, int32 msg, intptr wparam, intptr lparam) @ system.windows.forms.unsafenativemethods.dispatchmessagew(msg& msg) @ system.windows.forms.application.componentmanager.system.windows.forms.unsafenativemethods.imsocomponentmanager.fpushmessageloop(intptr dwcomponentid, int32 reason, int32 pvloopdata) @ system.windows.forms.application.threadcontext.runmessageloopinner(int32 reason, applicationcontext context) @ system.windows.forms.application.threadcontext.runmessageloop(int32 reason, applicationcontext context) @ system.windows.forms.application.run(form mainform) @ windowsformsapplication1.program.main() in d:\c-sharp\readwritetomemory\windowsformsapplication1\windowsformsapplication1\program.cs:line 19 @ system.appdomain._nexecuteassembly(runtimeassembly assembly, string[] args) @ system.appdomain.executeassembly(string assemblyfile, evidence assemblysecurity, string[] args) @ microsoft.visualstudio.hostingprocess.hostproc.runusersassembly() @ system.threading.threadhelper.threadstart_context(object state) @ system.threading.executioncontext.runinternal(executioncontext executioncontext, contextcallback callback, object state, boolean preservesyncctx) @ system.threading.executioncontext.run(executioncontext executioncontext, contextcallback callback, object state, boolean preservesyncctx) @ system.threading.executioncontext.run(executioncontext executioncontext, contextcallback callback, object state) @ system.threading.threadhelper.threadstart() innerexception: system.indexoutofrangeexception hresult=-2146233080 message=index outside bounds of array. source=system.windows.forms stacktrace: @ system.windows.forms.control.marshaledinvoke(control caller, delegate method, object[] args, boolean synchronous) @ system.windows.forms.control.invoke(delegate method, object[] args) @ system.windows.forms.control.invoke(delegate method) @ windowsformsapplication1.form1.backgroundworker1_progresschanged(object sender, progresschangedeventargs e) in d:\c-sharp\readwritetomemory\windowsformsapplication1\windowsformsapplication1\form1.cs:line 293 @ system.componentmodel.backgroundworker.onprogresschanged(progresschangedeventargs e) @ system.componentmodel.backgroundworker.progressreporter(object arg) innerexception:

this line 292 in form1 :

progressbar1.value = e.progresspercentage;

this line 293 in form1:

label9.invoke(new methodinvoker(delegate { label9.text = allfiles[e.progresspercentage].fullname; }));

why needed alter progressbar1 maximum 1000 in designer if changed allfiles.lenght in constructor ? else stop @ file 101 , throw exception mininmum , maximum.

what sec exception ? , how can prepare label9 files names study ? in avimovie method in new class tried in line:

bgw1.reportprogress(count);

to study count variable n pctdone. in cases went untill file 101 , throw exception.

** bit long post connected tried explain more clearly.

your indexing fileinfo kind of percentage. have reverse calculation count index array properly.

c# .net winforms

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -