Here is a part of the code. I included only main parts which deal with
this issue. I hope it will give you a picture what I'm trying to do.
The part which takes long time is "m_Tcl.brExecute(oMs,oMs);"
Question for Willy:
You mentioned to run the thread in an STA. Could you please send me
some sample of this?
Thanks
Paul
namespace Pick_Query
{
#region Public Delegates
// delegates used to call MainForm functions from worker thread
public delegate void DelegateAddString(String s);
#endregion
/// <summary>
/// Summary description for MainForm.
/// </summary>
public class MainForm : System.Windows.Forms.Form
{
public MainForm()
{
InitializeComponent();
// initialize delegates
m_DelegateAddString = new DelegateAddString(this.AddLineToOutput);
}
[STAThread]
static void Main()
{
Application.Run(new MainForm());
}
private void button1_Click(object sender, System.EventArgs e)
{
Work work = new Work(this);
ThreadStart threadDelegate = new ThreadStart(work.QueryTest);
Thread newThread = new Thread(threadDelegate);
newThread.Name = "test thread...";
newThread.Start();
}
}
public class Work
{
MainForm mainForm;
public Work(MainForm mform)
{
mainForm = mform;
}
public void QueryTest()
{
string msg;
object oMs = System.Reflection.Missing.Value;
D3CLODBC.clsD3Environment m_Env = null;
D3CLODBC.clsD3Connection m_Con = null;
D3CLODBC.clsD3TclCommand m_Tcl;
//initilize the connection
m_Env = new D3CLODBC.clsD3EnvironmentClass();
m_Con = m_Env.brOpenConnection("ODBC","members_test");
msg = "Connected...";
//send message to the MainForm
mainForm.Invoke(mainForm.m_DelegateAddString,new object[]{msg});
//command to be executed on the D3 server
string cmd = "pq_sort_tab solicitor a1 a2 a3 a4 a5";
m_Tcl = m_Con.brOpenTclCommand(cmd);
// this is the part which takes long time
m_Tcl.brExecute(oMs,oMs); //executing the command
//returning string of stuff
msg = m_Tcl.brCapturing.get_brCString();
mainForm.Invoke(mainForm.m_DelegateAddString,new object[]{msg});
}
}
}
"Willy Denoyette [MVP]" <wi*************@pandora.be> wrote in message news:<OW*************@TK2MSFTNGP12.phx.gbl>...
Make sure your separate thread is initialized to run in an STA, if you
don't, your COM object will still run on the UI (STA)thread.
Also note that you shouldn't directly update the UI elements from this
thread, you have to call Control.Invoke or Control.BeginInvoke.
Willy.