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.brExecut e(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 DelegateAddStri ng(String s);
#endregion
/// <summary>
/// Summary description for MainForm.
/// </summary>
public class MainForm : System.Windows. Forms.Form
{
public MainForm()
{
InitializeCompo nent();
// initialize delegates
m_DelegateAddSt ring = new DelegateAddStri ng(this.AddLine ToOutput);
}
[STAThread]
static void Main()
{
Application.Run (new MainForm());
}
private void button1_Click(o bject sender, System.EventArg s e)
{
Work work = new Work(this);
ThreadStart threadDelegate = new ThreadStart(wor k.QueryTest);
Thread newThread = new Thread(threadDe legate);
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.Reflecti on.Missing.Valu e;
D3CLODBC.clsD3E nvironment m_Env = null;
D3CLODBC.clsD3C onnection m_Con = null;
D3CLODBC.clsD3T clCommand m_Tcl;
//initilize the connection
m_Env = new D3CLODBC.clsD3E nvironmentClass ();
m_Con = m_Env.brOpenCon nection("ODBC", "members_test") ;
msg = "Connected. ..";
//send message to the MainForm
mainForm.Invoke (mainForm.m_Del egateAddString, new object[]{msg});
//command to be executed on the D3 server
string cmd = "pq_sort_ta b solicitor a1 a2 a3 a4 a5";
m_Tcl = m_Con.brOpenTcl Command(cmd);
// this is the part which takes long time
m_Tcl.brExecute (oMs,oMs); //executing the command
//returning string of stuff
msg = m_Tcl.brCapturi ng.get_brCStrin g();
mainForm.Invoke (mainForm.m_Del egateAddString, new object[]{msg});
}
}
}
"Willy Denoyette [MVP]" <wi************ *@pandora.be> wrote in message news:<OW******* ******@TK2MSFTN GP12.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.BeginIn voke.
Willy.