Autor Beitrag
Christoph1972
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 690
Erhaltene Danke: 16


VS2015 Pro / C# & VB.Net
BeitragVerfasst: So 18.01.09 22:40 
Hallo zusammen,


ich habe eine Klasse, in dieser empfange ich Daten vom Serial Port. Die Daten übergebe ich an meine Main Form, dazu ist ein Invoke nötig, was auch funktioniert. Aber wie kann ich es erreichen, das ich die Daten schon Threadsicher aus der Klasse übergebe? Also, dass das Invoke schon in der Klasse passiert. Aber in der Klasse ist Invoke oder InvokeRequired ja nicht verfügbar. Gibt es eine Möglichkeit das zu erreichen? Ich würde es einfach sauberer finden, wenn die Logic dazu in der Klasse ist.


Gruß
Christoph
Kha
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 3803
Erhaltene Danke: 176

Arch Linux
Python, C, C++ (vim)
BeitragVerfasst: Mo 19.01.09 12:58 
user profile iconChristoph1972 hat folgendes geschrieben Zum zitierten Posting springen:
Ich würde es einfach sauberer finden, wenn die Logic dazu in der Klasse ist.
Die Synchronisierung ist Aufgabe der GUI, mit der deine Klasse nichts zu tun hat. Ein asynchrones Event ist also kein Designfehler, SerialPort und viele andere Klassen machen es ja auch so.

_________________
>λ=
Ralf Jansen
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 4708
Erhaltene Danke: 991


VS2010 Pro, VS2012 Pro, VS2013 Pro, VS2015 Pro, Delphi 7 Pro
BeitragVerfasst: Mo 19.01.09 13:33 
Die Datenübergabe erfolgt per Event?
Dann könntest du den Event schon so ausführen das der EventHandler im Threadcontext des jeweiligen Subscribers ausgeführt wird.

ausblenden volle Höhe C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
private readonly object myEventLock = new object();
private EventHandler myEvent;

public event EventHandler MyEvent
{
    add
    {
        lock (myEventLock)
        {
            myEvent += value;
        }
    }
    remove
    {
        lock (myEventLock)
        {
            myEvent -= value;
        }
    }
}

protected void OnMyEvent(EventArgs args)
{
    lock (myEventLock)
    {
        foreach (EventHandler eventHandler in myEvent.GetInvocationList())
        {
            ISynchronizeInvoke target = eventHandler.Target as ISynchronizeInvoke;

            if ((target != null) && (target.InvokeRequired))
            {
                target.Invoke((MethodInvoker)delegate()
                {
                    eventHandler(this, args);
                }, null);
            }
            else
                eventHandler(this, args);
        }
    }
}



Ich würde trotzdem aber eher Kah zustimmen. Synchronisierung ist Aufgabe des Empfängers(hier GUI). Nur der Empfänger kann letztlich entscheiden ob eine (teure) Synchronisierung überhaupt notwendig ist.
Christoph1972 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 690
Erhaltene Danke: 16


VS2015 Pro / C# & VB.Net
BeitragVerfasst: Mo 19.01.09 18:03 
OK, vielen Dank an euch. Ich werde es dann in der Main Form machen.



Gruß
Christoph