تقييم الموضوع :
  • 0 أصوات - بمعدل 0
  • 1
  • 2
  • 3
  • 4
  • 5
بوتن ايقاف BackgroundWorker
#17
كود :
Imports System.Text.RegularExpressions
Imports System.ComponentModel

Public Class Form1

   Dim WithEvents backwork As New BackgroundWorker With {.WorkerSupportsCancellation = True}
   Dim Status As New StatusStrip
   Dim StatusLabel = New ToolStripStatusLabel() With {.Text = "Ready."}
   Dim StatusProgressBar As New ToolStripProgressBar() With {.Style = ProgressBarStyle.Marquee, .Visible = False, .MarqueeAnimationSpeed = 1}

   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       Control.CheckForIllegalCrossThreadCalls = False
       Me.Status.Items.Add(StatusProgressBar)
       Me.Status.Items.Add(StatusLabel)
       Me.Controls.Add(Status)
       '----------------------
       Me.TextBox1.ReadOnly = True
       Me.TextBox2.ReadOnly = True

       Me.Button3.Enabled = False

   End Sub

   Dim الملف_الرئيسي As String
   Dim مرجع_التقسيم As String

   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       Using o As New OpenFileDialog
           o.Filter = "Text files (*.txt)|*.txt"
           If o.ShowDialog = Windows.Forms.DialogResult.OK Then
               الملف_الرئيسي = o.FileName
               TextBox1.Text = IO.Path.GetFileName(o.FileName)
           End If
       End Using
   End Sub

   Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
       Using o As New OpenFileDialog
           o.Filter = "Text files (*.txt)|*.txt"
           If o.ShowDialog = Windows.Forms.DialogResult.OK Then
               مرجع_التقسيم = o.FileName
               TextBox2.Text = IO.Path.GetFileName(o.FileName)

               Me.Button1.Enabled = False
               Me.Button2.Enabled = False
               Me.TextBox1.Enabled = False
               Me.TextBox2.Enabled = False
               Me.TextBox3.Enabled = False
               Me.TextBox4.Enabled = False
               Me.ListBox1.Items.Clear()

               StatusProgressBar.Visible = True

               backwork.RunWorkerAsync()

               Me.Button3.Enabled = True

           End If
       End Using
   End Sub

   Private Sub backwork_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles backwork.DoWork
       Try
           Dim sourceLines() As String = IO.File.ReadAllLines(الملف_الرئيسي)

           ' header ------------------------------
           Me.StatusLabel.Text = "header working..."
           Dim header(7 - 1) As String
           Array.Copy(sourceLines, 0, header, 0, 7)
           Me.StatusLabel.Text = "header completed"

           ' details -----------------------------
           Me.StatusLabel.Text = "details working..."
           Dim details(sourceLines.Count - 7 - 1) As String
           Array.Copy(sourceLines, 7, details, 0, sourceLines.Count - 7)

           For i = 0 To details.Length - 1
               If backwork.CancellationPending Then
                   e.Cancel = True
                   Exit Sub
               End If
               details(i) = details(i).Substring(9, 4) & " - " & details(i)
           Next
           Array.Sort(details)
           For i = 0 To details.Length - 1
               If backwork.CancellationPending Then
                   e.Cancel = True
                   Exit Sub
               End If
               details(i) = details(i).Substring(7)
           Next
           Me.StatusLabel.Text = "header completed"


           'splitfiles ---------------------------------------------
           Dim files() As String = IO.File.ReadAllLines(مرجع_التقسيم)
           For Each f As String In files
               If backwork.CancellationPending Then
                   e.Cancel = True
                   Exit Sub
               End If
               If f.Trim <> "" Then
                   Dim filename As String = f.Substring(0, 8)
                   Me.StatusLabel.Text = filename & " working..."
                   Dim flagStart As Integer = Val(f.Substring(9, 4))
                   Dim flagEnd As Integer = Val(f.Substring(14, 4))
                   Dim flagCount As Integer = Val(f.Substring(19))
                   Dim range = Enumerable.Range(flagStart, flagEnd - flagStart + 1)
                   Dim RegexPattern As String = "^\w(\d{3})\s\d{4}(" & String.Join("|", range) & ")\s.+"

                   Dim ms As MatchCollection = Regex.Matches(String.Join(vbNewLine, details), RegexPattern, RegexOptions.Multiline)
                   Dim resultLines = (From x As Match In ms Select x.Value).ToArray

                   If flagCount <> ms.Count Then
                       ListBox1.Items.Add(filename)
                   End If

                   Dim resultFile As String = String.Empty
                   resultFile &= "date: xxxxxxxxx" & vbNewLine
                   resultFile &= "mois:  xxxxxxxxxx " & vbNewLine
                   resultFile &= "project: xxxxxxxxx" & vbNewLine
                   resultFile &= "observation: xxxxxxxx" & vbNewLine
                   resultFile &= "unti: xxxxxxxxxxxxxxx" & vbNewLine
                   resultFile &= "" & vbNewLine
                   resultFile &= "" & vbNewLine

                   resultFile &= String.Join(vbNewLine, resultLines) & vbNewLine

                   IO.File.WriteAllText(filename & ".txt", resultFile)
                   Me.StatusLabel.Text = filename & " completed"
               End If
           Next

       Catch ex As Exception
           Me.StatusLabel.Text = "Error."
           MsgBox(ex.Message)
       End Try
   End Sub


   Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
       If backwork.IsBusy Then backwork.CancelAsync()
       Me.Button3.Enabled = False
   End Sub

   Private Sub backwork_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles backwork.RunWorkerCompleted
       If e.Cancelled Then
           Me.StatusLabel.Text = "Cancelled."
       Else
           Me.StatusLabel.Text = "Successful."
       End If

       StatusProgressBar.Visible = False

       Me.Button1.Enabled = True
       Me.Button2.Enabled = True
       Me.TextBox1.Enabled = True
       Me.TextBox2.Enabled = True
       Me.TextBox3.Enabled = True
       Me.TextBox4.Enabled = True

       Me.Button3.Enabled = False

   End Sub

