c# - Cannot access a non-static member of outer type 'FormMain' via nested type 'FormMain.ImageDelegateClass' -


i need update c# winforms picturebox memorystream input. able accomplish using

picturebox.image = new bitmap(new memorystream(payload)); 

in thread parses stream [rxthread()] advised use delegate avoid undesirable effects. i've implemented this:

private void rxthread() {     ...     var imagedelegateclass = new imagedelegateclass();     var imagedelegate = new imagedelegate(imagedelegateclass.setimage);     imagedelegate(payload);     ... }  delegate void imagedelegate(byte[] payload); class imagedelegateclass {     public void setimage(byte[] payload)     {         picturebox.image = new bitmap(new memorystream(payload));     } } 

but following error code when try compile:

cannot access non-static member of outer type 'formmain' via nested type 'formmain.imagedelegateclass'

i sure bad idea make picturebox static since winforms generated. know repair simple bit new c#. have read chapter on delegates in jon skeets c# in depth 2nd edition multiple times first time i've tried use one. how can change setimage() can access picturebox?

you should move method form class itself.
don't need separate class @ all.

also, can use built-in action<byte[]> delegate instead of creating own delegate type.

also, calling the delegate directly doesn't good; still run on background thread.

you need call begininvoke(new action<byte[]>(this.setimage), payload) run delegate on ui thread.


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -