我有一個函數可以從DocuShare服務器下載郵件消息作爲MSG文件。當從主線程調用該函數時,該函數完美工作。但是,當我在一個單獨的線程中調用該函數時,下載失敗。當我介入代碼時,我可以看到該函數正在被調用,所有參數都被正確計算,返回值就是我所期望的。不幸的是,我看到,沒有文件被下載。C#2.0函數在單獨線程中調用時不起作用
代碼:
private void btnDownloadMails_Click(object sender, EventArgs e)
{
//Thread t = new Thread(new ThreadStart(DownloadMailAsMsg));
//t.Start(); //Does not work
DownloadMailAsMsg(); // Works fine
}
void DownloadMailAsMsg()
{
DSServerMap.Server dsserver = new DSServerMap.Server();
if (!SelectMappedServer(ref dsserver, textServer.Text.ToString()))
return;
long status = 0;
dsserver.DocuShareAddress = textServer.Text;
dsserver.UserName = textUser.Text;
dsserver.Password = textPwd.Text;
status = dsserver.Logon();
if (status == 0)
{
IItemObj objParentItem;
string[] emailHan = { "MailMessage-12", "MailMessage-13", "MailMessage-31" };
foreach (string handnum in emailHan)
{
objParentItem = (IItemObj)dsserver.CreateObject(handnum);
DSGATEWAYLib.IGatewayHandler gateway = (DSGATEWAYLib.IGatewayHandler)dsserver.Open();
objParentItem.AttachGateway(gateway, true);
objParentItem.Name = @"D:\em\m_" + handnum + ".msg";
int flag = objParentItem.DSDownload(0);
}
}
}
任何想法?
感謝 普拉卡什
但是,我懷疑這是否是主要原因:您甚至不應該在非UI線程上討論所有那些'.Text'屬性。理想情況下,您應該先獲得並將其傳遞給工作人員。 – 2010-10-13 10:46:56