[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 847
  • Last Modified:

Progress or Wait Indicator in C# or VB.Net While Writing To File


I am looking for a very simple method of letting the user know that something is happening.  I looked at a number of posts related to the progress bar - but they seem too much work for their worth.  I just need a message box to pop up while the application is running and close itself when it ends.
0
asc2010
Asked:
asc2010
  • 4
  • 3
1 Solution
 
John (Yiannis) ToutountzoglouCommented:
0
 
asc2010Author Commented:
jtoutou:  Thank you for this article.  Idle_Mind is the person who provided the core of the program I am using!  However, the splash screen is kind of overkill for this simple project.  I really need just a simple messagebox or some kind of small indicator that the application is running.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
"... or some kind of small indicator that the application is running."

Then one of my other articles may be better suited:
http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Basic.NET/A_2729-Building-a-Formless-WinForms-Application-with-a-NotifyIcon-in-VB-Net-2008.html

You can simply display a NotifyIcon in the Tray while the app processes and then hide it and exit when done.

Be sure to place a call to DoEvents() INSIDE your main loop to keep the message queue pumping.  I recommend you do it immediately after you read a line, or at the bottom of the loop:

    While Not sr.EndOfStream
        Application.DoEvents()

Another option would be to start with the code in the link and then place the core of the code I gave you into a BackgroundWorker() control.  This would eliminate the need to call DoEvents() and would be very easy since you are not updating the GUI at all.  In fact, a straight thread would also work.

Let me know if you need help fleshing it out....
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
asc2010Author Commented:
Ilde_Mind:

Wow! This works!!  However, it seems to keep the application running until the user right-clicks the icon and manually exits.  Is there a way to automatically terminate after the process is complete?
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Sure...just use this line when your processing is complete and you want the app to close:

        Application.Exit()

You can get rid of the menu portion of the tray icon altogether if you want...

Just comment out this line:

        ' CMS.Items.Add(ExitMenu)

Now your tray icon will just be static.
0
 
asc2010Author Commented:
I had to add Application.Run(Form1) in the Sub Main().  If I do not place that line somewhere in Module1 then the file never gets parsed and the subsequent files are never created.  I have a feeling I'm missing something obvious, but have no idea what...

I added Application.DoEvents() as specified above.  

I am not familiar at all with the BackgroundWorker() control, but am interested!
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Gotcha...you would remove Form1 completely and move the processing code to the MyContext() class.

Here is what it might look like:
Imports System.IO
Imports System.ComponentModel
Module Module1

    Public Sub Main()
        Application.Run(New MyContext)
    End Sub

End Module

Public Class MyContext
    Inherits ApplicationContext

    Public strFileName As String = "hotlist.txt"
    Public strCurrent As String = Directory.GetCurrentDirectory()

    Private WithEvents BGW As New BackgroundWorker
    Private WithEvents TrayIcon As New NotifyIcon

    Public Sub New()
        TrayIcon.Icon = My.Resources.Happy
        TrayIcon.Text = "Extracting Data..."
        TrayIcon.Visible = True

        BGW.RunWorkerAsync()
    End Sub

    Private Sub BGW_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BGW.DoWork
        ParseMe()
    End Sub

    Private Sub BGW_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BGW.RunWorkerCompleted
        Application.Exit()
    End Sub

    Private Sub MyContext_ThreadExit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.ThreadExit
        TrayIcon.Visible = False
    End Sub

    Private Sub ParseMe()
        Dim Path As String = strCurrent
        Dim DataFile As String = strFileName
        Dim V_File As String = "Stolen.txt"
        Dim W_File As String = "Wanted.txt"
        Dim LP_File As String = "LP.txt"
        Dim MP_File As String = "MP.txt"

        Using sw_V As New StreamWriter(System.IO.Path.Combine(Path, V_File), False)
            Using sw_W As New StreamWriter(System.IO.Path.Combine(Path, W_File), False)
                Using sw_LP As New StreamWriter(System.IO.Path.Combine(Path, LP_File), False)
                    Using sw_MP As New StreamWriter(System.IO.Path.Combine(Path, MP_File), False)

                        Using sr As New StreamReader(System.IO.Path.Combine(Path, DataFile))
                            While Not sr.EndOfStream
                                Dim values As New List(Of String)
                                values.AddRange(sr.ReadLine.Split(" "))
                                If values(2).ToUpper = "0H" AndAlso values(1).Trim <> "" Then
                                    ' remove columns indexed at: 0, 3, 4, 9
                                    values.RemoveAt(9)
                                    values.RemoveAt(4)
                                    values.RemoveAt(3)
                                    values.RemoveAt(0)
                                    Dim output As String = String.Join(",", values.ToArray)
                                    ' output to file based on first character of value in last colmun
                                    Select Case values(values.Count - 1).Substring(0, 1).ToUpper
                                        Case "V"
                                            sw_V.WriteLine(output)
                                        Case "W"
                                            sw_W.WriteLine(output)
                                        Case "P"
                                            sw_LP.WriteLine(output)
                                        Case "M"
                                            sw_MP.WriteLine(output)
                                    End Select
                                End If
                            End While
                        End Using

                    End Using
                End Using
            End Using
        End Using
    End Sub

End Class

Open in new window

0
 
asc2010Author Commented:
Idle_Mind,

Thank you once again!!!  This is an awesome little program that does exactly what I need.  More importantly, you have provided excellent code which I have already begun studying to learn how to do it myself next time!  
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now