Καλώς ορίσατε στο dotNETZone.gr - Σύνδεση | Εγγραφή | Βοήθεια
σε

 

Αρχική σελίδα Ιστολόγια Συζητήσεις Εκθέσεις Φωτογραφιών Αρχειοθήκες

Asynchronous execution και background Thread

Îåêßíçóå áðü ôï ìÝëïò axaros. Τελευταία δημοσίευση από το μέλος RedHat στις 21-01-2006, 22:04. Υπάρχουν 3 απαντήσεις.
Ταξινόμηση Δημοσιεύσεων: Προηγούμενο Επόμενο
  •  04-10-2005, 10:22 5869

    Asynchronous execution και background Thread

    Καλημέρα σε όλους !!!
    Το πρόβλημα :
    Θέλω να εκτελέσω ένα "βαρύ" statement αλλά και να δώσω τη δυνατότητα ακύρωσης στο χρήστη ανά πάσα στιγμή.
    Χρησιμοποίησα ένα background thread. 'Ηθελα τη γνώμη σας :

    Στο click Event μίας Win form σηκώνω modally μία άλλη (με ένα label με το κλασσικό "Παρακαλώ περιμένετε"
    ένα PicureBox με κάποιο animated gif και ένα Cancel button)

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click

       Dim MyForm As New frmWait

       Try
          
    With DataGrid1
             .DataSource =
    Nothing
             
    .Refresh()
          
    End With

          MyForm.ShowDialog(Me)

          With DataGrid1
             .DataSource = MyForm.Data
          
    End With

       Catch ex As Exception
          MsgBox(ex.Message)
       
    Finally
          
    MyForm.Dispose()
       
    End Try
    End Sub

    Ο κώδικας της frmWait είναι :

    Imports System.Data
    Imports System.Data.OleDb
    Imports System.Threading
    Imports System.Windows.Forms

    Public Class frmWait
       
    Inherits System.Windows.Forms.Form

       #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
          
    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
       
    'NOTE: The following procedure is required by the Windows Form Designer
       
    'It can be modified using the Windows Form Designer. 
       
    'Do not modify it using the code editor.
       
    Friend WithEvents Label1 As System.Windows.Forms.Label
       
    Friend WithEvents Button1 As System.Windows.Forms.Button
       
    Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox

    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(frmWait))

    Me.Label1 = New System.Windows.Forms.Label
    Me.Button1 = New System.Windows.Forms.Button
    Me.PictureBox1 = New System.Windows.Forms.PictureBox

    Me.SuspendLayout()

    '

    'Label1

    '

    Me.Label1.Font = New System.Drawing.Font("Tahoma", 9.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))

    Me.Label1.Location = New System.Drawing.Point(24, 128)

    Me.Label1.Name = "Label1"

    Me.Label1.Size = New System.Drawing.Size(157, 16)

    Me.Label1.TabIndex = 0

    Me.Label1.Text = "Παρακαλώ περιμένετε "

    '

    'Button1

    '

    Me.Button1.Location = New System.Drawing.Point(56, 160)

    Me.Button1.Name = "Button1"

    Me.Button1.Size = New System.Drawing.Size(80, 23)

    Me.Button1.TabIndex = 1

    Me.Button1.Text = "Ακύρωση"

    '

    'PictureBox1

    '

    Me.PictureBox1.Image = CType(resources.GetObject("PictureBox1.Image"), System.Drawing.Image)

    Me.PictureBox1.Location = New System.Drawing.Point(40, 16)

    Me.PictureBox1.Name = "PictureBox1"

    Me.PictureBox1.Size = New System.Drawing.Size(112, 96)

    Me.PictureBox1.TabIndex = 2

    Me.PictureBox1.TabStop = False

    '

    'frmWait

    '

    Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)

    Me.ClientSize = New System.Drawing.Size(190, 198)

    Me.ControlBox = False

    Me.Controls.Add(Me.PictureBox1)

    Me.Controls.Add(Me.Button1)

    Me.Controls.Add(Me.Label1)

    Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle

    Me.Name = "frmWait"

    Me.ShowInTaskbar = False

    Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen

    Me.ResumeLayout(False)

    End Sub

    #End Region

       Private MyDTable As DataTable
       Protected Friend Property Data() As DataTable
          
    Get
             
    Data = MyDTable
          
    End Get
          
    Set(ByVal Value As DataTable)
             MyDTable = Value
          
    End Set
       
    End Property

    Dim MyAsyncThread As Thread
    Dim MyAsyncThreadStart As New ThreadStart(AddressOf GetData)

    Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

    Try

    If MessageBox.Show("Θέλετε σίγουρα να ακυρώσετε την προεπισκόπηση" + _
       "των αποτελεσμάτων;", "Ακύρωση", MessageBoxButtons.YesNo, _ 
       MessageBoxIcon.Question, MessageBoxDefaultButton.Button2, _
       MessageBoxOptions.DefaultDesktopOnly) = DialogResult.No
    Then 
       Exit Sub
    End If

    If MyAsyncThread.ThreadState = ThreadState.Background Then
       
    With MyAsyncThread
          .Sleep(0)
          .Abort()
          .Join()
       End With

                   Application.DoEvents()
                   
    Me.Data = Nothing
                   
    Me.Close()
                
    End If

     

     

    Catch Aex As ThreadAbortException

    MsgBox("Operation aborted")

    Catch Eex As ThreadStateException

    MsgBox(Eex.Message)

    Catch ex As Exception

    Throw ex

    End Try

    End Sub

    Private Sub GetData()

    Dim MyDataset As New DataSet
    Dim MyConnection As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" + _
    "Data Source=C:\Atreus Co ltd\Sinp " + _
    "Reporter\Database\TEST.mdb;")
    Dim MyAdapter As New OleDbDataAdapter("SELECT * FROM VERY_LARGE_TABLE", _
                                                                                     MyConnection)

    Try

    MyDataset = New DataSet
    MyConnection.Open()
    MyAdapter.Fill(MyDataset, "TEST")
    MyConnection.Close()

    Me.Data = MyDataset.Tables(0)
    Application.DoEvents()
    Me.Close()

    Catch ex As Exception
                
    Throw ex
    Finally
                MyAdapter.Dispose()
                With MyConnection
                   .Close()
                   .Dispose()
                End With

    End Try

    End Sub

    Private Sub frmWait_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load

       MyAsyncThread = New Thread(MyAsyncThreadStart)
       
    With MyAsyncThread
          .IsBackground =
    True
          
    .Name = "GetDataThread"
          .Start()
       
    End With

    End Sub

    End Class

     




    Πάνος Αβραμίδης
  •  05-10-2005, 00:15 5886 σε απάντηση της 5869

    Απ: Asynchronous execution και background Thread

    Έχεις κοιτάξει αυτό http://www.dotnetzone.gr/cs/forums/1429/ShowPost.aspx το post?
    Δεν έχω διαβάσει προσεκτικά τον κώδικά σου, αλλά νομίζω ότι πραγματεύεται περίπου το ίδιο θέμα...


    Vir prudens non contra ventum mingit
  •  05-10-2005, 12:10 5892 σε απάντηση της 5886

    Απ: Asynchronous execution και background Thread

    Μάνο ακριβώς το ίδιο πράγμα ζητάω (αν και δεν με ενδιέφερε το progress view) !!!
    Θα το κοιτάξω και αν χρειαστεί θα επανέρθω.

    Σε ευχαριστώ πολύ!!!


    Πάνος Αβραμίδης
  •  21-01-2006, 22:04 8859 σε απάντηση της 5869

    Απ: Asynchronous execution και background Thread

    look and vb.net 2005 "backgroundWorker"
    Peter Bournakas
Προβολή Τροφοδοσίας RSS με μορφή XML
Με χρήση του Community Server (Commercial Edition), από την Telligent Systems