Using ThreadPool in Visual COBOL

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.

Tags:

Parents
  • Verified Answer

    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.
    
Reply
  • Verified Answer

    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.
    
Children
No Data