-1

I have written a Side-By-Side COM DLL in C#. I am using this DLL in VB6.

The COM DLL contains a thread. As soon as I start this thread, the calling application (in my case VB6) blocks (meaning I can not do anything in it anymore).

I am starting the thread like this:

    private Thread _startMasterThread;

    public void Init()
    {
        if (_startMasterThread == null)
        {
            _startMasterThread = new Thread(new ThreadStart(pMasterThread));
            _startMasterThread.Priority = ThreadPriority.Highest;
            _startMasterThread.Start();
        }
    }

    private void pMasterThread()
    {
        while (!_bAbortAll)
        {
            //do something
        }
    }

ThreadStart comes from this:

namespace System.Threading
{
    [ComVisible(true)]
    public delegate void ThreadStart();
}

Is it normal that the calling application becomes unresponsive? I thought that since it is a different thread, it would not do this.

tmighty
  • 10,734
  • 21
  • 104
  • 218
  • 1
    **1.** Not sure whether this will help, but since you're dealing with COM here, it might be a good idea to explicitly set the apartment state for the created thread (before you start it) via [`_startMasterThread.SetApartmentState`](https://msdn.microsoft.com/en-us/library/system.threading.thread.setapartmentstate.aspx "MSDN reference page"). **2.** (Off-topic:) A DLL cannot "contain" a thread; code in the DLL may *create* and/or *start* a thread. – stakx - no longer contributing Mar 10 '15 at 08:08

1 Answers1

0

It looks like you create a STA thread and then neglect to serve the message pump. Callers will block because nobody responds to their messages. Hence, the UI (a caller) freezes (block).

Some old, but highly relevant articles:

After you read these, and understand the issue, the solution(s) will be obvious. Switch to an MTA or run a message pump in your thread.

Remus Rusanu
  • 288,378
  • 40
  • 442
  • 569
  • I have tried _startMasterThread.SetApartmentState(ApartmentState.STA); but that did not change anything. Do you mean that I should change the DLL to be MTA? I did not see where I could set this setting. – tmighty Mar 10 '15 at 20:29