Solved

.Net Threading Wait/Busy Form

Posted on 2013-06-07
11
715 Views
Last Modified: 2013-07-12
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
0
Comment
Question by:mcsdguyian
  • 4
  • 3
  • 2
  • +1
11 Comments
 
LVL 10

Expert Comment

by:adriankohws
ID: 39231050
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.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 39231614
Nothing in the code you posted pops out as being a potential cause for that error.

What are you doing in " '---:[Run Query]:---"?
0
 

Author Comment

by:mcsdguyian
ID: 39235166
I am just executing some SQL stored procedures that do some long database updates.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 39238957
Does the error go away if you do not use the image?
0
 

Author Comment

by:mcsdguyian
ID: 39239359
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
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:mcsdguyian
ID: 39239389
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
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 39239409
Have you considered using a backgroundworker?
0
 

Author Comment

by:mcsdguyian
ID: 39239476
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?
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 39239535
Advantage would be getting rid of threading issues

http://www.java2s.com/Code/VB/GUI/BackgroundWorkerDemo.htm
0
 
LVL 10

Expert Comment

by:adriankohws
ID: 39241140
" 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.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 39241354
If the animated GIF is not animating smoothly (or at all) it means you haven't properly moved the "work" to a background thread...
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now