English 中文(简体)
银星、COM和Threads
原标题:Silverlight, COM and Threads

I´ve getting an exception "Invalid cross-thread access." I´m developing an Silverlight application. The problem is that I´m instantiating the object (COM+ object) in one thread and trying to access from another. This is an necessary behavior for my application. So I suppose that I need to say that the interop with COM is in MTAThread mode but I can´t see the point to do this I know the problem but I can´t see the solution.

Any help?? Thanks in advance

实例守则是:

using System.ComponentModel;
using System.Windows.Controls;
using System.Windows;
using System.Threading;
using System.Runtime.InteropServices.Automation;
using System;

namespace BackgroundWorkerTest
{
    public partial class MainPage : UserControl
    {
        Thread backgroundThread;
        dynamic _myspeech;
        static readonly object locker = new object();

        public MainPage()
        {
            InitializeComponent();

            backgroundThread = new Thread(StartScanProcess);
            backgroundThread.Start();
        }

        private void StartScanProcess(object sender, DoWorkEventArgs e)
        {
            StartScanProcess();
        }

        private void StartScanProcess()
        {
            lock (locker)
            {
                _myspeech = AutomationFactory.CreateObject("Sapi.SpVoice")
                _myspeech.Volume = 100;
            } 
        }

        private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            _myspeech.Speak(UIThreadId.Text); 
        }
    }
}
问题回答

did you tryed to use Dispatcher ?

something like: Dispatcher.BeginInvoke(delegate { _myspeech.Speak(UIThreadId.Text); });





相关问题
How can i add a button to all windows explorer instances?

I am trying to add a button to one of the existing tool bars in any windows explorer instance. After much research i figured out that BHO (browser helper objects) are the best way to hook to ...

Hunting memory leaks

I m finding leaked heap blocks by using the following command in WinDbg !heap –l With each leaked heap block I get, I m running to following to get the stack trace. !heap -p -a leakedheapblock The ...

Why use CComBSTR instead of just passing a WCHAR*?

I m new to COM. What exactly is the advantage of replacing: L"String" with CComBSTR(L"String") I can see a changelist in the COM part of my .NET application where all strings are replaced in this ...

IThumbnailProvider and IInitializeWithItem

I am trying to develop an IThumbnailProvider for use in Windows 7. Since this particular thumbnail would also be dependant on some other files in the same directory, I need to use something other than ...

Getting a byte array from out of process C++ COM to C#

What s the best way to get a chunk of memory (i.e. void*) from a COM server to C#? We have been using an IStream (using CreateStreamOnHGlobal) and passing that back, which worked. However when we ...

COM Basic links

folks can you provide me the tutorial link or .pdf for learning basic COM?. i do google it.. still i recommend answers of stackoverflow so please pass me.. Thanks

热门标签