multithreading - How to apply threading in vb.net with passing an object of a custom class for a method -
i newbie here. need help. trying develop tool want implement multi-threading in vb.net. stuck in place not resolve. let me explain you.
i have created custom class below:
public class testclass 'few private members of class private string,b string public structure abc dim xx object dim yy object dim zz object end structure public aa(10) abc 'now needed override constructor initialization of 'instance of class can made in 2 different ways due 'requirement public sub new(byval xy string,byref yz object) 'some internal method initialize object me.a=xy me.b=xy end sub 'another way create instance public sub new(byval xy string,byval yz string) 'some internal method initialize object me.a=xy me.b=yz end sub 'now public method want call using threading public sub testsub() 'do me.a,me.b , aa(some index) end sub end class
now in different module creating instance of class below:
dim x1 new testclass("some string",<some object reference>) dim x2 new testclass("some string","some string")
now have declared sub in same module below
sub domystuff(byref a1 testclass) a1.testsub() end sub
after want create thread , run sub "domystuff" passing reference of x1
to have imported system threading:
imports system.threading
inside module after initialization of x1 , x2 have written:
dim t1 new threading.thread(addressof domystuff) t1.start(x1)
here getting error: overload resolution failed because no 'new' can called arguments: 'public sub new(start system.threading.parameterizedthreadstart, maxstacksize integer)': method 'public sub domystuff(byref a1 testclass)' not have signature compatible delegate 'delegate sub parameterizedthreadstart(obj object)'. 'public sub new(start system.threading.threadstart, maxstacksize integer)': method 'public sub domystuff(byref a1 testclass)' not have signature compatible delegate 'delegate sub threadstart()'.
if writing this,
dim t1 threading.thread t1 = (addressof domystuff) t1.start(x1)
i getting error: 'addressof' expression can not converted 'system.threading.thread' because 'system.threading.thread' not delegate type.
might missing thing, unable find. not in delegate type concept. first project implementation of threading. looked answers in google unable understand sort out. highly solicited.
thanks
edit: have checked reference michael z. , helpful. first of big thank :)
however, unable understand thing in there. if me out there, highly grateful. per understanding, have changed code below:
public delegate sub test2(byref a1 testclass) public t1 test2 = new test2(addressof domystuff) public sub domystuff(byref a1 testclass) a1.testsub() end sub
now have declared in module follows:
dim t threading.thread = new threading.thread(addressof dostuff)
here unable understand how write dostuff sub in example working textbox1 object in ui, wrote:
public sub dostuff() if textbox1.invokerequired textbox1.invoke(t1,"hello") end if end sub
but here need work object custom made i.e. x1 or x2. need pass reference of object in thread method testsub can work created object either x1 or x2. totally lost here :(
please correct me if anywhere wrong understand reference, otherwise if correctly understood, can please me out here. appreciate in advance.
thanks
firstly, ensure you're calling method:
dim t1 new threading.thread(addressof domystuff) t1.start(x1)
next remove byref
in method parameter sub domystuff(a1 testclass)
essentially make second module like:
module module2 dim x1 new testclass("some string", "asd") dim x2 new testclass("some string", "some string") sub testcall dim t1 new thread(addressof domystuff) t1.start(x1) end sub private sub domystuff(a1 testclass) a1.testsub() end sub end module
and can call so:
testcall()
Comments
Post a Comment