0

I am using some tasks to do some operation and meantime i am showing user additional 'waiting form' then when task's function is finished welcome form s closing from the task. I have already solution and it was discussed in this topic: enter link description here

Now the problem is i wanted to implement it in some other place and i face issue that my waiting form is still on place (not being closed) and i have no idea why. What i can say is i checked if function is retreiving value - it is for sure. I suppose that this is because function is finishing before Welcome form is showing up therefore it stacks... Is there anyway to check if that the case? That's the code:

 Dim pic As New Waiting

                Dim tsk As Task(Of Boolean) = Task.Factory.StartNew(Function()
                                                                        '--Run lenghty task
                                                                        '--Show the form
                                                                        pic.ShowDialog()
                                                                        Dim retValue As Boolean = THIS_UpdateTransport()
                                                                        '--Close form once done (on GUI thread)
                                                                        pic.Invoke(New Action(Sub() pic.Close()))
                                                                        Return retValue
                                                                    End Function)


                '--Show the form
                pic.ShowDialog()
                Task.WaitAll(tsk

)

Community
  • 1
  • 1
  • You could change `THIS_UpdateTransport()` to an async function returning `Task(Of Boolean)` and `await` it in your lambda expression. – Alex B. Dec 30 '15 at 11:53
  • @Alex B. Could you please show me as an answer how dhould i do it properly? –  Dec 30 '15 at 12:08
  • Do you really need to show the waiting form as a modal dialog box? Like already answered in your linked question `ShowDialog()` blocks the current Thread until its closed. So you have to show it after you execute your long running task. But this implies that you try to close the form before it has beeing shown -> deadlock. If you use `Show` instead your problem can be solved easily. – Alex B. Dec 30 '15 at 12:48
  • the purpose is i want to block parent. How to do it using async/await could you be so kind and show. P.S Why then in other places this problem not occuring what could be the cause here? –  Dec 30 '15 at 12:54
  • I´m sorry the async/await approach does not change the problem for the ShowDialog problem. Without seeing your working code it´s hard to determine any differences ;) I´ll try to give you an example where the parent is blocked but shows the waiting as form. – Alex B. Dec 30 '15 at 13:02

1 Answers1

0

Here is my answer using .Show() instead of ShowDialog() with blocking Main Form while long task is running:

  1. Changed THIS_UpdateTransport toTHIS_UpdateTransportAsync

       Private Function THIS_UpdateTransportAsync() As Task(Of Boolean)
            'Do some really important work
            Return Task.Factory.StartNew(Function()
                                                 Threading.Thread.Sleep(5000)
                                                 Return True
                                         End Function)
        End Function
    
  2. Declaring the UI method invoking the Task as async (button click in my example):

    Private Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim pic As New Waiting()
        'Block Parent Form from all user interactions
        Me.Enabled = False         
        'Use Show() instead of ShowDialog() to avoid Thread blocking
        pic.Show(Me) 
    
        'Await the long running Task until its done
        Dim result As Boolean = Await THIS_UpdateTransportAsync()
    
        'Close Waiting window
        pic.Close()
        'Reactivating Parent Form and enable use interactions
        Me.Activate()
        Me.Enabled = True
    End Sub
    

So this worked for me and some example forms + controls.

Alex B.
  • 2,145
  • 1
  • 16
  • 24