【发布时间】:2015-03-10 12:33:42
【问题描述】:
我有一个 Windows 窗体应用程序和一个调用函数的按钮
我正在将大文件从一个地方复制到另一个地方。
这需要很长时间,所以我决定使用进度条。 我需要通过单击按钮调用后台进程
copyItems() 函数遍历列表项并从另一个位置复制这些项。它又调用一个函数CopyListItem 复制一个项目。
我在 UI 中设置了一个文本框值,但它返回一个
发生异常“Microsoft.VisualStudio.Debugger.Runtime.CrossThreadMessagingException”`
如何在backgroundworker todo事件中调用复制功能
当我在 click 事件中调用 runworkerasync 方法时出现错误
private void btnCopyItems_Click(object sender, EventArgs e)
{
backgroundWorker1.RunWorkerAsync();
}
我创建了一个类
public partial class WorkerItem
{
Helper Helper = new Helper();
Complaints comp = new Complaints();
public void CopyItem(SPListItem sourceItem, string destinationListName, string destServerURL)
{
//Copy sourceItem to destinationList
try
{
// copies file from one location to another
}
catch (Exception ex)
{
Helper.LogtoList("Copy List ", string.Format(" {0} Message {1} Source {2} Stack trace {3}", ex.InnerException.ToString(), "Message " + ex.Message + "Source" + ex.Source + "Stack trace" + ex.StackTrace));
}
}
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
this.Text=e.ProgressPercentage.ToString();
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
try
{
WorkerItem workerItem = (WorkerItem)e.Argument;
// check if the site valid
Helper.siteName = txtSite.Text;
{
progressBar1.Maximum = itemscoll.Count;
foreach (SPListItem sourceItem in itemscoll)
{
date = sourceItem["Date_x0020_Created"].ToString();
if (!string.IsNullOrEmpty(date))
{
workerItem.CopyItem(sourceItem, Helper.destinationListName, Helper.destServerURL);
}
}
}
Cursor.Current = Cursors.Default;
MessageBox.Show(string.Format("Items Copied {0}", Helper.ItemsCopied.ToString()));
}
catch (Exception ex)
{
Helper.LogtoList("Main Function ", string.Format("{0} Message {1} Source {2} Stack trace {3}", ex.InnerException.ToString(), "Message " + ex.Message + "Source" + ex.Source + "Stack trace" + ex.StackTrace));
}
}
【问题讨论】:
标签: c# .net winforms c#-4.0 c#-3.0