firing events from a seperate thread

  • Thread starter Milosz - [playseven.com]
  • Start date
M

Milosz - [playseven.com]

i have an app which has a WorkPrepare Class
the WorkPrepare class initializes a Worker class
in theWorkPrepare class there is a function initWork() started in a separate
thread (second thread), which starts Worker.doWork() in seperate threads in
an for each construct.

when the method Worker.doWork() is finished it fires an event to the
WorkPrepare class with its result.

My Problem is that only the last result reaches the WorkPrepare class. the
function that handles this event is only invoked with the result for the
last task.

I thought that the multiple threads are colliding in the event handling
function so only the last event is handled.
i tred an application.doevents after firiing the event and even if i fire
the event in an own function and make an mutex around it only the the last
event is recognized by the handling function..

what i am doing wrong ?

in simplified Code:

Class PrepareWork
Private th as thread
Private Worker as new Worker
Private Count as integer
sub new(cnt a integer)
me.Count = cnt
th = new thread(AdressOf initWork)
th.start
end sub

Sub initWork()
for i = 1 to Count
Worker.doWork(i)
next
end sub
'this method is only invoked from the last task, with i = Count
Private Sub getRespond(msg as string) Handles Worker.Respond
'get the respond Message
end sub
End Class


Class Worker
Friend Event Respond(msg as string)
Private m_i as integer
Public sub doWork(i as integer)
me.m_i = i
dim th as new thread(AdressOf mySpecialWork)
th.start
end sub

Private Sub mySpecialWork
'does something with me.m_i
.......
'
raiseevent Respond(i & " and what i have made with it")
end sub
End Class

Instead of firing the event from within the mySpecialWork metjod i have
tried

Private Sub mySpecialWork
'does something with me.m_i
.......
'
raiseevent Respond(i & " and what i have made with it")
end sub

Private Sub MakeResponse(msg a string)
me.mx.WaitOne() ' where mx is a Mutex initialized in the constructor
raiseevent Respond(msg)
application.doEvents
me.mx.Realese
End Sub

this had no effect

--
-> Milosz Weckowski
www.playseven.com

mailto:[email protected]
ICQ Number: 84867613

Get the enhanced Progressbar and a fine Colorpicker for the Compact Framwork
for free:
http://www.playseven.com/11620/p7_Controls.html
 
M

Milosz - [playseven.com]

of cause the Worker calss is declared withevents
and the change is to call the method MakeResponse
 
R

Ryan Chapman [MSFT]

I Milosz,

I'm not sure I fully understand what the classes are doing, but it seems
this code would fire the Respond event Count times with the value of Count
each time. Is the problem that the event is not being fired the correct
number of times, or that the result is wrong? If it is the latter, the
reason is that m_i is a single variable whose scope is class-wide. Since
you only have one copy of Worker, you'll only have one copy of m_i that
mySpecialWork is working on.

Hope this helps.

Ryan

This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top