使用BackgroundWorker或Threading以获得高性能
如何在我的代码中使用BackgroundWorker 或Threading。 我更新 TreeView (Winforms) 并调用 WCF 服务。
有什么建议请。 亲切的问候。
提前致谢
AdministradorUILogging.TrazarDebug("PanelArbolFicheros. tslGuardarArbol_Click")
Dim listaFichero As New List(Of Fichero)
Windows.Forms.Cursor.Current = Cursors.WaitCursor
Me.TreeViewGB1.SuspendUpdate()
For Each nodo As NodoArbol In TreeViewGB1.Nodes
ProcesarNodo(nodo, listaFichero)
Next
Me.TreeViewGB1.ResumeUpdate()
' Cambiamos el cursor , tener en cuena si hacerlo asincrono '
Try
Using bfll As New ComunBfll()
bfll.AltaManualListaFicheros(listaFichero)
Mensajes.InformacionGuardada()
End Using
Catch ex As WCF.ServicioBase.Contrato.Excepciones.NoExisteOperacionException
Mensajes.AdvertenciaErrores("No existe la operación")
Catch ex As WCF.ServicioBase.Contrato.Excepciones.NoExisteExpedienteException
Mensajes.AdvertenciaErrores("No existe el expediente")
Catch ex As WCF.ServicioBase.Contrato.Excepciones.ConsistenciaException
Mensajes.AdvertenciaErrores("Inconsistencia detectada al superar el máximo permitido de ficheros para un tipo documental")
Catch ex As Exception
AdministradorUILogging.TrazarError(Me.[GetType]().FullName & " -> " & System.Reflection.MethodBase.GetCurrentMethod().Name & "." & ex.Message)
ExcepcionesIUUtil.ProcesarExcepcionPresentacion(ex, Me.Container)
End Try
InicializarArbol()
Windows.Forms.Cursor.Current = Cursors.Arrow
How can use BackgroundWorker or Threading for my code. I update TreeView (Winforms) and I call WCF service.
Any suggestions please. Kind regards.
Thanks in advance
AdministradorUILogging.TrazarDebug("PanelArbolFicheros. tslGuardarArbol_Click")
Dim listaFichero As New List(Of Fichero)
Windows.Forms.Cursor.Current = Cursors.WaitCursor
Me.TreeViewGB1.SuspendUpdate()
For Each nodo As NodoArbol In TreeViewGB1.Nodes
ProcesarNodo(nodo, listaFichero)
Next
Me.TreeViewGB1.ResumeUpdate()
' Cambiamos el cursor , tener en cuena si hacerlo asincrono '
Try
Using bfll As New ComunBfll()
bfll.AltaManualListaFicheros(listaFichero)
Mensajes.InformacionGuardada()
End Using
Catch ex As WCF.ServicioBase.Contrato.Excepciones.NoExisteOperacionException
Mensajes.AdvertenciaErrores("No existe la operación")
Catch ex As WCF.ServicioBase.Contrato.Excepciones.NoExisteExpedienteException
Mensajes.AdvertenciaErrores("No existe el expediente")
Catch ex As WCF.ServicioBase.Contrato.Excepciones.ConsistenciaException
Mensajes.AdvertenciaErrores("Inconsistencia detectada al superar el máximo permitido de ficheros para un tipo documental")
Catch ex As Exception
AdministradorUILogging.TrazarError(Me.[GetType]().FullName & " -> " & System.Reflection.MethodBase.GetCurrentMethod().Name & "." & ex.Message)
ExcepcionesIUUtil.ProcesarExcepcionPresentacion(ex, Me.Container)
End Try
InicializarArbol()
Windows.Forms.Cursor.Current = Cursors.Arrow
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用BackgroundWorker或一个线程不会使您的应用程序运行得更快,而只会阻止您的应用程序不响应。
如果您只需要一个线程,只需使用BackgroundWorker,这就是它的用途。
如果在这种精确的情况下,您的listaFichero变量可以分成更小的列表并并行处理,以及客户端和服务器之间的带宽不是瓶颈,并且您的服务器支持并行进程,那么您可以创建多个线程。
Using a BackgroundWorker or one thread will not make your application go faster, but will just prevent your application from not responding.
If you only need one thread, just use a BackgroundWorker, that's what it's meant for.
If in this precise case, your listaFichero variable can be splitted into smaller lists and processed in parallel, and the bandwidth between the client and the server is not a bottleneck, and your server supports parallel process, then you can create multiple threads.
说实话,只要你做得正确,这并不重要。
与后台工作人员一起正确完成工作会更容易,因此我建议这样做。
To be honest it doesn't matter as long as you do it properly.
It easier to do properly with a background worker so i'd suggest doing it with that.