Maxikat
Messages postés5Date d'inscriptionmardi 4 mars 2003StatutMembreDernière intervention 6 décembre 2004
-
6 déc. 2004 à 17:43
Wardall
Messages postés40Date d'inscriptionmardi 18 décembre 2001StatutMembreDernière intervention10 décembre 2004
-
10 déc. 2004 à 02:32
Hello.
Je fais un petit code d'exemple (ci-dessous) pour l'utilisation des fonctions delegate. Mon souci principal est de lancer divers traitements en asynchrone.
1. Est-ce que vous pensez que j'ai utiliser la meilleure sollution ?
2. Je n'arrive pas à traiter les erreurs hors du thread principal. Je provoque une erreur mais rien ne se passe. Le thread est annulé et le endInvoke ne récupère pas l'erreur. POURQUOI ?
Copie du code :
Option Explicit On
Option Strict On
Public Class Form1
Inherits System.Windows.Forms.Form
Public Delegate Function TestDelegateSimple(ByRef info As String) As String
Dim e As System.Threading.ManualResetEvent
'Dim infoSortie As String
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
Me.Button1.Text = "Start"
Me.Label1.Text = ""
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Start()
Me.Label1.Text += vbCrLf + "Button1 Fini"
End Sub
Private Sub Start()
Dim cb As AsyncCallback = New AsyncCallback(AddressOf AsyncInfo)
Dim d As TestDelegateSimple = New TestDelegateSimple(AddressOf AsyncStartSimple)
Dim infoEnter As String = "Debut Thread"
Try
d.BeginInvoke(infoEnter, cb, "Deb")
Catch
Debug.Assert(False)
End Try
End Sub
Shared Function AsyncStartSimple(ByRef info As String) As String
'Je ne sais pas récupérer les erreurs !!! HELP !!!
' Try
'Debug.Assert(False)Dim i As Double 0 : i 1 / 0 'Ne plante pas : renvoi l'infini ! si si 'Dim tb As Int16() : ReDim tb(1) : tb(0) 0 : tb(1) 1 : tb(2) = 2
Return "ca marche ici " + i.ToString
' Catch ex As Exception
' Throw ex
'Return ex.ToString
'Finally
'Debug.Assert(False)
'End Try
'MsgBox(info)
End Function
Shared Sub AsyncStart(ByRef sqlDA As System.Data.SqlClient.SqlDataAdapter, ByRef TestDt As System.Data.DataTable)
'Threading.Thread.CurrentThread.Sleep(500)
MsgBox("ici")
sqlDA = New System.Data.SqlClient.SqlDataAdapter("Select * from CLIP_DIRCOM", "server=(local)\NetSDK;Trusted_Connection=yes;database=CLIP_Local")
sqlDA.Fill(TestDt)
End Sub
Public Sub AsyncInfo(ByVal ar As System.IAsyncResult)
Dim value As String = Convert.ToString(ar.AsyncState)
Dim Async As System.Runtime.Remoting.Messaging.AsyncResult
Async = CType(ar, System.Runtime.Remoting.Messaging.AsyncResult)
Dim tmp As TestDelegateSimple = CType(Async.AsyncDelegate, TestDelegateSimple)
Dim result As String = tmp.EndInvoke("tmp", ar)
Me.Label1.Text += vbCrLf + "Passé là : " + value + " -> " + result