robertb_NZ
Well-known member
I have been using this code in synchronous mode.
By swapping the commenting to
'UploadCPY(CPYLIBNAME) ' Run synchronously
DoUpload.Start(CPYLIBNAME) ' Run asynchronously
it runs asynchronously, apparently without problem.
However it is invoked twice, and it is important that
UploadCPYFile(Name1)
runs (or fails) before
UploadCPYFile(Name2)
If the prior thread is still active then I would like the 2nd thread to wait for the first to complete or fail, although I don't want the user to have to wait.
Does this asynchronous code guarantee this? I suspect not. Looking at the code I think that if UploadCPYFile(Name2) is invoked before UploadCPYFile(Name1) has finished then the 2nd thread is simply abandoned, terminating immediately when UploadInvoked is tested at the beginning of UploadCPY. I feel that, instead of using a simple global Boolean defined at the top of the class I should possibly use a stack variable, or code a wait.
Can somebody please tell me whether my suspicion is correct and so this code will not work as I want? If the code is incorrect, can you point me to an example of the sort of code that I should have?
Thank you, Robert.
Dim UploadInvoked As Boolean = False '<== global: defined at the top of the Class Sub UploadCPYFile(CPYLIBNAME As String) ' Designed for uploading SQL and Message (web service) definitions to CPYLIB. Build 13.3, jobs SQLDEF and JazzFTP ' Set up for multithreading in case I want to do this in the future, but currently used synchronously Dim DoUpload As New Threading.Thread(AddressOf UploadCPY) UploadCPY(CPYLIBNAME) ' Run synchronously 'DoUpload.Start(CPYLIBNAME) ' Run asynchronously End Sub Sub UploadCPY(CPYLIBNAME As String) If UploadInvoked Then Exit Sub End If UploadInvoked = True ' Invoke JazzFTP.Upload asynchronously. Dim JazzFTP As New JazzFTP(AddressOf Message, AddressOf IsFinished) JazzFTP.UploadCpyDef(CPYLIBNAME) UploadInvoked = False End Sub
By swapping the commenting to
'UploadCPY(CPYLIBNAME) ' Run synchronously
DoUpload.Start(CPYLIBNAME) ' Run asynchronously
it runs asynchronously, apparently without problem.
However it is invoked twice, and it is important that
UploadCPYFile(Name1)
runs (or fails) before
UploadCPYFile(Name2)
If the prior thread is still active then I would like the 2nd thread to wait for the first to complete or fail, although I don't want the user to have to wait.
Does this asynchronous code guarantee this? I suspect not. Looking at the code I think that if UploadCPYFile(Name2) is invoked before UploadCPYFile(Name1) has finished then the 2nd thread is simply abandoned, terminating immediately when UploadInvoked is tested at the beginning of UploadCPY. I feel that, instead of using a simple global Boolean defined at the top of the class I should possibly use a stack variable, or code a wait.
Can somebody please tell me whether my suspicion is correct and so this code will not work as I want? If the code is incorrect, can you point me to an example of the sort of code that I should have?
Thank you, Robert.