Solved

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

Posted on 2010-09-21
8
824 Views
Last Modified: 2013-12-16

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
Comment
Question by:asc2010
  • 4
  • 3
8 Comments
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33729869
0
 

Author Comment

by:asc2010
ID: 33735348
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
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 33735439
"... 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:asc2010
ID: 33738795
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
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 33739366
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
 

Author Comment

by:asc2010
ID: 33745216
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
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 33745578
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
 

Author Closing Comment

by:asc2010
ID: 33747152
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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

809 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