Autor Beitrag
lapadula
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 180
Erhaltene Danke: 10



BeitragVerfasst: Fr 25.05.18 15:41 
Hallo, ich habe hier eine Software vor mir, die ich zum laufen kriegen muss.

Jetzt gibt es hier eine Methode wo ich nicht ganz verstehe was diese macht, kann mir jemand den Sinn dahinter erklären, sofern es überhaupt einen gibt.

ausblenden volle Höhe 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:
Public Shared Sub watchdog()
            Dim counter2 As New PerformanceCounter With { _
                .CategoryName = "Processor", _
                .CounterName = "% Processor Time", _
                .InstanceName = "_Total" _
            }
            Dim counter As New PerformanceCounter With { _
                .CategoryName = "Process", _
                .CounterName = "% Processor Time", _
                .InstanceName = "friedaNKF" _
            }
            Dim currentProcess As Process = Process.GetCurrentProcess
            Do While (NKFProcess.m_strResult Is Nothing)
                If (((counter2.NextValue > 70!) AndAlso (counter.NextValue < 70!)) AndAlso (currentProcess.PriorityClass <> ProcessPriorityClass.BelowNormal)) Then
                    Thread.Sleep(&H7D0)
                    If ((counter2.NextValue > 70!) AndAlso (counter.NextValue < 70!)) Then
                        currentProcess.PriorityClass = ProcessPriorityClass.BelowNormal
                    End If
                ElseIf (((counter2.NextValue > 95!) AndAlso (counter.NextValue < 95!)) AndAlso (currentProcess.PriorityClass <> ProcessPriorityClass.Idle)) Then
                    Thread.Sleep(&H7D0)
                    If ((counter2.NextValue > 95!) AndAlso (counter.NextValue < 95!)) Then
                        currentProcess.PriorityClass = ProcessPriorityClass.Idle
                    End If
                ElseIf (((counter2.NextValue < 50!) AndAlso (counter.NextValue < 50!)) AndAlso (currentProcess.PriorityClass <> ProcessPriorityClass.Normal)) Then
                    Thread.Sleep(&H7D0)
                    If ((counter2.NextValue < 50!) AndAlso (counter.NextValue < 50!)) Then
                        currentProcess.PriorityClass = ProcessPriorityClass.Normal
                    End If
                End If
                Thread.Sleep(300)
            Loop
        End Sub


Hier ist eine Main-Mathode an die Argumente übergeben werden:

ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
<STAThread>
        Public Shared Sub Main(ByVal pv_args As String())
            Dim thread As New Thread(New ParameterizedThreadStart(AddressOf NKFProcess.nkf))
            thread.SetApartmentState(ApartmentState.MTA)
            thread.Start(pv_args)
            Dim thread2 As New Thread(New ThreadStart(AddressOf NKFProcess.watchdog))
            thread2.SetApartmentState(ApartmentState.MTA)
            thread2.Start()
        End Sub


ausblenden 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:
Public Shared Sub nkf(ByVal args As Object)
            Dim str10 As String = Conversions.ToString(NewLateBinding.LateIndexGet(args, New Object() { 0 }, Nothing))
            Dim str12 As String = Conversions.ToString(NewLateBinding.LateIndexGet(args, New Object() { 1 }, Nothing))
            Dim str13 As String = Conversions.ToString(NewLateBinding.LateIndexGet(args, New Object() { 2 }, Nothing))
            Dim str3 As String = Conversions.ToString(NewLateBinding.LateIndexGet(args, New Object() { 3 }, Nothing))
            Dim str5 As String = Conversions.ToString(NewLateBinding.LateIndexGet(args, New Object() { 4 }, Nothing))
            Dim str4 As String = Conversions.ToString(NewLateBinding.LateIndexGet(args, New Object() { 5 }, Nothing))
            Dim str6 As String = Conversions.ToString(NewLateBinding.LateIndexGet(args, New Object() { 6 }, Nothing))
            Dim str11 As String = Conversions.ToString(NewLateBinding.LateIndexGet(args, New Object() { 7 }, Nothing))
            Dim str7 As String = Conversions.ToString(NewLateBinding.LateIndexGet(args, New Object() { 8 }, Nothing))
            Dim str As String = Conversions.ToString(NewLateBinding.LateIndexGet(args, New Object() { 9 }, Nothing))
            Dim str8 As String = Conversions.ToString(NewLateBinding.LateIndexGet(args, New Object() { 10 }, Nothing))
            Dim str9 As String = Conversions.ToString(NewLateBinding.LateIndexGet(args, New Object() { 11 }, Nothing))
            Dim str2 As String = Conversions.ToString(NewLateBinding.LateIndexGet(args, New Object() {12}, Nothing))

            Dim user As friedaCOM.CObjUser = NKFProcess.getUserObj(str10, str12, str13, str3, str5, str4, str6)
            Try 
                NKFProcess.m_strResult = New NKFMassenberechnung().start(user, str8, Conversions.ToDate(str11), Conversions.ToInteger(str7), Conversions.ToInteger(str), NKFProcess.getBezeichnung(user, str7), NKFProcess.getBlockName(user, str), str9, str2)
                NKFProcess.saveLogToDatabase(user, NKFProcess.m_strResult, str8)
                NKFProcess.sendEMailTo(str9, user, NKFProcess.getBezeichnung(user, str7))
            Catch exception1 As Exception
                ProjectData.SetProjectError(exception1)
                Dim exception As Exception = exception1
                'New ErrorHandling().ErrorRaised(exception, user, "")
                ProjectData.ClearProjectError
            End Try
        End Sub
Th69
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Moderator
Beiträge: 4764
Erhaltene Danke: 1052

Win10
C#, C++ (VS 2017/19/22)
BeitragVerfasst: Fr 25.05.18 16:14 
So wie ich das interpretiere, wird in dem watchdog-Thread die Prozessorauslastung (sowohl gesamt als auch vom eigenen Prozess) ermittelt und dann entsprechend die eigene Prozess-Priorität gesetzt:
Größer als 70: BelowNormal
Über 95: Idle
Unter 50 Normal
Es wird dann jeweils 2s gewartet und eine erneute Messung vorgenommen, ehe die Priorität dann wirklich geändert wird.

Dies wird wohl gemacht, um das Gesamtsystem noch bedienbar zu halten, da wohl kein (direkter) Einfluß auf den Code in nkf (saveLogToDatabase, sendEMailTo) gemacht werden kann (und dieser evtl. länger läuft).


Zuletzt bearbeitet von Th69 am Fr 25.05.18 16:15, insgesamt 1-mal bearbeitet

Für diesen Beitrag haben gedankt: lapadula
Ralf Jansen
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 4700
Erhaltene Danke: 991


VS2010 Pro, VS2012 Pro, VS2013 Pro, VS2015 Pro, Delphi 7 Pro
BeitragVerfasst: Fr 25.05.18 16:15 
Da möchte jemand scheinbar seine CPU Benutzung anhand der aktuellen Systemauslastung steuern und im Auslastungsfall die eigene Prio runterdrehen.

Sieht für mich eher problematisch aus. BelowNormal Priorität ist eigentlich dem System vorbehalten und das Setzen auf diese Prio sollte knallen.
Und das setzen auf Idle hieße das dieser Prozess so ziemlich der letzte ist der jemals Prozessorzeit zugewiesen bekommt, falls dein System ausgelastet ist also quasi nie.
Ein Watchdog der möglicherweise nie dran kommt klingt falsch. Sio ein Watchdog sollte eigentlich ein anderer Prozess sein der diesen Prozess überwacht.

Nebenbei der Prozessortime Counter ist glaube ich nicht auf 100% genormt sondern ein vielfaches der Cores. Auf einem 8Kerner also bis zu 800%. Der Code stammt vermutlich aus seligen Singlecore Zeiten.

In der Main wird primär das ThreadApartment gesetzt um dann eine COM Bbliothek auszuführen. NIcht jede COM Bibliotheken läuft in jedem Apartment und MTA ist vermutlich eine Vorbedingung dieser COM Komponente.

Was dann in nkf passiert ist dann Komponenten spezifisches Voodoo. Dafür müßte man dieser spezielle Komponente kennnen. Das sieht auch eher nach reenginiertem Code aus und nicht explizit so geschrieben. Hast du dafür einen Dissambler benutzt um denn Code das aus einer Assembly zu bekommen?

Für diesen Beitrag haben gedankt: lapadula
lapadula Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 180
Erhaltene Danke: 10



BeitragVerfasst: Fr 25.05.18 20:45 
Danke für die Antworten, ich denke ich werde die Methode unterm Tisch fallen lassen :D
Bezweifele auch das es etwas bringt.

Ja der Code ist schon älter und ich habe die exe durch den .Net Reflector gejagt, weil die Sourcen abhanden gekommen sind.

Heute würde man async/await benutzen, richtig?