[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 213
  • Last Modified:

display a modal form but keep the underlying thread processing

I have a timer in a form that triggers some processing functionality
When that gets triggered it displays a message to the user
So if the timer elapses a bunch of times i only want 1 message displayed on the screen at any time
When the message is displayed i need is to be modal
This is kind of weird but i need a modal messagebox plus i need a way to continue processing in the underlying thread
Its like a modal asynchronous call
i have no sample code
Any advice on how i could do this?
0
glenn_r
Asked:
glenn_r
1 Solution
 
MlandaTCommented:
I have a timer in a form that triggers some processing functionality
Make sure you are using a System.Threading.Timer and not a WinForms.Timer. The Threading.Time runs on a seperate thread from the UI. This will keep it running even when there is a modal UI displayed on the GUI.

So if the timer elapses a bunch of times i only want 1 message displayed on the screen at any time
Create a form level variable and give it the reference to the modal popup. The Threading.Timer will now fire a handler on a separate thread (never mind that the timer handler is sitting within the same class and file as your form :) ). In the handler, you must use an Invoke to check if the form is already displayed and handle that. Something like:
Private STTimer As System.Threading.Timer
Private NotificationForm As NotificationFormClass

Private Sub btnStart_Click(sender As Object, e As RoutedEventArgs)

	Me.STTimer = New Timer(AddressOf Me.TimerElapsed, Nothing, 1000, System.Threading.Timeout.Infinite)

End Sub

Private Sub TimerElapsed(state As Object)

	Dim message As String = String.Format("Tick Generated from {0}", Thread.CurrentThread.Name)
	Me.Dispatcher.Invoke(DirectCast(Sub() 
               'Code to check whether to display modal form or not
               If NotificationForm IsNot Nothing Then 
                      NotificationForm = New NotificationFormClass
                      NotificationForm.ShowDialog
                      'do what you want with the result etc etc
                      NotificationForm = Nothing
               End If
        End Sub, Action))

End Sub

Open in new window


These articles might also give you some further insights:
http://www.abhisheksur.com/2011/03/all-about-net-timers-comparison.html
http://msdn.microsoft.com/en-us/magazine/cc164015.aspx#S4
0
 
glenn_rAuthor Commented:
i pasted the code into a sample winforms form and am having an error with the following line "Me.Dispatcher.Invoke(DirectCast(Sub()" it cannot find the dispatcher or namespace. I pasted this within a form class. thanks
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now