Solved

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

Posted on 2010-09-21
8
827 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 

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 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

726 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