locked
How to simulate the asynchronous scene of GetDefaultPrinter? RRS feed

  • Question

  • reference: 

    https://docs.microsoft.com/en-us/windows/win32/printdocs/getdefaultprinter

    This is a blocking or synchronous function and might not return immediately. How quickly this function returns depends on run-time factors such as network status, print server configuration, and printer driver implementation factors that are difficult to predict when writing an application. Calling this function from a thread that manages interaction with the user interface could make the application appear to be unresponsive.

    so how should i simulate the asynchronous scene of GetDefaultPrinter?

    i try many times in my pc , it always return immediately!

    Tuesday, May 12, 2020 1:42 AM

All replies

  • Hi,

    Thanks for posting here,

    According to the Print Spooler API Functions list, there seems to be no such api to achieve asynchronous operations on GetDefaultPrinter.

    However, you could use threads: CreateEvent, then call GetDefaultPrinter in the thread, and if GetDefaultPrinter returns, call SetEvent to set the specified event object to the signaled state. The main thread could get the complete event.

    Best Regards,

    Drake


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, May 12, 2020 3:29 AM
  • Thanks for your reply!

    Maybe I am not expressing clearly enough.

    getdefaultprinter:

    This is a blocking or synchronous function and might not return immediately.

    I just want to know which type of default printer to get,  it cannot be returned immediately!

    because it always return immediately in my pc, when i use the getdefaultprinter api ;

    Tuesday, May 12, 2020 5:56 AM
  • Such as a remote printer. As we can see in the printer api documents like, SetDefaultPrinter, EnumPrinters, the default printer could be a remote printer, the return speed of GetDefaultPrinter depends on the network status, If the network is currently congested, there may be a blocking according to the documentation.

    Best Regards,

    Drake


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, May 12, 2020 7:42 AM
  • Thanks a lot!

    Do you means the bellow flow ?

    1. On the local PC, Open Remote Desktop Connection(RDC)

    2. Enter the address PC1 you want to connect to

    3. On the PC1, the PC'printers will show the PC1. set the PC1'printer as default

    4. call the win32 api: GetDefaultPrinter

    by the flow, it still does not reproduce the block , and returns  immediately.

    so what should i do ?

     

    Tuesday, May 12, 2020 9:16 AM
  • This cannot be easily reproduced, use SetDefaultPrinter, set your default printer to a remote printer:

    SetDefaultPrinter ("\\\\server\\printername");

    When your network is stuck, check if GetDefaultPrinter() will block.

    In addition, SetDefaultPrinter will also not return immediately, if the network printer does not exist(take the time to try to connect and then fail) or the network is stuck.

    Best Regards,

    Drake


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, May 13, 2020 9:54 AM