I have a windows form application and have a button which calls a function
I am copying large files from one place to another.
It is taking a long time so I decided to use a progress bar. I need to invoke the Background process from the button click
The copyItems() function iterates through the list items and copies the items from another place. It in turn calls a function CopyListItem which copies one item .
I set a textbox value in the UI but it returns an An **exception 'Microsoft.VisualStudio.Debugger.Runtime.CrossThreadMessagingException' occurred**
How do I invoke the copy function in the backgroundworker todo event When I call the runworkerasync method in the click event I get an error
private void btnCopyItems_Click(object sender, EventArgs e)
{
backgroundWorker1.RunWorkerAsync();
}
I created a class
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));
}
}
Aucun commentaire:
Enregistrer un commentaire