-3

I made a program that changes a .txt file by using Writeline(). Everything works fine, just I really want to know whether it is possible or not to show the progress of the writing, or not, and if possible, how. Note: I only recently began with c#.

    private void button1_Click(object sender, EventArgs e)
    {
        string res = "";
        {
            if (checkBox1.Checked == true) { res = "playercontrols:1:up-w,right-d,left-a,aimy-,run-lshift,reload-r,portal2-,portal1-,jump- ,aimx-,down-s,use-e;playercontrols:2:up-joy-1-hat-1-u,right-joy-1-hat-1-r,left-joy-1-hat-1-l,aimy-joy-1-axe-4-neg,run-joy-1-but-3,reload-joy-1-but-4,portal2-joy-1-but-6,portal1-joy-1-but-5,jump-joy-1-but-1,aimx-joy-1-axe-5-neg,down-joy-1-hat-1-d,use-joy-1-but-2;playercontrols:3:up-joy-2-hat-1-u,right-joy-2-hat-1-r,left-joy-2-hat-1-l,aimy-joy-2-axe-4-neg,run-joy-2-but-3,reload-joy-2-but-4,portal2-joy-2-but-6,portal1-joy-2-but-5,jump-joy-2-but-1,aimx-joy-2-axe-5-neg,down-joy-2-hat-1-d,use-joy-2-but-2;playercontrols:4:up-joy-3-hat-1-u,right-joy-3-hat-1-r,left-joy-3-hat-1-l,aimy-joy-3-axe-4-neg,run-joy-3-but-3,reload-joy-3-but-4,portal2-joy-3-but-6,portal1-joy-3-but-5,jump-joy-3-but-1,aimx-joy-3-axe-5-neg,down-joy-3-hat-1-d,use-joy-3-but-2;playercolors:1:224,32,0,136,112,0,252,152,56;playercolors:2:255,255,255,0,160,0,252,152,56;playercolors:3:0,0,0,200,76,12,252,188,176;playercolors:4:32,56,236,0,128,136,252,152,56;portalhues:1:0,0.125;portalhues:2:0.25,0.375;portalhues:3:0.5,0.625;portalhues:4:0.75,0.875;mariohats:1:1;mariohats:2:1;mariohats:3:1;mariohats:4:1;scale:3;shader1:none;shader2:none;volume:1;mouseowner:1;mappack:smb;gamefinished;"; }
            else { res = "playercontrols:1:up-w,right-d,left-a,aimy-,run-lshift,reload-r,portal2-,portal1-,jump- ,aimx-,down-s,use-e;playercontrols:2:up-joy-1-hat-1-u,right-joy-1-hat-1-r,left-joy-1-hat-1-l,aimy-joy-1-axe-4-neg,run-joy-1-but-3,reload-joy-1-but-4,portal2-joy-1-but-6,portal1-joy-1-but-5,jump-joy-1-but-1,aimx-joy-1-axe-5-neg,down-joy-1-hat-1-d,use-joy-1-but-2;playercontrols:3:up-joy-2-hat-1-u,right-joy-2-hat-1-r,left-joy-2-hat-1-l,aimy-joy-2-axe-4-neg,run-joy-2-but-3,reload-joy-2-but-4,portal2-joy-2-but-6,portal1-joy-2-but-5,jump-joy-2-but-1,aimx-joy-2-axe-5-neg,down-joy-2-hat-1-d,use-joy-2-but-2;playercontrols:4:up-joy-3-hat-1-u,right-joy-3-hat-1-r,left-joy-3-hat-1-l,aimy-joy-3-axe-4-neg,run-joy-3-but-3,reload-joy-3-but-4,portal2-joy-3-but-6,portal1-joy-3-but-5,jump-joy-3-but-1,aimx-joy-3-axe-5-neg,down-joy-3-hat-1-d,use-joy-3-but-2;playercolors:1:224,32,0,136,112,0,252,152,56;playercolors:2:255,255,255,0,160,0,252,152,56;playercolors:3:0,0,0,200,76,12,252,188,176;playercolors:4:32,56,236,0,128,136,252,152,56;portalhues:1:0,0.125;portalhues:2:0.25,0.375;portalhues:3:0.5,0.625;portalhues:4:0.75,0.875;mariohats:1:1;mariohats:2:1;mariohats:3:1;mariohats:4:1;scale:3;shader1:none;shader2:none;volume:1;mouseowner:1;mappack:smb;"; }
            if (checkBox2.Checked == true) { res = res + "reachedworlds:smb:1,"; }
            else { res = res + "reachedworlds:smb:0,"; }
            if (checkBox3.Checked == true) { res = res + "1,"; }
            else { res = res + "0,"; }
            if (checkBox4.Checked == true) { res = res + "1,"; }
            else { res = res + "0,"; }
            if (checkBox5.Checked == true) { res = res + "1,"; }
            else { res = res + "0,"; }
            if (checkBox6.Checked == true) { res = res + "1,"; }
            else { res = res + "0,"; }
            if (checkBox7.Checked == true) { res = res + "1,"; }
            else { res = res + "0,"; }
            if (checkBox8.Checked == true) { res = res + "1,"; }
            else { res = res + "0,"; }
            if (checkBox9.Checked == true) { res = res + "1;"; }
            else { res = res + "0;"; }
        }
        DialogResult ans=MessageBox.Show("Warning! All settings in your game will be reset. Do you wish to continue?", "Warning", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
        if (ans == DialogResult.OK)
        {
            string roaming = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
            string path = roaming + "\\LOVE\\mari0\\options.txt";
            using (StreamWriter sr = new StreamWriter(path))
            {
                sr.Write(res);
            }
        }
B.K.
  • 9,982
  • 10
  • 73
  • 105
user
  • 5
  • 3
  • Have you googled and found this [How to use WinForms progress bar?](http://stackoverflow.com/questions/12126889/how-to-use-winforms-progress-bar) – user1477388 Feb 26 '14 at 20:39
  • Please think again about your question's title. It shall be a *summary* of the problem / question, not a mostly random collection of words. – Ondrej Tucny Feb 26 '14 at 20:45
  • After rethinking the title, ask yourself how much data you are writing to the file and how much time it is typically going to take. … Are you still sure you need a progress bar? – Ondrej Tucny Feb 26 '14 at 20:47
  • Ondrej Tucney, it was actually not my title,but it was edited for some reason. and I dont need one, im trying to learn how one works – user Feb 26 '14 at 21:00
  • @user He was commenting on your old title. I edited it so that it made more sense. – B.K. Feb 26 '14 at 21:43

1 Answers1

0

Assuming you're using winforms, based on one of your tags, here's something you can do:

public Form1()
{
    InitializeComponent();
    backgroundWorker1.WorkerReportsProgress = true;
}

private void WriteToFileMethod()
{
    // your routine here
}

private void button_Click(object sender, EventArgs e)
{
    progressBar.Maximum = 100;
    progressBar.Step = 1;
    progressBar.Value = 0;
    backgroundWorker.RunWorkerAsync();
}

private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
    var backgroundWorker = sender as BackgroundWorker;
    for (int i = 0; i < workSize; i++)
    {
        WriteToFileMethod();
        backgroundWorker.ReportProgress((i * 100) / workSize);
    }
}

private void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    progressBar1.Value = e.ProgressPercentage;
}

Just replace workSize with the size of the work that you're trying to do. You can also adjust that code to report progress for number of lines that you're writing or number of files. It just depends on what exactly you're trying to report. You can even do memory size.

MSDN does a fantastic job of explaining all the details here. Besides looking at my example, I highly suggest reading up their overview. It'll provide a more detailed understanding of what needs to be done.

Lastly, progress bar might not always be a viable option. Something you can implement to let the user know that a background process is taking place and that the process hasn't crashed is a WaitCursor: Cursor.Current = Cursor.WaitCursor; Just something to think about...

B.K.
  • 9,982
  • 10
  • 73
  • 105