我有一个运行无UI的FTP进程。并有一个使用这个ftp控件的winform。在那个窗口中我有一个显示ftp上传进度的进度条。进度通过interfase进入窗口,在underliying主持人更新(我使用MVP模式)。使用线程刷新进度条UI
我的问题是当试图更新进度时,它总是抛出我这个异常。
通过线程非法操作:控制'prgProgresoSubido'是从一个线程访问,而不是您创建它的线程。
即使我在表单中使用BackGroundWorker,该问题仍然存在。
// This is a delegated on presenter when a File finish to upload
void client_FileUploadCompletedHandler(object sender, FileUploadCompletedEventArgs e)
{
string log = string.Format("{0} Upload from {1} to {2} is completed. Length: {3}. ",
DateTime.Now, e.LocalFile.FullName, e.ServerPath, e.LocalFile.Length);
archivosSubidos += 1;
_Publicacion.ProgresoSubida = (int)((archivosSubidos/archivosXSubir) * 100);
//this.lstLog.Items.Add(log);
//this.lstLog.SelectedIndex = this.lstLog.Items.Count - 1;
}
// This is My interfase
public interface IPublicacion
{
...
int ProgresoSubida { set; }
}
/// And Here is the implementartion of the interfase on the form
public partial class PublicarForm : Form ,IPublicacion
{
//Credenciales para conectarse al servicio FTP
public FTPClientManager client = null;
public XmlDocument conf = new XmlDocument();
public string workingDir = null;
public webTalk wt = new webTalk();
private readonly PublicacionesWebBL _Publicador;
public PublicarForm()
{
InitializeComponent();
String[] laPath = { System.AppDomain.CurrentDomain.BaseDirectory};
String lcPath = System.IO.Path.Combine(laPath);
_Publicador = new PublicacionesWebBL(this, lcPath);
}
public int ProgresoSubida
{
set
{
// This is my prograss bar, here it throw the exception.
prgProgresoSubido.Value = value;
}
}
}
我该怎么办才能避免这个问题?
*“这个问题仍然存在,即使我用一个BackgroundWorker” * - 你可能不正确地使用BackgroudWorker – GolfWolf 2013-02-22 15:33:49
也许....这是我的第一次用它。也许你有一个简单的?问题是ftp在它自己的Thread中工作,然后我需要克隆另一个trhead来更新进度条? – 2013-02-22 15:34:47
'BackgroudWorker'应该很简单:使用ReportProgress方法显示当前的进度并处理ProgressChanged事件中的UI部分(将在正确的线程中调用)。请参阅http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.progresschanged.aspx – GolfWolf 2013-02-22 15:36:34