473,503 Members | 1,654 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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
4 2092
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2749
by: Russ McDaniel | last post by:
Originally posted to microsoft.public.dotnet.distributed_apps with no response. Reposted here with additional thoughts. --- Hello, I'm writing a Windows service which performs some...
8
2476
by: Prakash | last post by:
I have a msgbox in my form activate event with an OK button. However the msgbox shows up BEFORE the form is painted. I would like the msgbox to be visible after the form gets painted on...
1
1265
by: Will | last post by:
Hi All, I'm trying to find the correct form event to use to make a call to a database AFTER my form has been completely painted. The call to the database takes a few seconds and when I tried...
6
2614
by: billr | last post by:
I have developed a small API for taking care of a lot of boiler plate stuff in a multi formed windows application, for example setting up a messaging thread framework. New Forms, in the...
14
4073
by: | last post by:
Hi All, I am little confused here, hope you can help me. While processing WM_POWERBROADCAST (wParam=PBT_APMQUERYSUSPEND), I MUST to do some lengthy operation(30 sec) before system Suspends or...
11
5261
by: Crirus | last post by:
I need to derive the Windows.Forms.Control 2 times so I design a class like this Public Class BMControl Inherits System.Windows.Forms.UserControl Public Class MapControl Inherits BMControl
5
10560
by: eb65 | last post by:
I have a need to write a Windows Service application in VB.Net that needs to continuously do some processing, wait ten minutes, then repeat. What is a good approach to coding this type of thing?...
6
1627
by: Nayan | last post by:
I have been working on a card game (called Uno) for past few days. Language used : C# 2.0 When I start a new game, a dialog window pops up asking the name of the player. I enter the name, and...
15
4740
by: Angelo | last post by:
Hi all, I'm using a FileSystemWatcher to monitor a directory. I want the FSW to pop up my already instantiated but invisible form. However, I'm running into some problems with this. 1) In...
0
7198
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7319
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6979
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7449
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5570
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4998
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3160
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1498
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
373
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.