Highlighted
Super Contributor.
Super Contributor.
1559 views

Using ThreadPool in Visual COBOL

Jump to solution

The MSDN C# example looks like this:

using System;
using System.Threading;
public class Example
{
    public static void Main()
    {
        // Queue the task.
        ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc));

        Console.WriteLine("Main thread does some work, then sleeps.");
        // If you comment out the Sleep, the main thread exits before
        // the thread pool task runs.  The thread pool uses background
        // threads, which do not keep the application running.  (This
        // is a simple example of a race condition.)
        Thread.Sleep(1000);

        Console.WriteLine("Main thread exits.");
    }

    // This thread procedure performs the task.
    static void ThreadProc(Object stateInfo)
    {
        // No state object was passed to QueueUserWorkItem, so  
        // stateInfo is null.
        Console.WriteLine("Hello from the thread pool.");
    }
}

I converted this to Visual COBOL:

      $set ilusing"System"
      $set ilusing"System.Threading"
       class-id CobolThreading.Example static.
       
       working-storage section.

       method-id InstanceMethod static.
       procedure division.
       
           invoke type ThreadPool::QueueUserWorkItem(new WaitCallback(self::ThreadProc))    
       
           invoke type Console::WriteLine("Main thread does some work, then sleeps.")
           invoke type Thread::Sleep(1000)
           invoke type Console::WriteLine("Main thread exits.")
           goback.
       end method.
       
       method-id ThreadProc static.
       
       procedure division using stateInfo as object.
           invoke type Console::WriteLine("Hello from the thread pool.")
           goback.
       end method.

       end class.

The COBOL gives an error on this line:
           invoke type ThreadPool::QueueUserWorkItem(new WaitCallback(self::ThreadProc))   

Intellisense says "Could not find method 'ThreadProc' with this signature".

Did I code this incorrectly ?

I can use the Thread class for multi-threading but that uses 1 MB per thread and ThreadPool would be more efficient.

Phil Levin

Tags (1)
0 Likes
1 Solution

Accepted Solutions
Highlighted
Micro Focus Expert
Micro Focus Expert

RE: Using ThreadPool in Visual COBOL

Jump to solution

COBOL parameters are "by reference" by default, so if you try using "by value" in the TraceProc it should work just fine.

   
     method-id ThreadProc static.
       
       procedure division using by value stateInfo as object.
           invoke type Console::WriteLine("Hello from the thread pool.")
           goback.
       end method.

     end class.

View solution in original post

0 Likes
2 Replies
Highlighted
Micro Focus Expert
Micro Focus Expert

RE: Using ThreadPool in Visual COBOL

Jump to solution

COBOL parameters are "by reference" by default, so if you try using "by value" in the TraceProc it should work just fine.

   
     method-id ThreadProc static.
       
       procedure division using by value stateInfo as object.
           invoke type Console::WriteLine("Hello from the thread pool.")
           goback.
       end method.

     end class.

View solution in original post

0 Likes
Highlighted
Super Contributor.
Super Contributor.

RE: Using ThreadPool in Visual COBOL

Jump to solution

Thanks.

That was easy.

Phil Levin

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.