multithreading - Cross-thread operation not valid. C# -


i have class file sends integer form2 class function within class uses integer update picturebox1 image-location , form2 size.

the integer id of current map in (in irrelevant game).

here 2 scenarios play out.

1) now, if form2 opened prior function being executed (due not in-game yet due no id) , function executed (due being in-game) , system.invalidoperationexception @ line 'this.refresh();'.

2) now, if leave form2 unopened, enter game , open form2, picturebox1 image loaded; however, form size not updated. if hide form2 , enter new map (new id), system.invalidoperationexception @ line 'this.size...'. also, if not hide form in aforementioned sentence , enter new map, same error in scenario 1.

code

the following form2 class file has functions related 2 errors above.

public partial class form2 : form {     #region imports      [dllimport("kernel32.dll")]     static extern void outputdebugstring(string lpoutputstring);      #endregion      #region variables      public delegate void updatecontrolsdelegate();      public static form2 _form2;      #endregion      public form2()     {         initializecomponent();          _form2 = this;     }      map map = new map();     public void mupdate(int m)     {         outputdebugstring("{mupdate} map= " + m.tostring());          if (m <= 90000)         {             picturebox1.imagelocation = "http://randomspam.co/map/0000" + m.tostring() + ".img/minimap.canvas.png";         }         else         {             if (m <= 900000)             {                 picturebox1.imagelocation = "http://randomspam.co/map/000" + m.tostring() + ".img/minimap.canvas.png";             }             else             {                 if (m <= 9000000)                 {                     picturebox1.imagelocation = "http://randomspam.co/map/00" + m.tostring() + ".img/minimap.canvas.png";                 }                 else                 {                     picturebox1.imagelocation = "http://randomspam.co/map/" + m.tostring() + ".img/minimap.canvas.png";                 }             }         }          fupdate();          try         {             tooltip1.settooltip(picturebox1, map[m]);         }         catch         {           }     }      public void invokeupdatecontrols()     {         if (this.invokerequired)         {             this.invoke(new updatecontrolsdelegate(fupdate));         }         else         {             fupdate();         }     }      private void fupdate()     {         this.size = new size(picturebox1.width + 6, picturebox1.height + 24);          this.refresh();     } } 

errors

here errors related 2 scenarios above.

1)

   system.invalidoperationexception unhandled   hresult=-2146233079   message=cross-thread operation not valid: control 'form2' accessed thread other thread created on.   source=system.windows.forms   stacktrace:        @ system.windows.forms.control.get_handle()        @ system.windows.forms.control.invalidate(boolean invalidatechildren)        @ system.windows.forms.control.refresh()        @ sorprendente.form2.fupdate() in c:\users\andrew\documents\visual studio 2010\projects\msc\sorprendente\form2.cs:line 93        @ sorprendente.form2.mupdate(int32 m) in c:\users\andrew\documents\visual studio 2010\projects\msc\sorprendente\form2.cs:line 65        @ sorprendente.finteraction.fmtransfer(int32 m) in c:\users\andrew\documents\visual studio 2010\projects\msc\sorprendente\pipes.cs:line 272        @ sorprendente.pinteraction.readpipe() in c:\users\andrew\documents\visual studio 2010\projects\msc\sorprendente\pipes.cs:line 204        @ 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:  

2)

   system.invalidoperationexception unhandled   hresult=-2146233079   message=cross-thread operation not valid: control 'form2' accessed thread other thread created on.   source=system.windows.forms   stacktrace:        @ system.windows.forms.control.get_handle()        @ system.windows.forms.control.setboundscore(int32 x, int32 y, int32 width, int32 height, boundsspecified specified)        @ system.windows.forms.form.setboundscore(int32 x, int32 y, int32 width, int32 height, boundsspecified specified)        @ system.windows.forms.control.setbounds(int32 x, int32 y, int32 width, int32 height, boundsspecified specified)        @ system.windows.forms.control.set_size(size value)        @ system.windows.forms.form.set_size(size value)        @ sorprendente.form2.fupdate() in c:\users\andrew\documents\visual studio 2010\projects\msc\sorprendente\form2.cs:line 91        @ sorprendente.form2.mupdate(int32 m) in c:\users\andrew\documents\visual studio 2010\projects\msc\sorprendente\form2.cs:line 65        @ sorprendente.finteraction.fmtransfer(int32 m) in c:\users\andrew\documents\visual studio 2010\projects\msc\sorprendente\pipes.cs:line 272        @ sorprendente.pinteraction.readpipe() in c:\users\andrew\documents\visual studio 2010\projects\msc\sorprendente\pipes.cs:line 204        @ 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:  

i not understand how bypass these errors, information regarding on how fix current problem appreciated.

if request see additional class files/functions may related error, more willing post them.

use debug location toolbar in visual studio check thread you're on.

this other class of yours spawning new thread, mupdate not executed on windows thread. explained in comments above, cannot perform operations on windows controls on thread other main windows thread. includes properties such picturebox1.imagelocation , size.

to update controls thread other windows thread must via invoke, demonstrated in own code invokeupdatecontrols not calling.


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -