6 Replies - 10497 Views - Last Post: 23 February 2011 - 04:14 AM Rate Topic: -----

#1 sabaripriya  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 22-February 11

Progressbar using Threading concept using winforms in C#

Posted 22 February 2011 - 04:02 AM

Hi,
Can any one help me out. here s my query. i have two button called "open " and "Save" . am using OpenFileDialog and SaveFileDialog. here s my code.
 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;
using System.Net;

namespace FileOpenDialog
{
    public partial class Form2 : Form
    {

        public Form2()
        {
            InitializeComponent();
        }

        // - Reading a file
        private void button1_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
            DialogResult result = openFileDialog1.ShowDialog();
            if (result == DialogResult.OK)
            {
                string data = Read(openFileDialog1.FileName);
                textBox1.Text = data;
            }
            else
            {
                // nothing
            }
        }
        // Read Method 
        private string Read(string file)
        {
            StreamReader reader = new StreamReader(file);
            string data = reader.ReadToEnd();
            reader.Close();
            return data;
        }
        //Save Method
        private void Save(string file, string data)
        {
            StreamWriter writer = new StreamWriter(file);
            writer.Write(data);
            writer.Close();
            this.progressBar1.Value = 0;
        }


       

        button2- save the file
        private void button2_Click(object sender, EventArgs e)
        {
                    
            DialogResult result = saveFileDialog1.ShowDialog();
            string file = saveFileDialog1.FileName.ToString();
            string data = textBox1.Text;
            Save(file, data);
        }

       
    }
    
}


   


My query s i need to capture how long will it take to save the file that should get displayed in the percentage format in progress bar using threading concept..

Kindly help me out.
Thanks & regards,
Priya

Is This A Good Question/Topic? 0
  • +

Replies To: Progressbar using Threading concept using winforms in C#

#2 ragingben  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 170
  • View blog
  • Posts: 637
  • Joined: 07-October 08

Re: Progressbar using Threading concept using winforms in C#

Posted 22 February 2011 - 04:17 AM

Hi,

To clairify is your question how to calculate how much time it will take to save the file, or how to use a progess bar with threading?
Was This Post Helpful? 0
  • +
  • -

#3 marinus  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 135
  • View blog
  • Posts: 575
  • Joined: 14-April 10

Re: Progressbar using Threading concept using winforms in C#

Posted 22 February 2011 - 04:24 AM

An easy way is to use

StreamReader reader = new StreamReader(file);
reader.BaseStream.Length; //reads the size of file and make calculation based on that

Im not sure but you can get reedTimeout with this

int x = reader.BaseStream.ReadTimeout;

Hope this helps.

This post has been edited by marinus: 22 February 2011 - 04:31 AM

Was This Post Helpful? 0
  • +
  • -

#4 Guest_Priya*


Reputation:

Re: Progressbar using Threading concept using winforms in C#

Posted 22 February 2011 - 08:22 AM

View Postragingben, on 22 February 2011 - 04:17 AM, said:

Hi,

To clairify is your question how to calculate how much time it will take to save the file, or how to use a progess bar with threading?




Both has to be done.. Need to calculate how much percentage it will take to save the particular file. that particular percentage can be assigned to the value of the progress bar using threading.
Was This Post Helpful? 0

#5 sabaripriya  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 22-February 11

Re: Progressbar using Threading concept using winforms in C#

Posted 22 February 2011 - 08:44 AM

View Postmarinus, on 22 February 2011 - 04:24 AM, said:

An easy way is to use

StreamReader reader = new StreamReader(file);
reader.BaseStream.Length; //reads the size of file and make calculation based on that

Im not sure but you can get reedTimeout with this

int x = reader.BaseStream.ReadTimeout;

Hope this helps.



Hi, Thanks for your reply.

i tried the in the save method itself
             private void Save(string file, string data)
        {
            StreamWriter writer = new StreamWriter(file);

            writer.Write(data);
            StreamReader reader = new StreamReader(file);
            long a = reader.BaseStream.Length;
            Console.WriteLine(a);

            writer.Close();

            this.progressBar1.Value = 0;
        }
it nt working..



Let me frame my question in a clear manner .

And my query is, Need to calculate how much percentage it will take to save the particular file. that particular percentage can be assigned to the value of the progress bar using threading.


For example, we can say to save a "abc.txt" file took 70% percentage means, then the progress bar has to show the loading to 70% with the help of threading.

Actually the task is,
"1.create a thread object in the button click event handler
2.create a delegate of parameterziedthreadStat as the entry point in the thread constructor.
3.Get the total length of the stream(file)
4.divide by 100
5.then assign that value to the progresssbar and need to show it.."

kindly help me out how to perform this five steps.. got struct.. since am new to winforms.. kindly help me out..

Regards,
Priya
Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6076
  • View blog
  • Posts: 23,543
  • Joined: 23-August 08

Re: Progressbar using Threading concept using winforms in C#

Posted 22 February 2011 - 12:23 PM

Perhaps you could get the idea from this fabulous tutorial?
Was This Post Helpful? 0
  • +
  • -

#7 sabaripriya  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 22-February 11

Re: Progressbar using Threading concept using winforms in C#

Posted 23 February 2011 - 04:14 AM

View PostJackOfAllTrades, on 22 February 2011 - 12:23 PM, said:

Perhaps you could get the idea from this fabulous tutorial?



Hi, Thanks for your idea. i have gone through the link provided by u.. still i got struct with proceeding further.

And my query is, Need to calculate how much percentage it will take to save the particular file. that particular percentage can be assigned to the value of the progress bar using threading.


For example, we can say to save a "abc.txt" file took 70% percentage means, then the progress bar has to show the loading to 70% with the help of threading.

Actually the task is,
"1.create a thread object in the button click event handler
2.create a delegate of parameterziedthreadStat as the entry point in the thread constructor.
3.Get the total length of the stream(file)
4.divide by 100
5.then assign that value to the progresssbar and need to show it.."

kindly help me out how to perform this five steps.. got struct.. since am new to winforms.. kindly help me out..



Regards,
Priya
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1