Why does debugging affect thread behaviour

robertb_NZ

Well-known member
Joined
May 11, 2010
Messages
146
Location
Auckland, New Zealand
Programming Experience
10+
Summary
I have a class that handles FTP. When I run this synchronously, or as a thread but with a debugging break point, it works perfectly. However when I run this as a thread without a break point it appears to run twice, resulting in error messages from FTP.

Detail

Here is my code, with a direct call to JobSub and the threading call to DoJobSub commented out. This runs normally. As JazzFTP.JobSub runs it executes various Debug.Print statements: these show normal execution.
    Sub SubmitJob(Progname As String, SaveCOBOL As Boolean, SaveJCL As Boolean, SaveOLDJobs As Boolean)
        '   Called back from Process.
        '   Jobstream has been created in JCLFile. Now submit it to zOS
        Dim DoJobSub As New Threading.Thread(AddressOf JobSub)
        Dim JobSubArgs As New JobSubArgs(Progname, SaveCOBOL, SaveJCL, SaveOLDJobs)  ' Can only pass one parameter, so multiple parms put into a class
        JobSub(JobSubArgs)         '   Debugging - run synchronously
        'DoJobSub.Start(JobSubArgs)  '   Normal - run asynchronously
    End Sub

    Sub JobSub(JobSubArgs As JobSubArgs)
        '   Invoke JazzFTP.JobSub asynchronously.  
        Dim JazzFTP As New JazzFTP
        JazzFTP.Initialize(AddressOf Message, AddressOf IsFinished)
        Dim Result = JazzFTP.JobSub(JobSubArgs)
    End Sub

As JazzFTP.JpbSub runs the debug statements produce this.
Connecting to 192.86.32.59
Connected to 192.86.32.59. Logging on
Connected and Authenticated
Uploading CRDta1 to @Project.@Group.SRCLIB
Transfer completed successfully.
SITE command was accepted
Job JOB00914 Uploaded
Get Job:JOB00914
Job JOB00914 Downloaded Successfully. Click [Results] to see output
This is exactly what it should be. The job is correctly submitted, everything is fine, except of course for the fact that one has to wait for the FTP to finish before the UI responds.

However if I change this to execute JobSub with threading: -
        'JobSub(JobSubArgs)         '   Debugging - run synchronously
        DoJobSub.Start(JobSubArgs)  '   Normal - run asynchronously

I get these results from the Debug.Print statements: -
Connecting to 192.86.32.59
Connecting to 192.86.32.59
Connected to 192.86.32.59. Logging on
Connected to 192.86.32.59. Logging on
Connected and Authenticated
Uploading CRDta1 to @Project.@Group.SRCLIB
Connected and Authenticated
Uploading CRDta1 to @Project.@Group.SRCLIB
A first chance exception of type 'Limilabs.FTP.Client.FtpResponseException' occurred in Ftp.dll
IBMUSER.MANAJAZZ.SRCLIB(CRDTA1) used exclusively by someone else.
The thread '<No Name>' (0x524) has exited with code 0 (0x0).
Transfer completed successfully.
SITE command was accepted
Job JOB00913 Uploaded
Get Job:JOB00913
Job JOB00913 Downloaded Successfully. Click [Results] to see output

It appears as if JobSub is being invoked twice. One of these threads fails, but apart from the unexpected message "IBMUSER.MANAJAZZ.SRCLIB(CRDTA1) used exclusively by someone else." nothing appears wrong, as the other thread finishes normally and the FTP therefore seems to work.

Up to now I'd been treating this as a problem of an error message that shouldn't have been produced, but it's clear that the problem is the duplicate thread. To investigate this I put a check point on the line
Dim Result = JazzFTP.JobSub(JobSubArgs)
in JobSub. Now everything worked correctly again: the checkpoint was only triggered once, and the Debug.Print messages showed a single set.

Is there a fault with debugging? Can I ignore this problem, as it will work correctly when run as a compiled VB program, not with the debugger? Or is there something that I'm doing wrong and should change?

Thank you,
Robert Barnes
 

robertb_NZ

Well-known member
Joined
May 11, 2010
Messages
146
Location
Auckland, New Zealand
Programming Experience
10+
Problem Solved - sort of

It seems clear that there is a problem in threading, with DoJobSub.Start initiating two threads. I had the same problem in another situation where I was getting a folder list and putting it into a listbox: the list was doubled up. I solved the problem with the following work-around code: -

First, at the top of the class (general definitions) I defined a flag: -
Dim JobSubInvoked As Boolean = False​

Then I wrapped the code in JobSub with: -
1. Test if this flag is true. Exit immediately if so
2. Set it True
3. At end JobSub, set it back to False​

Here's my code: -

Sub JobSub(JobSubArgs As JobSubArgs)
If JobSubInvoked Then
Exit Sub​
End If
JobSubInvoked = True
' Invoke JazzFTP.JobSub asynchronously.
Dim JazzFTP As New JazzFTP(AddressOf Message, AddressOf IsFinished)
Dim Result = JazzFTP.JobSub(JobSubArgs)
JobSubInvoked = False​
End Sub

It's a bit of a kludge, but it solves the problem.

BTW, I am using VS2010 and ASP.NET 4.0
 
Top Bottom