display a modal form but keep the underlying thread processing

Posted on 2014-08-28
Last Modified: 2014-10-24
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?
Question by:glenn_r
    LVL 30

    Accepted Solution

    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)
                   'Code to check whether to display modal form or not
                   If NotificationForm IsNot Nothing Then 
                          NotificationForm = New NotificationFormClass
                          '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:

    Author Comment

    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

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
    A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    728 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

    20 Experts available now in Live!

    Get 1:1 Help Now