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

x
?
Solved

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

Posted on 2010-09-21
8
Medium Priority
?
838 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
[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
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 86

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
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 

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 86

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 86

Accepted Solution

by:
Mike Tomlinson earned 2000 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

Question has a verified solution.

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

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

636 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