invalidApartmentStateChange MDA

The invalidApartmentStateChange مدارة تصحيح الأخطاء assistant (MDS) هو activated بواسطة either of الثاني problems:

  • An attempt هو made إلى تغيير the COM apartment الولاية of a مؤشر ترابط that has already been initialized بواسطة COM إلى a different apartment الولاية.

  • The COM apartment الولاية of a مؤشر ترابط التغييرات unexpectedly.

الأعراض

  • A مؤشر ترابط's COM apartment الولاية ليس what was requested. This may cause proxies إلى be used for COM مكونات that have a نموذج مؤشر الترابط different من the الحالي واحد. This في turn may cause an InvalidCastException إلى be تم طرح when calling the COM كائن through الواجهات that are not التعيين لأعلى for cross-apartment التنظيم والإرسال.

  • The COM apartment الولاية of the مؤشر ترابط هو different than expected. This can cause a COMException مع an HRESULT of RPC_E_WRONG_THREAD كـ well كـ a InvalidCastException when making calls تشغيل a وقت تشغيل التفاف للاستدعاء (RCW). This can also cause some مفرد-threaded COM مكونات إلى be accessed بواسطة multiple عمليات جزئية at the same الوقت, which can lead إلى corruption أو بيانات loss.

السبب

  • The مؤشر ترابط was previously initialized إلى a different COM apartment الولاية. ملاحظة that the apartment الولاية of a مؤشر ترابط can be التعيين either بوضوح أو ضمنيا. The explicit العمليات تضمين the Thread.ApartmentState خاصية و the SetApartmentState و TrySetApartmentState وظائف. A مؤشر ترابط تاريخ الإنشاء using the Start أسلوب هو ضمنيا التعيين إلى MTA unless SetApartmentState هو called قبل the مؤشر ترابط هو started. The main مؤشر ترابط of the تطبيق هو also ضمنيا initialized إلى MTA unless the STAThreadAttribute سمة هو specified تشغيل the main أسلوب.

  • The CoUninitialize أسلوب (أو the CoInitializeEx أسلوب) مع a different concurrency model هو called تشغيل the مؤشر ترابط.

الدقة

التعيين the apartment الولاية of the مؤشر ترابط قبل it begins executing, أو يطبق either the STAThreadAttribute سمة أو the MTAThreadAttribute سمة إلى the main أسلوب of the تطبيق.

For the ثانية cause, ideally, the تعليمات برمجية that calls the CoUninitialize أسلوب should be ‏‏تاريخ التعديل إلى تأخير the يتصل until the مؤشر ترابط هو حول إلى ينهي و there are لا RCWs و their underlying COM مكونات still في استخدم بواسطة the مؤشر ترابط. However, if it ليس possible إلى تعديل the تعليمات برمجية that calls the CoUninitialize أسلوب, then لا RCWs should be used من عمليات جزئية that are uninitialized في this way.

التأثير تشغيل وقت التشغيل

MDA هذا أي تأثير تشغيل CLR.

الإخراج

The COM apartment الولاية of the الحالي مؤشر ترابط, و the الولاية that the تعليمات برمجية was attempting إلى يطبق.

التكوين

<mdaConfig>
  <assistants>
    <invalidApartmentStateChange />
  </assistants>
</mdaConfig>

مثال

The following تعليمات برمجية مثال demonstrates a situation that can تنشيط this MDA.

using System.Threading;
namespace ApartmentStateMDA
{
    class Program
    {
        static void Main(string[] args)
        {
            Thread.CurrentThread.SetApartmentState(ApartmentState.STA);
        }
    }
}

راجع أيضًا:

المرجع

MarshalAsAttribute

المبادئ

تشخيص الأخطاء مع المساعدين التصحيح مدارة

تنظيم إمكانية التشغيل المتداخل