Solved

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

Posted on 2010-09-21
8
815 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
Comment Utility
0
 

Author Comment

by:asc2010
Comment Utility
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
Comment Utility
"... 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
 

Author Comment

by:asc2010
Comment Utility
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

744 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now