Link to home
Create AccountLog in
Avatar of mcsdguyian
mcsdguyianFlag for United States of America

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(AddressOf 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_intTop, m_intLeft)
            objPlsWait.SetMessage(System.Threading.Thread.CurrentThread.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.WaitProcessing1
    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
Avatar of adriankohws
adriankohws
Flag of Singapore image

I would suggest you just create a physical form for the waitscreen with the animated GIF.
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.
Avatar of Mike Tomlinson
Nothing in the code you posted pops out as being a potential cause for that error.

What are you doing in " '---:[Run Query]:---"?
Avatar of mcsdguyian

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?
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.ThreadAbortException: 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.ThreadAbortException: Thread was being aborted.
   at System.Windows.Forms.UnsafeNativeMethods.SetWindowLongPtr32(HandleRef hWnd, Int32 nIndex, WndProc wndproc)
   at System.Windows.Forms.UnsafeNativeMethods.SetWindowLong(HandleRef hWnd, Int32 nIndex, WndProc wndproc)
   at System.Windows.Forms.NativeWindow.AssignHandle(IntPtr handle, Boolean assignUniqueID)
   at System.Windows.Forms.NativeWindow.AssignHandle(IntPtr handle)
   at System.Windows.Forms.NativeWindow.WindowClass.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.IntCreateWindowEx(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.UnsafeNativeMethods.CreateWindowEx(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.NativeWindow.CreateHandle(CreateParams cp)
   at System.Windows.Forms.Control.CreateHandle()
   at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
   at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
   at System.Windows.Forms.Control.CreateControl()
   at System.Windows.Forms.Control.WmShowWindow(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
   at System.Windows.Forms.ContainerControl.WndProc(Message& m)
   at System.Windows.Forms.Form.WmShowWindow(Message& m)
   at System.Windows.Forms.Form.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr 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/Microsoft.NET/Framework/v2.0.50727/mscorlib.dll
----------------------------------------
Commissions
    Assembly Version: 1.0.0.0
    Win32 Version: 1.0.0.0
    CodeBase: file:///C:/Program%20Files/QCSProgs/Commissions/Commissions.exe
----------------------------------------
Microsoft.VisualBasic
    Assembly Version: 8.0.0.0
    Win32 Version: 8.0.50727.3053 (netfxsp.050727-3000)
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/Microsoft.VisualBasic/8.0.0.0__b03f5f7f11d50a3a/Microsoft.VisualBasic.dll
----------------------------------------
System
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.3644 (GDR.050727-3600)
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System/2.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
System.Windows.Forms
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.3645 (GDR.050727-3600)
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Windows.Forms/2.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System.Drawing
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.3644 (GDR.050727-3600)
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Drawing/2.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
System.Runtime.Remoting
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.3053 (netfxsp.050727-3000)
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Runtime.Remoting/2.0.0.0__b77a5c561934e089/System.Runtime.Remoting.dll
----------------------------------------
System.Data
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.3053 (netfxsp.050727-3000)
    CodeBase: file:///C:/WINDOWS/assembly/GAC_32/System.Data/2.0.0.0__b77a5c561934e089/System.Data.dll
----------------------------------------
System.Configuration
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.3053 (netfxsp.050727-3000)
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Configuration/2.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll
----------------------------------------
System.Xml
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.3082 (QFE.050727-3000)
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Xml/2.0.0.0__b77a5c561934e089/System.Xml.dll
----------------------------------------
System.Transactions
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.3053 (netfxsp.050727-3000)
    CodeBase: file:///C:/WINDOWS/assembly/GAC_32/System.Transactions/2.0.0.0__b77a5c561934e089/System.Transactions.dll
----------------------------------------
System.EnterpriseServices
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.3053 (netfxsp.050727-3000)
    CodeBase: file:///C:/WINDOWS/assembly/GAC_32/System.EnterpriseServices/2.0.0.0__b03f5f7f11d50a3a/System.EnterpriseServices.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
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
Have you considered using a backgroundworker?
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
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
" 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.
If the animated GIF is not animating smoothly (or at all) it means you haven't properly moved the "work" to a background thread...