Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6152449
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:55:20+00:00 2026-05-23T19:55:20+00:00

There is an external library which has a function that returns a value with

  • 0

There is an external library which has a function that returns a value with a certain delay (sleep for 200-1000ms), C++ code:

__declspec( dllexport ) 
char ReadNextChar()
{
    char OneChar[2];
    while (Receive((char*)&OneChar)==-1) {Sleep(200);}

    return OneChar[0];
}

In the main program running thread,
C# code:

public void Start()
    {
        thread = new Thread(ReadChar);
        thread.IsBackground = true;
        thread.Priority = ThreadPriority.BelowNormal;

        thread.Start()
    }

    char currentChar;

    [DllImport("reader.dll")]
    public extern char ReadNextChar();       

    private void ReadChar()
    {
        while(true) {
            currentChar=ReadNextChar();
        }            
    }

It seems to me that this can be seriously optimised. What is the best way to do this task? (how does it really work while external function sleep?). Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T19:55:21+00:00Added an answer on May 23, 2026 at 7:55 pm

    The typical way to do this on win32 would be to use an event and signal this event from the ReadNextChar function.
    This is how the Win32 ReadFile function works when using overlapped mode (http://msdn.microsoft.com/en-us/library/aa365467(v=vs.85).aspx)

    You can then use WaitForSingleObject to put the thread to sleep. It will become runnable again when the event is signalled.

    This would make your implementation more complicated and would additionally require modifications to the dll. Now, it is only really worth doing this if the read you are doing can actually be waited on – otherwise you will end up having to add another thread in your dll that polls and sets the event (not much point in doing this if you are doing this for optimization purposes as it will be a pessimistic optimization, to say the least).

    To be honest, since you are likely to be sleeping for at least 200ms, polling your read function is unlikely to cause any significant performance issues – don’t optimize it for the sake of it.

    Clearly something that your code does not do is to make the thread exit-able without terminate()ing it. I would personally put the sleep in the c# code and return a read success code from the ReadNextChar function without spinning in a loop or having any sleeps in there. I expect your code is just an example though..

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In the external code that I am using there is enum: enum En {VALUE_A,
I have an external library which has a method which performs a long running
Is there a way to Minimize an external application that I don't have control
Under VS's external tools settings there is a Use Output Window check box that
I have a class from an external library that I want to extend with
I have a DLL which has certain functionality which requires UAC elevated privileges in
Is there any easy way / library / external app to introduce Unit of
I'm developing a common library (external jar) that is being provided to different developers
The Linq to Xml library in .Net has the extension method XPathEvaluate which allows
I am analyzing an external FORTRAN library which is quite huge. Anyways, the whole

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.