Code Monkey home page Code Monkey logo

Comments (13)

dahall avatar dahall commented on August 28, 2024

ThreadPool.BindToHandle only allows a handle to be bound once per thread. It appears you're trying to reuse the handle held in device in your code. So my question is: Should the library work around the ThreadPool code or are you using DeviceIoControl in a way that is not consistent with its pattern by calling it twice? I'm not familiar enough with that function to answer the question.

from vanara.

GF-Huang avatar GF-Huang commented on August 28, 2024

Reuse device is common scenes, for example: open a virtual network adapter, and then keep reading and writing in a while loop for redirect or analysis network traffic. If open and close device frequently, it will reduce performance.

Even the example is Read and Write, but this depends on the driver implementation, if it implement by DeviceIoControl ?

from vanara.

dahall avatar dahall commented on August 28, 2024

This one also is ready for testing

from vanara.

GF-Huang avatar GF-Huang commented on August 28, 2024

Capturing exceptions can cause some performance degradation.
So, I don't think so BindHandle each call is a good way.


Please see my test result:

This is the last code without my fix, you can see I call DeviceIoControlAsync 100 times, and print it cost time in Milliseconds:

image

the result:

image


If I do some fix, only BindHandle at first time, and the test code do not change as above:

image

the result:

image

from vanara.

GF-Huang avatar GF-Huang commented on August 28, 2024

I came up with three compromises.

  1. Wrap CreateFile with bool isAsync argument, BindHandle in side CreateFile if isAsync == true. Or check if FileFlagsAndAttributes.FILE_FLAG_OVERLAPPED had set.

  2. Exposing bool bindHandle argument for all the paths of DeviceIoControlAsync, but there's too much codes to modify, and it's not very elegant.

  3. Remove BindHandle that is inside SetupOverlappedFunction and add checking if the device had bind. Throw an exception tells user need to bind handle manually if not bind. (I don't know if this is feasible)

from vanara.

dahall avatar dahall commented on August 28, 2024

I had thought about 1 and 3. The problem is that the ThreadPool uses the handle to associate threads so it is not copying handles across thread boundaries. Removing it reduces the threading performance. I can collect handles and check them before calling BindHandle. With that, I only run the risk of holding the handle value until the process terminates. Maybe ThreadPool is already doing this so there is no harm.

from vanara.

GF-Huang avatar GF-Huang commented on August 28, 2024

Do you means save handles in a static HashSet or Dictionary, and check is it already exist before bind ?

from vanara.

dahall avatar dahall commented on August 28, 2024

Exactly

from vanara.

GF-Huang avatar GF-Huang commented on August 28, 2024

It seems feasible.

from vanara.

dahall avatar dahall commented on August 28, 2024

I just pushed updated code. Let me know if the bug is gone and performance is back.

from vanara.

GF-Huang avatar GF-Huang commented on August 28, 2024

Works well now.

image

from vanara.

GF-Huang avatar GF-Huang commented on August 28, 2024

By the way, are you planning to release version 2.3.0 of the nuget package these days ?

from vanara.

dahall avatar dahall commented on August 28, 2024

Yes. In the next 2 days.

from vanara.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.