|
Hi.
I have a class "Files" that creates txt files, i want the progress bar and the multiline textbox in the xaml shows the process when its occurs.
I receive all the paramates at the MainWindows.cs and i see the progressbar1.value growing and the textbox1.text having the desired text but in the gui nothing happends.
WHY?
|
|
|
|
|
It's a semicolon at line 34 that's causing a problem.
You may wonder why I said that. Well, I said it because I can't see your code. How on earth would you expect me to be able to figure it out.
I would suspect that the issue you are facing is that you are doing all your processing on the primary thread, which will block the update of the UI until it's finished processing.
|
|
|
|
|
i really wondered!!!
here is the class that create the files:
class Farmonicior
{
private string sourcpath = string.Empty;
public string SourcePath
{
get
{
if (sourcpath==string.Empty)
global::System.Windows.MessageBox.Show("The Source Cannot be Empty!!!");
return sourcpath;
}
set {sourcpath = value;}
}
private string resultFolder = string.Empty;
public string ResultFolder
{
get
{
if ( resultFolder == string.Empty)
{
FileInfo fi = new FileInfo(sourcpath);
resultFolder = fi.DirectoryName;
}
return resultFolder;
}
set { resultFolder = value; }
}
public void FilesWork()
{
string line = string.Empty;
List<string> LinesList = new List<string>();
try
{
StreamReader StrRdr = new StreamReader(SourcePath);
while ((line = StrRdr.ReadLine()) != null )
{
if (line.StartsWith("LN"))
{
LinesList.Add(line);
}
}
StrRdr.Close();
}
catch (Exception ErrExc)
{
System.Windows.MessageBox.Show(ErrExc.Message,"Read Source.");
}
Singleton.Instance.ProgressBarMAX = LinesList.Count;
try
{
Singleton.Instance.ProgressBarVAL = 0;
string year = DateTime.Today.ToString("yy");
foreach (string item in LinesList)
{
string[] lineArr = item.Split('|');
string newfile = string.Format(@"{0}\{1}_{2}.txt", ResultFolder, lineArr[2], lineArr[3]);
Singleton.Instance.NewFilesPath.Add(newfile);
StreamWriter StrWtr = new StreamWriter(newfile, false);
StrWtr.WriteLine(string.Format("yy"));
StrWtr.WriteLine(string.Format("nn|tttttt"));
if (lineArr.Length == 8)
{
if (lineArr[7] != "")
StrWtr.WriteLine("qwertyui");
else
StrWtr.WriteLine("poiuytrewq");
}
else
StrWtr.WriteLine(string.Format("HR|{0}|{1}", lineArr[3], year));
StrWtr.WriteLine(string.Format("RTG|{0}|{1}", lineArr[5], lineArr[6]));
StrWtr.WriteLine("END");
StrWtr.Close();
Singleton.Instance.ProgressBarVAL++;
}
}
catch (Exception ErrExc)
{
System.Windows.MessageBox.Show(ErrExc.Message,"Create Files.");
}
}
}
Here the MainWindows.cs
public partial class MainWindow : Window,INotifyPropertyChanged
{
Farmonicior hrm = new Farmonicior();
public MainWindow()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
PrgrssBar1.Value = 0;
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
dlg.DefaultExt = ".txt";
dlg.Filter = "Text documents (.txt)|*.txt";
Nullable<bool> result = dlg.ShowDialog();
if (result == true)
{
string filename = dlg.FileName;
SourceFileTxtBx.Text = filename;
hrm.SourcePath = filename;
ResulFolderTxtBx.Text = hrm.ResultFolder;
}
}
private void button2_Click(object sender, RoutedEventArgs e)
{
string selectedFolder = string.Empty;
FolderBrowserDialog selectFolderDialog = new FolderBrowserDialog();
selectFolderDialog.ShowNewFolderButton = true;
if (selectFolderDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
Singleton.Instance.NewFilesPath.Clear();
FilesNamesTxtBx.Clear();
Singleton.Instance.ProgressBarVAL = 0;
selectedFolder = selectFolderDialog.SelectedPath;
ResulFolderTxtBx.Text = selectedFolder;
hrm.ResultFolder = selectedFolder;
}
}
private void GoBttn_Click(object sender, RoutedEventArgs e)
{
PrgrssBar1.Maximum = Singleton.Instance.ProgressBarMAX;
if (hrm.SourcePath == string.Empty)
System.Windows.MessageBox.Show("Must Select a source file");
else
{
foreach (string item in Singleton.Instance.NewFilesPath)
{
FilesNamesTxtBx.AppendText(item + Environment.NewLine);
}
FilesWork();
}
}
public void ProgressBarStep()
{
PrgrssBar1.Value++;
}
internal void updateProgressbarMax()
{
PrgrssBar1.Maximum = Singleton.Instance.ProgressBarMAX;
progresBarTxtBx.Text = "Total files: " + Singleton.Instance.ProgressBarMAX.ToString();
}
}
here the singleton
public sealed class Singleton
{
MainWindow MW = new MainWindow();
static readonly Singleton _instance = new Singleton();
public static Singleton Instance
{
get { return _instance; }
}
int progBarVal;
public int ProgressBarVAL
{
get { return progBarVal; }
set
{
progBarVal = value;
MW.ProgressBarStep();
}
}
int progBarMax;
public int ProgressBarMAX
{
get { return progBarMax; }
set
{
progBarMax = value;
MW.updateProgressbarMax();
}
}
List<string> newfilePath = new List<string>();
public List<string> NewFilesPath
{
get { return newfilePath; }
set
{
newfilePath = value;
}
}
private Singleton()
{
}
}
this is. I hope i copy enough. be nice if you see coding horrors. thanks.
|
|
|
|
|
As Pete said, you're doing all your processing on the primary thread, which will block the update of the UI until it's finished processing.
Have a look at the BackgroundWorker[^] component - it makes it easier to do some work on a background thread and show the progress in the UI.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Hi.
Thanks for reply.
If i copy the FilesWork function to the MainWindows Class everything works OK. this still means i need the BackgroundWorker?.
modified 3-Feb-13 1:42am.
|
|
|
|
|
Thanks Richard.
The BackgroundWorker works and GUI works also. I forgot the ReportProgress so takes me some time to implemente it.
|
|
|
|