Re: Windows App... processing completes before form is painted on

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Ok, I figured it out. The GUI thread cannot update the controls as we would
think. You have to use a background thread:

I found a link to help you get there.
'http://msdn.microsoft.com/library/?url=/library/en-us/dv_vstechart/html/vbasync.asp

I may have implemented it wrong, but it certainly works.

Public Class Form1
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
Me.ProgressBar1.Minimum = 0
Me.ProgressBar1.Maximum = 100
Me.ProgressBar1.Value = 0

End Sub

#End Region

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

Me.Show()

Dim t As System.Threading.Thread
t = New System.Threading.Thread(AddressOf Process)

t.Start()

While t.IsAlive
Application.DoEvents()
End While

Close()

End Sub

Public Sub Process()

Dim iProgress As Integer
Const searchpath As String = "c:\test"
Const archivepath As String = "c:\archived"

Try
Dim filelist As ArrayList
Dim sFilePath As String
Dim sArchiveFilePath As String

filelist = New ArrayList
filelist.AddRange(System.IO.Directory.GetFiles(searchpath, "*.*"))

For Each sFile As String In filelist

ClientUpdateProgress(sFile, filelist.IndexOf(sFile), filelist.Count)
System.Threading.Thread.CurrentThread.Sleep(1000)

sFilePath = System.IO.Path.Combine(searchpath, sFile)
sArchiveFilePath = System.IO.Path.Combine(archivepath,
System.Guid.NewGuid.ToString("N"))

