Results 1 to 5 of 5

Thread: Building a Windows program, possibly a service

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2002
    Posts
    103

    Question Building a Windows program, possibly a service

    I have been thinking about this for a while, have asked for help here (and received it), and have tried various things, but nothing is a) completely working and b) as simple to implement as I imagined. Here’s the ‘entire’ problem:

    The user on a Windows 7 PC has a file with ‘quotes’ in it. Every time the computer comes out of standby mode, a randomly selected quote from the file should be displayed on the screen for about 10 seconds. Yeah, sounds trivially easy…

    I was able to create a service that wrote a message to a log file each time the computer comes out of standby, but was told to look at this https://docs.microsoft.com/en-au/dot...tframework-4.8 to make a service that can display something. This is quite involved! I was also able to write a program that simply writes a string to the screen, without using a form. So, I seem to have the basic components down – separate from each other…

    So, any guidance, pointers, etc. that you can share for putting it all together would be greatly appreciated. Thanks.

  2. #2
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,807

    Re: Building a Windows program, possibly a service

    Could you post what you already made?

  3. #3

    Thread Starter
    Lively Member
    Join Date
    May 2002
    Posts
    103

    Re: Building a Windows program, possibly a service

    The code for the service come from here, almost exactly: https://docs.microsoft.com/en-us/dot...onent-designer

    The code for program to show the 'quote' is as follows (and is far from exciting and still needs a bit of tweaking):
    Code:
    Imports Microsoft.Win32
    
    Module Program
        Public Sub Main(ByVal args() As String)
            Randomize()
    
            Dim myStrForm As New StringFormat()
            myStrForm.Alignment = StringAlignment.Center
            myStrForm.LineAlignment = StringAlignment.Center
    
            'file layout: Text 1, Line 1; Text 1, line 2, blank: Text 2, Line 1; Text 2, line 2, blank: etc.
            Dim strLC As String() = IO.File.ReadAllLines("QuoteFile.txt")
            Dim iRnd As Integer = Rnd() * CInt(strLC.Length / 3 - 1)
    
            Dim PrintMe1 As String = strLC(iRnd * 3)
            Dim PrintMe2 As String = strLC(iRnd * 3 + 1)
    
            Dim myFont As New Font("Calibri", 100, FontStyle.Bold)
            Dim myGraph As Graphics = Graphics.FromHwnd(IntPtr.Zero)
    
            Dim tWid As Integer = CInt(0.94 * Screen.PrimaryScreen.Bounds.Width)
            Dim tOff As Integer = CInt((Screen.PrimaryScreen.Bounds.Width - tWid) / 2)
            myGraph.DrawString(PrintMe1, myFont, Brushes.Green, New Rectangle(tOff, 100, tWid, 1000), myStrForm)
            myGraph.DrawString(PrintMe2, myFont, Brushes.Orange, New Rectangle(80, 500, 2400, 1000), myStrForm)
    
            Threading.Thread.Sleep(4000)
            myGraph.Dispose()
        End Sub
    End Module

  4. #4
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,116

    Re: Building a Windows program, possibly a service

    Creating a service for this seems like a bit of overkill. There's no reason for this program to be running all the time and only do the thing it's designed to do (display a message) once in a great while.

    What I would do is forget the service stuff - just get a program working that displays the message the way you want to display it when the program is ran. Then, set up a Scheduled Task that will run your program, where the Trigger for the task is that the computer has come out of sleep mode.

    There is some guidance on how to set up a task this way here:

    https://superuser.com/questions/8444...leep-hibernate

    Good luck.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    May 2002
    Posts
    103

    Question Re: Building a Windows program, possibly a service

    OptionBase1 - thanks for the suggestion. Your idea should get the job done. Note 'should'...

    I ran the program, essentially that which is shown above (I added a 10 second sleep at the beginning and a InvalidateRect to the end) and it ran just as expected.

    I then created a custom task that runs when the computer wake up (following the guidance here: https://sumtips.com/how-to/run-progr...eep-hibernate/). I then suspended my computer and woke it up and sadly, the program did not run. Instead, I got an Error message of type 'Problem Event Name: CLR20r3'. Looking this up, I am not sure a) how this could be relevant or b) exactly how to resolve the issue.

    Any leads would be appreciated!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width