Highlighted
Super Contributor.
Super Contributor.
1381 views

Bom dia, antes eu utilizava o Cobol isCobol e lá temos a instrução "perform thread" essa instrução faz com que desvie o fluxo do programa para uma rotina e fique executando outra ao mesmo tempo como fazer com Visual Cobol, obrigado.

Good day, before I used the Cobol isCOBOL and there we have the statement " perform thread" this statement causes divert the program flow to a routine and stay running another at the same time how to make Visual Cobol , thank you.

0 Likes
1 Solution

Accepted Solutions
Highlighted
Micro Focus Expert
Micro Focus Expert

RE: Thread

Jump to solution

This was just a simple example of one way that you can use the .NET Threading syntax which happened to use a static method. If you need to use an instance method with threading you can do this as follows:

      $set ilusing"System"
      $set ilusing"System.Threading"
       program-id. Program1 as "testthreading.Program1".

       data division.
       working-storage section.
       01 any-key pic x.
       procedure division.
           declare thread1 as type Thread
           declare myobject as type ThreadWork = new ThreadWork
           declare threadDelegate as type ThreadStart = new ThreadStart(myobject::DoWork)
           set thread1 to new Thread(threadDelegate)
           invoke thread1::Start
           perform varying i as binary-long from 0 by 1 until i > 3
              display "In Main"
              invoke type Thread::Sleep(100)
           end-perform
           accept any-key
           goback.
           
       end program Program1.
       class-id ThreadWork.
       working-storage section.
       
       method-id DoWork public.
       procedure division.
           perform varying i as binary-long from 0 by 1 until i > 3
              display "Working Thread..."
              invoke type Thread::Sleep(100)
           end-perform
          
           goback.
       end method.
       
       end class.
      

If you do wish to use a static method that does file handling then the file itself should be defined as static or you will get errors.

Example:


       class-id ThreadWork.
          select optional test-file assign to "testfile.dat"
                           organization is line sequential
                           file status is file-status.
       file section.
       fd test-file static.
       01 test-record  pic x(20).
       working-storage section.
       01 file-status pic x(2) static.
       method-id DoWork static.
       procedure division.
           open extend test-file
           display "open = " file-status
           perform varying i as binary-long from 0 by 1 until i > 3
              display "Working Thread..."
              move i to test-record
              write test-record
              invoke type Thread::Sleep(100)
           end-perform
           close test-file
           goback.
       end method.
       
       end class.

View solution in original post

0 Likes
4 Replies
Highlighted
Micro Focus Expert
Micro Focus Expert

RE: Thread

Jump to solution

The perform thread syntax is not supported in Visual COBOL.

Multi-threading can be done using a variety of methods. There is an entire  section in the documentation devoted to this topic. It can be found here:

In addition to the threading syntax and callable library routines available, in managed code you can also use the Threading class to start up additional threads.

Example:

      $set ilusing"System"
      $set ilusing"System.Threading"
       program-id. Program1 as "testthreading.Program1".

       data division.
       working-storage section.

       procedure division.
           declare thread1 as type Thread
           declare threadDelegate as type ThreadStart = new ThreadStart(type ThreadWork::DoWork)
           set thread1 to new Thread(threadDelegate)
           invoke thread1::Start
           perform varying i as binary-long from 0 by 1 until i > 3
              display "In Main"
              invoke type Thread::Sleep(100)
           end-perform
           goback.
           
       end program Program1.
       class-id ThreadWork.
       working-storage section.
       
       method-id DoWork static.
       procedure division.
           perform varying i as binary-long from 0 by 1 until i > 3
              display "Working Thread..."
              invoke type Thread::Sleep(100)
           end-perform
          
           goback.
       end method.
       
       end class.
      
0 Likes
Highlighted
Super Contributor.
Super Contributor.

RE: Thread

Jump to solution

Fiz os testes, e devo ter feito alguma coisa errada. Eu não consigo fazer leitura arquivos em um method static?

I did the tests , and I must have done something wrong . I can not do reading files in a static method ? Thank you for your help.

0 Likes
Highlighted
Micro Focus Expert
Micro Focus Expert

RE: Thread

Jump to solution

This was just a simple example of one way that you can use the .NET Threading syntax which happened to use a static method. If you need to use an instance method with threading you can do this as follows:

      $set ilusing"System"
      $set ilusing"System.Threading"
       program-id. Program1 as "testthreading.Program1".

       data division.
       working-storage section.
       01 any-key pic x.
       procedure division.
           declare thread1 as type Thread
           declare myobject as type ThreadWork = new ThreadWork
           declare threadDelegate as type ThreadStart = new ThreadStart(myobject::DoWork)
           set thread1 to new Thread(threadDelegate)
           invoke thread1::Start
           perform varying i as binary-long from 0 by 1 until i > 3
              display "In Main"
              invoke type Thread::Sleep(100)
           end-perform
           accept any-key
           goback.
           
       end program Program1.
       class-id ThreadWork.
       working-storage section.
       
       method-id DoWork public.
       procedure division.
           perform varying i as binary-long from 0 by 1 until i > 3
              display "Working Thread..."
              invoke type Thread::Sleep(100)
           end-perform
          
           goback.
       end method.
       
       end class.
      

If you do wish to use a static method that does file handling then the file itself should be defined as static or you will get errors.

Example:


       class-id ThreadWork.
          select optional test-file assign to "testfile.dat"
                           organization is line sequential
                           file status is file-status.
       file section.
       fd test-file static.
       01 test-record  pic x(20).
       working-storage section.
       01 file-status pic x(2) static.
       method-id DoWork static.
       procedure division.
           open extend test-file
           display "open = " file-status
           perform varying i as binary-long from 0 by 1 until i > 3
              display "Working Thread..."
              move i to test-record
              write test-record
              invoke type Thread::Sleep(100)
           end-perform
           close test-file
           goback.
       end method.
       
       end class.

View solution in original post

0 Likes
Highlighted
Super Contributor.
Super Contributor.

RE: Thread

Jump to solution

Muito obrigado Chris, funcionou perfeitamente.

0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.