If (System.IO.File.GetAttributes(sFilePath) And Not
System.IO.FileAttributes.Archive) = IO.FileAttributes.Archive Then
Try
System.IO.File.Copy(sFilePath, sArchiveFilePath)
WriteToCatalog(sFilePath, sArchiveFilePath)
System.IO.File.SetAttributes(sFilePath,
System.IO.File.GetAttributes(sFilePath) Or IO.FileAttributes.Archive)
Catch ex As Exception
Trace.WriteLine(String.Format("Failed to archive {0}. The
exception was {1}", sFile, ex))
End Try
End If

SetFileProgress(sFile)

System.Threading.Thread.CurrentThread.Sleep(1000)

Next

Catch ex As Exception
Trace.WriteLine(String.Format("An unexpected exception occured during
the archive process. The exception was {0}", ex))
End Try

End Sub

Public Sub ClientUpdateProgress(ByVal FileName As String, ByVal
CurrentStep As Integer, ByVal TotalSteps As Integer)

Trace.WriteLine(String.Format("{0}, {1}, {2}", FileName, CurrentStep,
TotalSteps))

Dim iTemp As Integer

Me.Label1.Text = FileName
Me.Label2.Text = "Processing"

iTemp = Int(CurrentStep / TotalSteps * 100)

Me.ProgressBar1.Value = iTemp

End Sub

Public Sub SetFileProgress(ByVal FileName As String)

Me.Label1.Text = FileName
Me.Label2.Text = "Completed"

End Sub

<MTAThread()> Public Shared Sub Main()

Dim fProgress As Form1
fProgress = New Form1
Application.Run(fProgress)

End Sub


End Class

"hzgt9b@xxxxxxxxxx" <hzgt9bnopostcom@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in
message news:FC073EB8-DF11-48BF-8668-564ACD5C45B2@xxxxxxxxxxxxxxxx
> Thanks for your quick feedback.
>
> I aready have a "main" - but I'm not sure how to tell the form to paint
> before starting processing. "init()" doesn't work for me (VB .NET 2003)...
>
> Any other thpughts?
>
>
> "AMDRIT" wrote:
>
>> Are you performing these tasks in the constructor or the form_load event?
>> If you would like to display a progess while all your actions are taking
>> place, you should, instead; change the way your code is laid out.
>>
>> Create a sub Main that will perform the processing. First load the
>> desired
>> for for visual feed back, then perform your steps, close your form and
>> exit
>> the application.
>>
>> <STAThread()> Sub Main()
>>
>> Dim fProgress As frmProgress
>> Dim iProgress As Integer
>> Const searchpath As String = "c:\test"
>> Const archivepath As String = "c:\archived"
>> fProgress = New frmProgress
>>
>> Try
>>
>> Dim filelist As ArrayList
>> Dim sFilePath As String
>> Dim sArchiveFilePath As String
>>
>> filelist = New ArrayList
>> filelist.AddRange(System.IO.Directory.GetFiles(searchpath, "*.*"))
>>
>> fProgress.Init()
>>
>> For Each sFile As String In filelist
>>
>> fProgress.SetProgress(sFile, filelist.IndexOf(sFile),
>> filelist.Count)
>> System.Threading.Thread.CurrentThread.Sleep(50)
>>
>> sFilePath = System.IO.Path.Combine(searchpath, sFile)
>> sArchiveFilePath = System.IO.Path.Combine(archivepath,
>> System.Guid.NewGuid.ToString("N"))
>>
>> If (System.IO.File.GetAttributes(sFilePath) And
>> System.IO.FileAttributes.Archive) <> IO.FileAttributes.Archive Then
>> Try
>> System.IO.File.Copy(sFilePath, sArchiveFilePath)
>> WriteToCatalog(sFilePath, sArchiveFilePath)
>> System.IO.File.SetAttributes(sFilePath,
>> System.IO.File.GetAttributes(sFilePath) Or IO.FileAttributes.Archive)
>> Catch ex As Exception
>> Trace.WriteLine(String.Format("Failed to archive {0}. The
>> exception was {1}", sFile, ex))
>> End Try
>> End If
>>
>> fProgress.EndProgress(sFile)
>>
>> System.Threading.Thread.CurrentThread.Sleep(50)
>>
>> Next
>>
>> Catch ex As Exception
>> Trace.WriteLine(String.Format("An unexpected exception occured
>> during
>> the archive process. The exception was {0}", ex))
>> End Try
>>
>> Application.Exit()
>>
>> End Sub
>>
>>
>> "hzgt9b@xxxxxxxxxx" <hzgt9bnopostcom@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in
>> message news:5E3267EE-5B6E-4AAC-A4DA-BC9386400BFA@xxxxxxxxxxxxxxxx
>> > Using VB .NET 2003,
>> > I have a windows application that performs a series of file actions
>> > (copy,
>> > move, delete) but the actions are completing before the window is
>> > painted
>> > on
>> > the screen... how can I force the form to be painted on the screen
>> > first,
>> > then the actions start? Or should I pause the app for a few seconds
>> > while
>> > the
>> > form paints.
>> >
>> > Any suggestion woudl be appreciated!
>>
>>
>>


.



Relevant Pages

  • Project Error
    ... Private Declare Sub Sleep Lib "Kernel32" ... Dim strDataSrc As String ...
    (microsoft.public.vb.bugs)
  • Re: Is there a way to prevent a RichTextBox from scrolling?
    ... Private _isRegex As Boolean ... Public Sub New(ByVal thispattern As String, ... Dim entry As tDict ...
    (microsoft.public.dotnet.framework.windowsforms.controls)
  • Excel Listing tool using VB
    ... Sub ListFiles2() ... Dim directories() As String, CurrentDirectory As String ... Dim dirtopaste, dirok ...
    (microsoft.public.vb.general.discussion)
  • Form Error
    ... SMSDS_CallerID As String ... Private Declare Sub Sleep Lib "kernel32" ... Dim ComString As String ... Dim AppPath As String, FreeFileNo% ...
    (microsoft.public.vb.bugs)
  • Re: Encrypt/hide Password
    ... Public Sub New(ByVal strCryptoName As String) ... ' instantiated crypto class. ... Dim fsKey As New FileStream(strSaveToPath, FileMode.OpenOrCreate, _ ...
    (microsoft.public.scripting.wsh)