Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Message Box in Windows Service

Posted on 2010-08-16
11
Medium Priority
?
1,742 Views
Last Modified: 2013-11-26
I have recently picked up programming again after a few years of not really doing any. I was previously used to VB 6 coding, but have now made the jump to VB.net 2010.

I have never created a Windows Service before so am trying to create a service that interacts with the desktop in a way that will allow me to show a message box.

For my first windows service sample/test app I wanted to create a service with 1 timer that runs for 5 seconds and on the elapsed event to show a message box of the current time.

Whatever I try, whether I use a windows forms timer control or a system.timer or ticking interactive on the service propertys box, nothing seems to work and because the message box isn't showing im uncertain what the service is doing or if the timer is even working.

So the question is, how do I create a simple windows service that throws a message box up onto the screen showing the current time at each 5 or 10 second loop?
0
Comment
Question by:Terry Rogers
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
  • +1
11 Comments
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33443937
try this
Button ->btnStartStop
Label -> Min
Label ->Sec
Private Sub btnStartStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStartStop.Click
        If btnStartStop.Text = "Start" Then
            CountDownTime = Now.AddMinutes(1/12)            
            Dim ts As TimeSpan = CountDownTime.Subtract(Now)
            Min.Text = ts.Minutes.ToString
            Sec.Text = ts.Seconds.ToString
            Timer1.Start()
            btnStartStop.Text = "Stop"
        Else
            Timer1.Stop()
            btnStartStop.Text = "Start"
        End If
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If CountDownTime < Now Then
            Timer1.Stop()
            MessageBox.Show("out of time.")
            btnStartStop.Text = "Start"
        Else

            Dim ts As TimeSpan = CountDownTime.Subtract(Now)
            Min.Text = ts.Minutes.ToString
            Sec.Text = ts.Seconds.ToString

        End If
    End Sub

Open in new window

0
 
LVL 1

Author Comment

by:Terry Rogers
ID: 33443946
This is a Windows Service so I don't have any forms to put a button or label onto.
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33443974
yeap you are right!!!Trying!!
0
Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 
LVL 18

Assisted Solution

by:John (Yiannis) Toutountzoglou
John (Yiannis) Toutountzoglou earned 664 total points
ID: 33443985
0
 
LVL 48

Expert Comment

by:Wayne Taylor (webtubbs)
ID: 33444066
To show a MessageBox from a Service, you need to use the "ServiceNotification" option....

    System.Windows.Forms.MessageBox.Show("Your Message", _
                                                                         "Caption", _
                                                                         System.Windows.Forms.MessageBoxButtons.OK, _
                                                                         System.Windows.Forms.MessageBoxIcon.Information, _
                                                                         System.Windows.Forms.MessageBoxDefaultButton.Button1, _
                                                                         System.Windows.Forms.MessageBoxOptions.ServiceNotification)

Wayne
0
 
LVL 1

Author Comment

by:Terry Rogers
ID: 33444164
Hmmm, getting closer, I can now see the timer event does first as I was able to put some code that logged a message each time it fired, however it still wont display a message box.

My code as it stands is attached.
Imports System.Windows.Forms
Imports System.Threading

Public Class WillNet
    Private oTimer As System.Threading.Timer
    Protected Overrides Sub OnStart(ByVal args() As String)
        ' Add code here to start your service. This method should set things
        ' in motion so your service can do its work.
        Dim oCallback As New TimerCallback(AddressOf OnTimedEvent)
        oTimer = New System.Threading.Timer(oCallback, Nothing, 0, 5000)

    End Sub

    Protected Overrides Sub OnStop()
        ' Add code here to perform any tear-down necessary to stop your service.
    End Sub

    Private Sub OnTimedEvent(ByVal state As Object)
        MessageBox.Show(Now, Me.ServiceName, MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.ServiceNotification)
    End Sub

End Class

Open in new window

0
 
LVL 48

Expert Comment

by:Wayne Taylor (webtubbs)
ID: 33444266
Add a NotifyIcon and show a message from there. This is the most common method for showing messages from a service.
0
 
LVL 1

Author Comment

by:Terry Rogers
ID: 33444304
Just tried that with no success, even though I have ticked "Allow service to interact with desktop" it just doesn't want to show anything on screen.
0
 
LVL 48

Accepted Solution

by:
Wayne Taylor (webtubbs) earned 668 total points
ID: 33444467
0
 
LVL 1

Author Comment

by:Terry Rogers
ID: 33445090
A notification area isn't going to work as their wont be a notification area if a user hasn't logged on.
0
 
LVL 10

Assisted Solution

by:joriszwaenepoel
joriszwaenepoel earned 668 total points
ID: 33445585
If you create a Windows Service, then one of consequences is that you can't interact with the logged on user, so you can't show a messagebox.
If you can find a trick that would allow you to do that, you will still run into problems.
If no one is logged on, should the servcie continue or wait until someone logs on?
If the service continues, and show a new messagebx, and then another one, and then another one ...

You should try to find another design.
For example: the Windows Service could write messages in a file or database.
Then when a user log on, start a WinForms app that reads the file or database and shows the messages to the user.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question