0

I'm using vb.net in visual basic 2008. I'm wondering how perfom sequential tasks using vb.net ??

Example :

Do Task 1 then Move to Task 2 when Task 1 is complete 

Any idea Please ? Which things and tools I have to use to solve this problem ?

Dan Puzey
  • 33,626
  • 4
  • 73
  • 96
  • This is the way a program runs anyway: in the order you specify the instructions. Perhaps you need to explain in more detail what you're doing - your question suggests that you're running multi-threaded code, but you don't make this clear. – Dan Puzey Oct 23 '12 at 14:54
  • This is a very vague question. What are your tasks doing exactly? Accessing a database? Doing calculations? Generally speaking in .Net things *will happen sequentially* unless you do work to make things execute simultaneously. – El Ronnoco Oct 23 '12 at 14:55

1 Answers1

1

.NET framework has a Task class, which supports Task continuation. You would use Task.ContinueWith to chain them together. Here is a code sample:

Imports System.Threading.Tasks

Public Class Form1
  Dim _var1 As Integer = 0

  Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    Dim task1 As task = Task.Factory.StartNew(AddressOf Task1_Code).ContinueWith(AddressOf Task2_Code)
    task1.Wait()
    MessageBox.Show(_var1)
  End Sub

  Sub Task1_Code()
    Threading.Thread.Sleep(1000)
    _var1 = 5
  End Sub

  Sub Task2_Code()
    Threading.Thread.Sleep(1000)
    _var1 *= 5
  End Sub
End Class
Victor Zakharov
  • 25,801
  • 18
  • 85
  • 151