By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,509 Members | 1,301 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,509 IT Pros & Developers. It's quick & easy.

Windows App... processing completes before form is painted on scre

P: n/a
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!
Jan 19 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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(sea rchpath, "*.*"))

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
"hz****@nopost.com" <hz*************@discussions.microsoft.com> wrote in
message news:5E**********************************@microsof t.com...
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!

Jan 19 '06 #2

P: n/a
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(sea rchpath, "*.*"))

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
"hz****@nopost.com" <hz*************@discussions.microsoft.com> wrote in
message news:5E**********************************@microsof t.com...
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!


Jan 19 '06 #3

P: n/a
Init() doesn't exist by default. You would have to create that. I only put
it there to illustrate that you could still interact with the form from
within your main()

I wrote this code blind, and demonstrated why we need to test our code
before releasing it. First I never displayed the form, add fProgress.Show()
after the initialization. This will display the form. Second, I never
closed the form before exiting the application, add fProgress.Close() before
application.exit(). This will close the form.

Second, if the form in not repaiting for you as you think it should, try
fProgress.Invalidate. You should not have to worry about this, as it should
just work as expected or you have a blocking issue between the GUI thread
and your application. I have not experimented with the code that I sent to
you, give me some time and I will.


"hz****@nopost.com" <hz*************@discussions.microsoft.com> wrote in
message news:FC**********************************@microsof t.com...
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(sea rchpath, "*.*"))

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
"hz****@nopost.com" <hz*************@discussions.microsoft.com> wrote in
message news:5E**********************************@microsof t.com...
> 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!


Jan 19 '06 #4

P: n/a
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(sea rchpath, "*.*"))

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

"hz****@nopost.com" <hz*************@discussions.microsoft.com> wrote in
message news:FC**********************************@microsof t.com...
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(sea rchpath, "*.*"))

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
"hz****@nopost.com" <hz*************@discussions.microsoft.com> wrote in
message news:5E**********************************@microsof t.com...
> 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!


Jan 19 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.