CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Oct 1999
    Location
    Belgium
    Posts
    440

    Question QueueUserWorkItem

    It appears I can also call QueueUserWorkItem without a WaitCallBack constructor in the parameter list ...
    Code:
    static void Main(string[] args)
    {
        ThreadPool.QueueUserWorkItem(ThreadMethod);
        Console.ReadKey();
    }
    
    static void ThreadMethod(Object objInfo)
    {
        Console.WriteLine("This is the threaded method.");
    }
    MSDN however states this is only allowed in VB ... http://msdn.microsoft.com/en-us/libr....90).aspx#Y108

    Can anyone tell me which is preferred in C# ?

  2. #2
    Join Date
    May 2007
    Posts
    1,546

    Re: QueueUserWorkItem

    C# can automatically infer the type of the required delegate so everything works automatically. What you've written is the preferred way.
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  3. #3
    Join Date
    Jan 2010
    Posts
    1,133

    Re: QueueUserWorkItem

    @zvenny: As for the MSDN article, it says:
    "Visual Basic users can omit the WaitCallback constructor, and simply use the AddressOf operator when passing the callback method to QueueUserWorkItem. Visual Basic automatically calls the correct delegate constructor."

    Although after reading this one can infer exactly what you've concluded, this statement doesn't actually say anything about other languages; I think it's just a note directed specifically at VB.NET developers to inform them about an option they may not be aware of.

  4. #4
    Join Date
    Oct 1999
    Location
    Belgium
    Posts
    440

    Re: QueueUserWorkItem

    Until yesterday, I did not know about delegate inference in C#
    The comment in the MSDN article indeed made me think this was a VB feature.

    Thx all

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured