Thread and COM

Source: Internet
Author: User

Scenario:

C ++ is encapsulated as COM and C # For calling. The call may be in the thread.

When the application exits, part of C # is automatically cleared, and some threads may not be completely stopped.

COM is also releasing memory.

At this time, it will crash if you are careful.

Conflict point:

1. Multiple Threads repeat ReleaseComObject,

2. C # A thread is still in use, and another thread or main thread goes to ReleaseComObject.

3. ReleaseComObject has been performed, and c # is still in use by a thread.

The most depressing thing is:

=


This Try-Catch is useless. If an exception occurs, it will still die.

 

The solution is as follows:

1. To solve the problem of repeated ReleaseComObject in multiple threads, determine the principle of "who creates Release". If not, leave it to Release when the App exits.

2. The resource has been Release and is still in use.

Check found that a thread sleep for 100 milliseconds. When the App exits, it can do a lot of things within Ms.
In addition, I don't know if there is a good way to determine whether the COM object exists or not and whether it can be called safely.

3. The resources being used, and the other thread goes to Release and follows the first principle for execution.

4. The method of loop in the thread should be as small as possible, so that the UI and quick brakes can be updated in time.

5. The thread factory should be as simple and clear as possible. The cost of complexity is the pain of writing and maintenance.

 

I tried to change a few places and found that there was still a problem. The out-of-control multithreading meets COM, which is really difficult.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.