Threading problem.

T

trialproduct2004

Hi all
I am having problem at a time of handling threading.
I am having application containing thread.
In thread procedure i ma using recursive function.
This recursive function is adding some entries in my temprary file.
But when i called abort on thread it is not aborting that thread.
After calling aborting still entries are getting inserted into
temporary files.
That means still recursive function is getting executed.
Can some one tell me why this is happening even after calling abort on
thread.
Is there any alternative for aborting thread.
Please help me as this is very very important for me.

Thanks in advance.
 
C

Crouchie1998

Of course we can help, but we need to see some code to see where you're
going wrong

Crouchie1998
BA (HONS) MCP MCSE
 
A

Armin Zingler

Hi all
I am having problem at a time of handling threading.
I am having application containing thread.
In thread procedure i ma using recursive function.
This recursive function is adding some entries in my temprary file.
But when i called abort on thread it is not aborting that thread.
After calling aborting still entries are getting inserted into
temporary files.
That means still recursive function is getting executed.
Can some one tell me why this is happening even after calling abort on
thread.
Is there any alternative for aborting thread.
Please help me as this is very very important for me.

Thanks in advance.

Don't call abort. Use your own abort mechanism. I prefer passing an object
containing a Cancel flag that can be set by a client thread and checked by
the executing thread. Example:

class CancelClass
cancel as boolean
end class

Pass an object (called 'o' here) of this type to the thread. The thread
checks o.cancel from time to time (at a good place in a loop) to see if it
is to be canceled. In the main/other thread, you can set o.cancel=true
instead of calling abort.

Armin
 
T

trialproduct2004

hello
thanks for your reply.
i am sending general structure of my classes and function as below:-
if u get some idea please help me.
I have form on which i have two buttons.



On one button i have code like:-
objs = new scanning
objs.Main()

on another button i have code like
objs.threadsynch.abort
which is not aborting synch thread.


class Scanning
Public Sub Main()
dim objcls as new scan
threadsynch=new thread(addressof objcls.startscan
threadsynch.start()
end sub
end class



class Scan
public function startscan()
' contains someactual code for scanning
'''some extra code
scanWithSubDirs
''''some extra code
end function

Public Function scanWithSubDirs()

' actual scanning is donw here which contains one recursive function

travresebothDir()

end function


Private Function travresebothDir()szLocalPath As String, ByVal
bDirExists As Boolean)
''is recursive function which is traversing two local directores and
writing simiarler filename in file


Dim dirInfo As System.IO.DirectoryInfo

If szLocalPath.EndsWith(":") Then
dirInfo = New System.IO.DirectoryInfo(szLocalPath & "\")
Else
dirInfo = New System.IO.DirectoryInfo(szLocalPath)
End If
Dim fInfo As System.IO.FileInfo
Dim eachDirInfo As System.IO.DirectoryInfo


For Each eachDirInfo In dirInfo.GetDirectories("*")
oftp.FTPChangeDirectory(eachDirInfo.Name)
objDir = objFTP.GetDirectoryListing()
travresebothDir(eachDirInfo.FullName, True)
writeSynchrItem("temp.txt", szAddDir)
travresebothDir(eachDirInfo.FullName, False)
next

end function
end class
 
T

trialproduct2004

Hi
thanks for your reply.
But can you tell me reason behing not using abort method.
as calling abort is throwing exception where i can release all
resources.
Because problem is i have used abort at many places and not chaning it
to flag is becoming difficule for me.
So can u explaing me reason behind not using abort method.

thansk
 
A

Armin Zingler

Hi
thanks for your reply.
But can you tell me reason behing not using abort method.
as calling abort is throwing exception where i can release all
resources.
Because problem is i have used abort at many places and not chaning it
to flag is becoming difficule for me.
So can u explaing me reason behind not using abort method.

thansk

Why crash the car instead of braking and stopping at the right (or left)
side? Abort is the hard way to stop a thread. I prefer to have better
control over it, thus the suggestion to use a signal object.

To answer your question: I can not reproduce the problem. If I call the
abort method of a thread doing recursive function calls, the thread is also
aborted, i.e. the Threadabortexception is raised. In general, it shouldn't
matter whether the calls are recursive. Maybe your own code handles the
exception in a way that prevents you from stopping the thread. (How) do you
handle the exception?

Armin
 
R

Robin Tucker

Abort can also be problematic if you are making use of COM objects in your
thread, causing strange exceptions regurgitated from the innermost depths of
windows that are difficult to reproduce and impossible to "fix". Don't use
Abort, unless you mean "URGENT! STOP NOW!". I suspect what you really mean
is "Please make an effort to stop now, if you don't mind; whenever you think
it's convienient to do so.".

Threading: be gentle (use a "bStopping" flag).




Robin
 

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