mcsdguyian
asked on
.Net Threading Wait/Busy Form
I want to display a animated gif while some data is being loaded in the background on its own thread. the code works and loads the image, but after running the function a couple time I get "Process cannot access a file because it is being used by another process". I believe that it has something to do with how I am using the gif file. any help to fix this would be appreciated.
---:[My Class Code For Threading]:---
Public Class clsOPAWaitScreen
Dim Thread1 As System.Threading.Thread
Dim m_intTop As Integer
Dim m_intLeft As Integer
Dim objPlsWait As New frmPleaseWait
Public Sub ShowWaitScreen(ByVal strMessage As String, ByVal p_intTop As Integer, ByVal p_intLeft As Integer)
Thread1 = New System.Threading.Thread(Ad dressOf MakeItHappen)
Thread1.Name = strMessage ' Name is being used here to pass parameters. (In this case a message.)
m_intTop = p_intTop
m_intLeft = p_intLeft
Thread1.Start()
End Sub
Private Sub MakeItHappen(ByVal strMsg As String)
Try
objPlsWait.SetLocation(m_i ntTop, m_intLeft)
objPlsWait.SetMessage(Syst em.Threadi ng.Thread. CurrentThr ead.Name)
objPlsWait.ShowDialog()
Catch ex As Exception
' Do nothing supress error.
Finally
If objPlsWait IsNot Nothing Then objPlsWait.Dispose() : objPlsWait = Nothing
End Try
End Sub
Public Sub CloseWaitScreen()
'objPlsWait.Close()
Thread1.Abort(Nothing) ' Makes the form go away.
End Sub
End Class
---:[My Wait Processing Form]:---
Public Class frmPleaseWait
Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
'Me.PictureBox1.Image = ImageList1.Images(0)
PictureBox1.Image = My.Resources.WaitProcessin g1
End Sub
Public Sub SetMessage(ByVal strMessage As String)
lblMsg.Text = strMessage
End Sub
Public Sub SetLocation(ByVal p_intTop As Integer, ByVal p_intLeft As Integer)
p_intLeft = p_intLeft - CInt(Me.Width / 2)
p_intTop = p_intTop - CInt(Me.Height / 2)
Me.Location = New Point(p_intLeft, p_intTop)
End Sub
End Class
---:[How I call the Code]:---
Dim objPlsWait As New clsOPAWaitScreen
objPlsWait.ShowWaitScreen( "Please wait...", intTop, intLeft)
'---:[Run Query]:---
objPlsWait.CloseWaitScreen ()
objPlsWait = Nothing
Thanks
Ian
---:[My Class Code For Threading]:---
Public Class clsOPAWaitScreen
Dim Thread1 As System.Threading.Thread
Dim m_intTop As Integer
Dim m_intLeft As Integer
Dim objPlsWait As New frmPleaseWait
Public Sub ShowWaitScreen(ByVal strMessage As String, ByVal p_intTop As Integer, ByVal p_intLeft As Integer)
Thread1 = New System.Threading.Thread(Ad
Thread1.Name = strMessage ' Name is being used here to pass parameters. (In this case a message.)
m_intTop = p_intTop
m_intLeft = p_intLeft
Thread1.Start()
End Sub
Private Sub MakeItHappen(ByVal strMsg As String)
Try
objPlsWait.SetLocation(m_i
objPlsWait.SetMessage(Syst
objPlsWait.ShowDialog()
Catch ex As Exception
' Do nothing supress error.
Finally
If objPlsWait IsNot Nothing Then objPlsWait.Dispose() : objPlsWait = Nothing
End Try
End Sub
Public Sub CloseWaitScreen()
'objPlsWait.Close()
Thread1.Abort(Nothing) ' Makes the form go away.
End Sub
End Class
---:[My Wait Processing Form]:---
Public Class frmPleaseWait
Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
'Me.PictureBox1.Image = ImageList1.Images(0)
PictureBox1.Image = My.Resources.WaitProcessin
End Sub
Public Sub SetMessage(ByVal strMessage As String)
lblMsg.Text = strMessage
End Sub
Public Sub SetLocation(ByVal p_intTop As Integer, ByVal p_intLeft As Integer)
p_intLeft = p_intLeft - CInt(Me.Width / 2)
p_intTop = p_intTop - CInt(Me.Height / 2)
Me.Location = New Point(p_intLeft, p_intTop)
End Sub
End Class
---:[How I call the Code]:---
Dim objPlsWait As New clsOPAWaitScreen
objPlsWait.ShowWaitScreen(
'---:[Run Query]:---
objPlsWait.CloseWaitScreen
objPlsWait = Nothing
Thanks
Ian
Nothing in the code you posted pops out as being a potential cause for that error.
What are you doing in " '---:[Run Query]:---"?
What are you doing in " '---:[Run Query]:---"?
ASKER
I am just executing some SQL stored procedures that do some long database updates.
Does the error go away if you do not use the image?
ASKER
I recently re installed my computer and I didn't have service pack1 on Visual studio 2010. I installed that and I haven't seen the error.
When I run the program from another computer with out VS2010, I get the following error "System.Threading.ThreadAb ortExcepti on: Thread was being aborted." and sometimes the image has a Big Red "X" through it.
When running it on my computer I was under the impression that it was the image. Now after removing the image and running it on a client computer I have more information in the form of error messages.
Its not the image, but has something else to do with threading.
There is a full error detail I am posting below if it helps.
See the end of this message for details on invoking
just-in-time (JIT) debugging instead of this dialog box.
************** Exception Text **************
System.Threading.ThreadAbo rtExceptio n: Thread was being aborted.
at System.Windows.Forms.Unsaf eNativeMet hods.SetWi ndowLongPt r32(Handle Ref hWnd, Int32 nIndex, WndProc wndproc)
at System.Windows.Forms.Unsaf eNativeMet hods.SetWi ndowLong(H andleRef hWnd, Int32 nIndex, WndProc wndproc)
at System.Windows.Forms.Nativ eWindow.As signHandle (IntPtr handle, Boolean assignUniqueID)
at System.Windows.Forms.Nativ eWindow.As signHandle (IntPtr handle)
at System.Windows.Forms.Nativ eWindow.Wi ndowClass. Callback(I ntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.Unsaf eNativeMet hods.IntCr eateWindow Ex(Int32 dwExStyle, String lpszClassName, String lpszWindowName, Int32 style, Int32 x, Int32 y, Int32 width, Int32 height, HandleRef hWndParent, HandleRef hMenu, HandleRef hInst, Object pvParam)
at System.Windows.Forms.Unsaf eNativeMet hods.Creat eWindowEx( Int32 dwExStyle, String lpszClassName, String lpszWindowName, Int32 style, Int32 x, Int32 y, Int32 width, Int32 height, HandleRef hWndParent, HandleRef hMenu, HandleRef hInst, Object pvParam)
at System.Windows.Forms.Nativ eWindow.Cr eateHandle (CreatePar ams cp)
at System.Windows.Forms.Contr ol.CreateH andle()
at System.Windows.Forms.Contr ol.CreateC ontrol(Boo lean fIgnoreVisible)
at System.Windows.Forms.Contr ol.CreateC ontrol(Boo lean fIgnoreVisible)
at System.Windows.Forms.Contr ol.CreateC ontrol()
at System.Windows.Forms.Contr ol.WmShowW indow(Mess age& m)
at System.Windows.Forms.Contr ol.WndProc (Message& m)
at System.Windows.Forms.Scrol lableContr ol.WndProc (Message& m)
at System.Windows.Forms.Conta inerContro l.WndProc( Message& m)
at System.Windows.Forms.Form. WmShowWind ow(Message & m)
at System.Windows.Forms.Form. WndProc(Me ssage& m)
at System.Windows.Forms.Contr ol.Control NativeWind ow.OnMessa ge(Message & m)
at System.Windows.Forms.Contr ol.Control NativeWind ow.WndProc (Message& m)
at System.Windows.Forms.Nativ eWindow.Ca llback(Int Ptr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
************** Loaded Assemblies **************
mscorlib
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.3643 (GDR.050727-3600)
CodeBase: file:///c:/WINDOWS/Microso ft.NET/Fra mework/v2. 0.50727/ms corlib.dll
-------------------------- ---------- ----
Commissions
Assembly Version: 1.0.0.0
Win32 Version: 1.0.0.0
CodeBase: file:///C:/Program%20Files /QCSProgs/ Commission s/Commissi ons.exe
-------------------------- ---------- ----
Microsoft.VisualBasic
Assembly Version: 8.0.0.0
Win32 Version: 8.0.50727.3053 (netfxsp.050727-3000)
CodeBase: file:///C:/WINDOWS/assembl y/GAC_MSIL /Microsoft .VisualBas ic/8.0.0.0 __b03f5f7f 11d50a3a/M icrosoft.V isualBasic .dll
-------------------------- ---------- ----
System
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.3644 (GDR.050727-3600)
CodeBase: file:///C:/WINDOWS/assembl y/GAC_MSIL /System/2. 0.0.0__b77 a5c561934e 089/System .dll
-------------------------- ---------- ----
System.Windows.Forms
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.3645 (GDR.050727-3600)
CodeBase: file:///C:/WINDOWS/assembl y/GAC_MSIL /System.Wi ndows.Form s/2.0.0.0_ _b77a5c561 934e089/Sy stem.Windo ws.Forms.d ll
-------------------------- ---------- ----
System.Drawing
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.3644 (GDR.050727-3600)
CodeBase: file:///C:/WINDOWS/assembl y/GAC_MSIL /System.Dr awing/2.0. 0.0__b03f5 f7f11d50a3 a/System.D rawing.dll
-------------------------- ---------- ----
System.Runtime.Remoting
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.3053 (netfxsp.050727-3000)
CodeBase: file:///C:/WINDOWS/assembl y/GAC_MSIL /System.Ru ntime.Remo ting/2.0.0 .0__b77a5c 561934e089 /System.Ru ntime.Remo ting.dll
-------------------------- ---------- ----
System.Data
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.3053 (netfxsp.050727-3000)
CodeBase: file:///C:/WINDOWS/assembl y/GAC_32/S ystem.Data /2.0.0.0__ b77a5c5619 34e089/Sys tem.Data.d ll
-------------------------- ---------- ----
System.Configuration
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.3053 (netfxsp.050727-3000)
CodeBase: file:///C:/WINDOWS/assembl y/GAC_MSIL /System.Co nfiguratio n/2.0.0.0_ _b03f5f7f1 1d50a3a/Sy stem.Confi guration.d ll
-------------------------- ---------- ----
System.Xml
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.3082 (QFE.050727-3000)
CodeBase: file:///C:/WINDOWS/assembl y/GAC_MSIL /System.Xm l/2.0.0.0_ _b77a5c561 934e089/Sy stem.Xml.d ll
-------------------------- ---------- ----
System.Transactions
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.3053 (netfxsp.050727-3000)
CodeBase: file:///C:/WINDOWS/assembl y/GAC_32/S ystem.Tran sactions/2 .0.0.0__b7 7a5c561934 e089/Syste m.Transact ions.dll
-------------------------- ---------- ----
System.EnterpriseServices
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.3053 (netfxsp.050727-3000)
CodeBase: file:///C:/WINDOWS/assembl y/GAC_32/S ystem.Ente rpriseServ ices/2.0.0 .0__b03f5f 7f11d50a3a /System.En terpriseSe rvices.dll
-------------------------- ---------- ----
************** JIT Debugging **************
To enable just-in-time (JIT) debugging, the .config file for this
application or computer (machine.config) must have the
jitDebugging value set in the system.windows.forms section.
The application must also be compiled with debugging
enabled.
For example:
<configuration>
<system.windows.forms jitDebugging="true" />
</configuration>
When JIT debugging is enabled, any unhandled exception
will be sent to the JIT debugger registered on the computer
rather than be handled by this dialog box.
Thanks for any help you can provide
When I run the program from another computer with out VS2010, I get the following error "System.Threading.ThreadAb
When running it on my computer I was under the impression that it was the image. Now after removing the image and running it on a client computer I have more information in the form of error messages.
Its not the image, but has something else to do with threading.
There is a full error detail I am posting below if it helps.
See the end of this message for details on invoking
just-in-time (JIT) debugging instead of this dialog box.
************** Exception Text **************
System.Threading.ThreadAbo
at System.Windows.Forms.Unsaf
at System.Windows.Forms.Unsaf
at System.Windows.Forms.Nativ
at System.Windows.Forms.Nativ
at System.Windows.Forms.Nativ
at System.Windows.Forms.Unsaf
at System.Windows.Forms.Unsaf
at System.Windows.Forms.Nativ
at System.Windows.Forms.Contr
at System.Windows.Forms.Contr
at System.Windows.Forms.Contr
at System.Windows.Forms.Contr
at System.Windows.Forms.Contr
at System.Windows.Forms.Contr
at System.Windows.Forms.Scrol
at System.Windows.Forms.Conta
at System.Windows.Forms.Form.
at System.Windows.Forms.Form.
at System.Windows.Forms.Contr
at System.Windows.Forms.Contr
at System.Windows.Forms.Nativ
************** Loaded Assemblies **************
mscorlib
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.3643 (GDR.050727-3600)
CodeBase: file:///c:/WINDOWS/Microso
--------------------------
Commissions
Assembly Version: 1.0.0.0
Win32 Version: 1.0.0.0
CodeBase: file:///C:/Program%20Files
--------------------------
Microsoft.VisualBasic
Assembly Version: 8.0.0.0
Win32 Version: 8.0.50727.3053 (netfxsp.050727-3000)
CodeBase: file:///C:/WINDOWS/assembl
--------------------------
System
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.3644 (GDR.050727-3600)
CodeBase: file:///C:/WINDOWS/assembl
--------------------------
System.Windows.Forms
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.3645 (GDR.050727-3600)
CodeBase: file:///C:/WINDOWS/assembl
--------------------------
System.Drawing
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.3644 (GDR.050727-3600)
CodeBase: file:///C:/WINDOWS/assembl
--------------------------
System.Runtime.Remoting
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.3053 (netfxsp.050727-3000)
CodeBase: file:///C:/WINDOWS/assembl
--------------------------
System.Data
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.3053 (netfxsp.050727-3000)
CodeBase: file:///C:/WINDOWS/assembl
--------------------------
System.Configuration
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.3053 (netfxsp.050727-3000)
CodeBase: file:///C:/WINDOWS/assembl
--------------------------
System.Xml
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.3082 (QFE.050727-3000)
CodeBase: file:///C:/WINDOWS/assembl
--------------------------
System.Transactions
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.3053 (netfxsp.050727-3000)
CodeBase: file:///C:/WINDOWS/assembl
--------------------------
System.EnterpriseServices
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.3053 (netfxsp.050727-3000)
CodeBase: file:///C:/WINDOWS/assembl
--------------------------
************** JIT Debugging **************
To enable just-in-time (JIT) debugging, the .config file for this
application or computer (machine.config) must have the
jitDebugging value set in the system.windows.forms section.
The application must also be compiled with debugging
enabled.
For example:
<configuration>
<system.windows.forms jitDebugging="true" />
</configuration>
When JIT debugging is enabled, any unhandled exception
will be sent to the JIT debugger registered on the computer
rather than be handled by this dialog box.
Thanks for any help you can provide
ASKER
adriankohws,
In response to your answer. I did initially try this, but could not get the animated gif to run animated. I was also trying to keep the waitform on top and was having to run a timer to check that it was on top and move it to the top if needed.
Have you tried this with success on animated Gif files and kept the waitform on top without losing it behind another form?
thanks
In response to your answer. I did initially try this, but could not get the animated gif to run animated. I was also trying to keep the waitform on top and was having to run a timer to check that it was on top and move it to the top if needed.
Have you tried this with success on animated Gif files and kept the waitform on top without losing it behind another form?
thanks
Have you considered using a backgroundworker?
ASKER
I really have very little experience with backgroundworkers and threading. What would the advantage be? do you have any code or point to a good resource?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
" I did initially try this, but could not get the animated gif to run animated. I was also trying to keep the waitform on top and was having to run a timer to check that it was on top and move it to the top if needed."
I kind of agreed, I tried that before, it's moving, but not very smooth.
I kind of agreed, I tried that before, it's moving, but not very smooth.
If the animated GIF is not animating smoothly (or at all) it means you haven't properly moved the "work" to a background thread...
So before you execute whatever you need to execute, show that form and perform an
Application.DoEvents(). Once the executions are done, then you display message and then close the form.