I have a MFC C++ Activex control and I am calling an Activex class function from a background thread DoWork()
function. I found that it blocks the main thread. On the other hand, if I replace call to c++ COM function(which takes 5 seconds to execute) by Thread.Sleep(5000)
in C#, it works in background thread.
I have tried many things like creating the instance of an activex class in a worker thread also. But it seems that the COM function always executes on the main thread. I have also attached the sample project.
/* Here's the C# code */
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void exit_Click(object sender, EventArgs e)
{
this.Close();
}
private void cpp_Click(object sender, EventArgs e)
{
this.progressBar1.Style = ProgressBarStyle.Marquee;
this.backgroundWorker1.RunWorkerAsync(1);
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
if (Convert.ToInt16(e.Argument) == 1)
{
// C++ function that takes 5 seconds to execute.
this.axActivexComponent1.AboutBox();
}
else
{
/* Normal C# code, for this it works fine */
System.Threading.Thread.Sleep(5000);
}
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
this.progressBar1.Style = ProgressBarStyle.Blocks;
MessageBox.Show("Thread Completed!!!");
}
private void csharp_Click(object sender, EventArgs e)
{
this.progressBar1.Style = ProgressBarStyle.Marquee;
this.backgroundWorker1.RunWorkerAsync(2);
}
}