End Class
الرد }}}
تم الشكر بواسطة:


الردود في هذا الموضوع
بوتن ايقاف BackgroundWorker - بواسطة rabeh.ram - 21-06-17, 12:15 AM
RE: بوتن ايقاف BackgroundWorker - بواسطة silverlight - 22-06-17, 03:51 AM
RE: بوتن ايقاف BackgroundWorker - بواسطة rabeh.ram - 22-06-17, 04:32 AM
RE: بوتن ايقاف BackgroundWorker - بواسطة سعود - 22-06-17, 04:55 AM
RE: بوتن ايقاف BackgroundWorker - بواسطة rabeh.ram - 22-06-17, 05:39 AM
RE: بوتن ايقاف BackgroundWorker - بواسطة سعود - 22-06-17, 05:52 AM
RE: بوتن ايقاف BackgroundWorker - بواسطة rabeh.ram - 22-06-17, 06:40 AM
RE: بوتن ايقاف BackgroundWorker - بواسطة سعود - 22-06-17, 06:44 AM
RE: بوتن ايقاف BackgroundWorker - بواسطة silverlight - 22-06-17, 06:59 AM
RE: بوتن ايقاف BackgroundWorker - بواسطة rabeh.ram - 22-06-17, 12:00 PM
RE: بوتن ايقاف BackgroundWorker - بواسطة silverlight - 23-06-17, 02:51 AM
RE: بوتن ايقاف BackgroundWorker - بواسطة rabeh.ram - 23-06-17, 04:04 AM
RE: بوتن ايقاف BackgroundWorker - بواسطة silverlight - 23-06-17, 06:00 AM
RE: بوتن ايقاف BackgroundWorker - بواسطة rabeh.ram - 23-06-17, 06:23 AM
RE: بوتن ايقاف BackgroundWorker - بواسطة silverlight - 23-06-17, 07:14 AM
RE: بوتن ايقاف BackgroundWorker - بواسطة rabeh.ram - 23-06-17, 09:17 AM
RE: بوتن ايقاف BackgroundWorker - بواسطة WL_2000 - 23-06-17, 09:41 AM
RE: بوتن ايقاف BackgroundWorker - بواسطة rabeh.ram - 23-06-17, 11:39 AM


التنقل السريع :


يقوم بقرائة الموضوع: