Module mit Timer

  • VB.NET

Es gibt 4 Antworten in diesem Thema. Der letzte Beitrag () ist von Quakxi.

    Module mit Timer

    Hallo,
    ich bastle momentan an einer Möglichkeit Anwendungen mit der vbc.exe zu kompilieren.
    Momentan bin ich so weit:

    VB.NET-Quellcode

    1. Imports System.Windows.Forms
    2. Imports System.Runtime.InteropServices
    3. Imports System.IO
    4. Module Module1
    5. Private i As Integer = 0
    6. Friend WithEvents Timer1 As New System.Windows.Forms.Timer
    7. Sub Main()
    8. MsgBox("hallo")
    9. Timer1.Interval = 10
    10. Timer1.Enabled = True
    11. While True
    12. End While
    13. End Sub
    14. Public Sub Timer1_Tick_1(sender As Object, e As EventArgs) Handles Timer1.Tick
    15. MsgBox("hallo")
    16. i += 1
    17. File.WriteAllText("C:\TEMP\" & i & ".txt", "Hallo" & i)
    18. End Sub
    19. End Module


    Es gibt nun zwei Probleme, zum einen wird die Timer1_Tick Sub nicht aufgerufen, zum anderen beendet sie die Anwendung automatisch. Dies lässt sich nur mit der While True unterbinden, da es keine Console gibt und ich auch keine möchte, kann ich kein Console.ReadLine() Verwenden.
    Die Anwendung wird wie folgt kompiliert:

    Quellcode

    1. Shell(Environ("systemroot") & "\Microsoft.NET\Framework\v3.5\vbc.exe /target:winexe C:\TEMP\Blubb.vb", AppWinStyle.Hide)

    Hat jemand einen Tip, wie die beiden gennanten Probleme unterbunden werden könnten?
    Intel i7-4710HQ |Nvidia GTX 860M | 1TB SSHD| 8GB RAM 1600Mhz :saint:
    Intel Core Duo2 | 320GB | 4 GB RAM | Linux Debian :D
    AMD E-350 | 320GB| 6GB RAM :thumbdown:
    @Quakxi Warum bastelst Du einen System.Windows.Forms.Timer, der speziell für System.Windows.Forms gemacht ist, in eine Console-Applikation?
    Klar muss die Console ausgebremst werden, wenn der Timer zuschlagen soll.
    Bevor Du da iwas compilierst, sollte es im Studio korrekt funktionieren.
    Damit Dein Plan aber korrekt funktioniert, mach Dir lieber eine WinForms-Anwendung, die unsichtbar ist (Opacity = 0).
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!
    Das hab ich mir auch schon überlegt, und habe dazu folgenden Code gehabt:

    VB.NET-Quellcode

    1. Imports System.Windows.Forms
    2. Imports System.Runtime.InteropServices
    3. Module Module1
    4. Sub Main()
    5. Dim frm As New Form1
    6. frm.InitializeComponent()
    7. frm.ShowDialog()
    8. Console.Readline()
    9. While True
    10. End While
    11. End Sub
    12. End Module
    13. Public Class Form1
    14. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    15. MsgBox("Hallo")
    16. Me.Visible = False
    17. Me.Hide()
    18. End Sub
    19. Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    20. End Sub
    21. End Class
    22. Partial Class Form1
    23. Inherits System.Windows.Forms.Form
    24. Public Sub h()
    25. Me.Hide()
    26. End Sub
    27. 'Das Formular überschreibt den Löschvorgang, um die Komponentenliste zu bereinigen.
    28. '<System.Diagnostics.DebuggerNonUserCode()> _
    29. 'Wird vom Windows Form-Designer benötigt.
    30. Private components As System.ComponentModel.IContainer
    31. 'Hinweis: Die folgende Prozedur ist für den Windows Form-Designer erforderlich.
    32. 'Das Bearbeiten ist mit dem Windows Form-Designer möglich.
    33. 'Das Bearbeiten mit dem Code-Editor ist nicht möglich.
    34. '<System.Diagnostics.DebuggerStepThrough()> _
    35. Public Sub InitializeComponent()
    36. Me.Button1 = New System.Windows.Forms.Button()
    37. Me.SuspendLayout()
    38. '
    39. 'Button1
    40. '
    41. Me.Button1.Location = New System.Drawing.Point(81, 151)
    42. Me.Button1.Name = "Button1"
    43. Me.Button1.Size = New System.Drawing.Size(75, 23)
    44. Me.Button1.TabIndex = 0
    45. Me.Button1.Text = "Button1"
    46. Me.Button1.UseVisualStyleBackColor = True
    47. '
    48. 'Form1
    49. '
    50. Me.AutoScaleDimensions = New System.Drawing.SizeF(8.0!, 16.0!)
    51. Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
    52. Me.ClientSize = New System.Drawing.Size(282, 253)
    53. Me.Controls.Add(Me.Button1)
    54. Me.Name = "Form1"
    55. Me.Text = "Form1"
    56. Me.ResumeLayout(False)
    57. End Sub
    58. Friend WithEvents Button1 As Button
    59. End Class

    Aber hier hab ich das selbe Problem, sobald die Prozedur in der Sub Main() abgelaufe ist beendet sich wieder mein Programm.
    Intel i7-4710HQ |Nvidia GTX 860M | 1TB SSHD| 8GB RAM 1600Mhz :saint:
    Intel Core Duo2 | 320GB | 4 GB RAM | Linux Debian :D
    AMD E-350 | 320GB| 6GB RAM :thumbdown:
    @Quakxi Mach mal Deine Klasse Program und die Prozedur Main so:

    VB.NET-Quellcode

    1. Imports System
    2. Imports System.Diagnostics
    3. Imports System.Globalization
    4. Imports System.Threading
    5. Imports System.Windows.Forms
    6. Module Module1
    7. Sub Main()
    8. ' standard settings
    9. Application.EnableVisualStyles()
    10. Application.SetCompatibleTextRenderingDefault(False)
    11. Application.Run(New Form1)
    12. End Sub
    13. End Module

    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!
    Danke jetzt funktionierts mit folgenden Code:

    VB.NET-Quellcode

    1. Imports System
    2. Imports System.IO
    3. Imports System.Windows.Forms
    4. Imports System.Diagnostics
    5. Imports System.Globalization
    6. Imports System.Threading
    7. Imports System.Runtime.InteropServices
    8. Module Module1
    9. Sub Main()
    10. Application.EnableVisualStyles()
    11. Application.SetCompatibleTextRenderingDefault(False)
    12. Dim ding As New Form1
    13. ding.InitializeComponent()
    14. Application.Run(ding)
    15. End Sub
    16. End Module
    17. Public Class Form1
    18. Inherits System.Windows.Forms.Form
    19. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    20. MsgBox("Hallo")
    21. Me.Hide
    22. End Sub
    23. Private components As System.ComponentModel.IContainer
    24. Public Sub InitializeComponent()
    25. Me.Button1 = New System.Windows.Forms.Button()
    26. Me.SuspendLayout()
    27. '
    28. 'Button1
    29. '
    30. Me.Button1.Location = New System.Drawing.Point(81, 151)
    31. Me.Button1.Name = "Button1"
    32. Me.Button1.Size = New System.Drawing.Size(75, 23)
    33. Me.Button1.TabIndex = 0
    34. Me.Button1.Text = "Button1"
    35. Me.Button1.UseVisualStyleBackColor = True
    36. '
    37. 'Form1
    38. '
    39. Me.AutoScaleDimensions = New System.Drawing.SizeF(8.0!, 16.0!)
    40. Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
    41. Me.ClientSize = New System.Drawing.Size(282, 253)
    42. Me.Controls.Add(Me.Button1)
    43. Me.Name = "Form1"
    44. Me.Text = "Form1"
    45. Me.ResumeLayout(False)
    46. End Sub
    47. Friend WithEvents Button1 As Button
    48. End Class
    Intel i7-4710HQ |Nvidia GTX 860M | 1TB SSHD| 8GB RAM 1600Mhz :saint:
    Intel Core Duo2 | 320GB | 4 GB RAM | Linux Debian :D
    AMD E-350 | 320GB| 6GB RAM :thumbdown: