Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Empfohlene Antworten

Veröffentlicht

Hallo ihr Lieben,

ich möchte eine Eigenschaft eines Objektes über einen Thread zyklisch mit Daten füllen. Aus dem Formular möchte ich dann gern auf diese Daten zugreifen. Wie bekomme ich das denn am elegantesten synchronisiert?

Das Objekt selber wird im Formularthread (Hauptthread) erzeugt.

Mit OnChanged-Events ist das kein Problem, aber ich dachte eher daran, dass das Formular zyklisch per Timer aufgefrischt wird.

Wenn ich jetzt aber zu einem ungünstigen Zeitpunkt (während das Objekt vom Thread gefüllt wird), vom Formular die Eigenschaft des Objektes abfrage, kann dann doch ein nicht definiertes Ergebnis rauskommen.

Reicht das, wenn ich um Get/Set der Eigenschaft ein lock{} baue?

Edit: "Invoke im Formularcode möchte ich gern umgehen, da das ganze in eine DLL ausgelagert werden soll"

Bearbeitet von steinadler

Vergiss den ganzen Kram und beschäftige Dich mit Download Details - Microsoft Download Center - Visual Studio Async CTP

Unter der kommenden .NET-Version wird das alles ein wenig einfacher :]

Ansonsten "Producer-Consumer" ist schon das richtige Pattern.


class Producer
{
Container con;

public void Execute()
{
Thread myThread = new Thread(()=>DoAction());
myThread.IsBackground = true;
myThread.Start();
}

public Producer(Container Con)
{
con = Con;
}

void DoAction()
{
Random rng = new Random(0);
for (int i = 0; i < 10; i++)
{
Console.WriteLine("Producing {0}", i);
lock (con.ContainerLock)
{
con.FertigeFirmen.Enqueue(System.Guid.NewGuid());
Monitor.Pulse(con.ContainerLock);
}
Thread.Sleep(rng.Next(2000));
}
}

}

class Consumer
{

Container con;

public void Execute()
{
Thread myThread = new Thread(() => DoAction());
myThread.IsBackground = true;
myThread.Start();
}

private void DoAction()
{
Random rng = new Random(1);
for (int i = 0; i < 10; i++)
{
Console.WriteLine("Consuming {0}", i);
lock (con.ContainerLock)
{
while (con.FertigeFirmen.Count==0) Monitor.Wait(con.ContainerLock);
Console.WriteLine(con.FertigeFirmen.Dequeue());
}
Thread.Sleep(rng.Next(3500));
}
}

public Consumer(Container Con)
{
con = Con;
}
}

class Container
{
public Queue<Guid> FertigeFirmen = new Queue<Guid>();

public readonly object ContainerLock = new object();

}

class Program
{
static void Main(string[] args)
{
Container container = new Container();
Producer producer = new Producer(container);
Consumer consumer = new Consumer(container);
Console.WriteLine("Start!");
producer.Execute();
consumer.Execute();

Console.ReadKey();

}
}
[/php]

Mal so als Anregung.

Wenn's ein wenig komplexer sein darf: NServiceBus - The most popular open-source service bus for .net zur asynchronen Kommunikation könnte interessant sein - kommt auf den Anwendungsfall an*g*

Erstelle ein Konto oder melde dich an, um einen Kommentar zu schreiben.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.