Hallo
Ich hab da ein Programm das zwei Projekte gleichzeitig startet und diese sollen abwechselnt etwas machen.(von 1 bis 100 zählen wobei das eine Projekt die geraden und das andere die ungeraden schreibt) Leider funktioniert das nicht wirklich. Nach einiger Zeit laufen sie überhaupt nicht mehr synchron.
Programm 1:
Imports System.Threading
Module OddNumbers
Sub Main()
Console.WriteLine("This process outputs odd numbers.")
Dim myMutex As New System.Threading.Mutex(True, "Global\Feichtinger")
Try
For i As Integer = 1 To 99 Step 2 'handle odd numbers
Console.WriteLine(i) 'output the next odd number
Thread.Sleep(1000) 'wait 1 sec
myMutex.ReleaseMutex() 'release the other thread
myMutex.WaitOne() 'wait for the other thread; get ownership of mutex
Next
Catch ex As Exception
Console.WriteLine(ex.Message)
Finally
myMutex.Close()
End Try
Console.WriteLine("Odd numbers - done.")
Console.ReadLine()
End Sub
End Module
Programm 2:
Imports System.Threading
Module EvenNumbers
Sub Main()
Console.WriteLine("This process outputs even numbers.")
Dim myMutex As New System.Threading.Mutex(False, "Global\Feichtinger")
Try
For i As Integer = 2 To 100 Step 2 'handle even numbers
myMutex.WaitOne() 'wait for the other thread; get ownership of mutex
Console.WriteLine(i) 'output the next odd number
myMutex.ReleaseMutex() 'release the other thread
Next
Catch ex As Exception
Console.WriteLine(ex.Message)
Finally
myMutex.Close()
End Try
Console.WriteLine("Even numbers - done.")
Console.ReadLine()
End Sub
End Module
Ich glaube das es daran liegt, dass es sein kann das ein Projekt sein eigenes Release bekommt weil das andere inzwischen nicht drankommt. Aber wie kann man es dann perfekt synchronisieren?
Danke im Voraus
Zerobias