|
-
March 15th, 2012, 01:44 AM
#1
Need help for Loading bar
i've got a problem
i tried to create a music player, and i already finish it.
and still searching for bug.
but i got problem if the user place folder with ton of recursive folder and like > 300 data.
it takes to long time, and make my programs like not responding/ crashed
so i think to create a loading notification when the program is adding the data.
i need to make a loading bar, but this loading bar isn't a progress bar it will show what filepath is currently added to program,
even if it change fast.
from what i think, i will add new windows form when the program started to add data, and dispose it when the program finish add data.
the problem is, how i make the form know what file i processed now, because if i do it normally. of course the form will freeze like the main form,
any idea here??
note: i used C# express with .net 4.0
-
March 15th, 2012, 02:44 AM
#2
Re: Need help for Loading bar
-
March 15th, 2012, 07:07 AM
#3
Re: Need help for Loading bar
Sounds like a pretty typical async problem.
Looking into a BackgroundWorker would probably work well for you. Otherwise, you could achieve this manually using threading.
Here's a small sample to get you started:
Code:
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Threading;
namespace AsyncFileAdd
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
e.Effect = DragDropEffects.Copy;
}
}
private void Form1_DragOver(object sender, DragEventArgs e)
{
DoDragDrop(e.Data, DragDropEffects.Copy);
}
private void Form1_DragDrop(object sender, DragEventArgs e)
{
object drop = e.Data.GetData(DataFormats.FileDrop);
label1.Visible = true;
Thread thread = new Thread(new ParameterizedThreadStart(AddFiles));
thread.Start(drop);
}
private void AddFiles(object state)
{
if (state is string[])
{
string[] paths = state as string[];
int count = 0;
int total = 0;
List<string> files = new List<string>();
foreach (string path in paths)
{
if (!System.IO.Directory.Exists(path))
continue;
files.AddRange(System.IO.Directory.GetFiles(path, "*.*", System.IO.SearchOption.AllDirectories));
}
total = files.Count;
foreach (string filepath in files)
{
count++;
label1.Invoke(new MethodInvoker(delegate()
{
label1.Text = String.Format("Adding file {0} of {1}: {2}", count, total, filepath);
}));
Thread.Sleep(500); //do stuff
}
}
}
}
}
It's not a bug, it's a feature!
-
March 17th, 2012, 07:45 AM
#4
Re: Need help for Loading bar
Im truely sorry to forget attach the code..
@foamy.
yours idea is nice. i really want to implement it. but how..
this code below will get all File name in the items you drop into
but when on the process of recursive getting folder. it got freeze until all data readed..
can you implement the thread here??
this is my code
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;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
DataGridView dgv = new DataGridView();
List<DataX> x = new List<DataX>();
public Form1()
{
InitializeComponent();
dgv.Size = new Size(260, 240);
dgv.Location = new Point(12, 12);
dgv.AllowDrop = true;
this.Controls.Add(dgv);
dgv.Visible = true;
DataGridViewTextBoxColumn temp = new DataGridViewTextBoxColumn();
temp.ReadOnly = true;
temp.Name = "Artist";
temp.HeaderText = "Artist";
dgv.Columns.Add(temp);
dgv.DragEnter+= new DragEventHandler(dgv_DragEnter);
dgv.DragDrop+=new DragEventHandler(dgv_DragDrop);
}
private void add(String Path)
{
x.Add(new DataX(Path));
int no = dgv.Rows.Add();
dgv.Rows[no].Cells[0].Value=x[x.Count - 1].theName;
}
private void dgv_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
e.Effect = DragDropEffects.Copy;
}
else
{
e.Effect = DragDropEffects.None;
}
}
private void dgv_DragDrop(object sender, DragEventArgs e)
{
try
{
string[] directoryName = (string[])e.Data.GetData(DataFormats.FileDrop);
//get all files inside folder
foreach (string data in directoryName)
{
if (Directory.Exists(data))
{
getFiles(data);
}
else
{
add(data);
}
}
}
catch (Exception ex) { /*MessageBox.Show(ex.Message);*/ }
}
private void getFiles(String Folder_Path)
{
try
{
string[] files = Directory.GetFiles(Folder_Path);
string[] folders = Directory.GetDirectories(Folder_Path);
foreach (string folder in folders)
{
getFiles(folder);
}
foreach (string file in files)
{
if (file.LastIndexOf(".") > 0)
{
add(file);
}
}
}
catch (Exception ex) { /*MessageBox.Show(ex.Message);*/ }
}
}
public class DataX
{
public String thePath;
public String theName;
public DataX(String Path)
{
thePath = Path;
theName = Path.Substring(Path.LastIndexOf("\\")+1);
}
}
}
the simple way to know it freeze or not. just drop your C: path.. dont worry wont crash or anything.
-
March 20th, 2012, 12:40 AM
#5
Re: Need help for Loading bar
i've tried with thread. but
when i do the datagridview i created show a new row but empty data.. even when i tried access it it crash..
so i was think of background thread..
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|