Coolguy
08-14-2015, 01:59 PM
Listbox threading for real-time log:
Thread thread1;
private void AddToLog(string message)
{
this.listBox1.Invoke(new MethodInvoker(() => this.listBox1.Items.Add(message)));
this.listBox1.Invoke(new MethodInvoker(() => this.listBox1.SelectedIndex = listBox1.Items.Count - 1));
this.listBox1.Invoke(new MethodInvoker(() => this.listBox1.SelectedIndex = -1));
}
//on start button
thread1 = new Thread(new ThreadStart(//your main function));
thread1.Start();
//on closing event
thread1.Abort();
Usage:
AddToLog("hi world");
Check if data folder/any text files exist and if they don't create them (example uses 1 text file):
public void createFolderAndTextFile()
{
string path = System.AppDomain.CurrentDomain.BaseDirectory;
[Only registered and activated users can see links]
string fileName = "Info.txt";
if (!Directory.Exists(System.IO.Path.Combine(path, dir)))
{
Directory.CreateDirectory(Path.Combine(path, dir));
File.WriteAllText(Path.Combine(path, dir, fileName), setText());
}
else if (!File.Exists(Path.Combine(path, dir, fileName)))
{
File.WriteAllText(Path.Combine(path, dir, fileName), setText());
}
else
{
File.WriteAllText(Path.Combine(path, dir, fileName), setText());
}
}
Set text:
private string setText()
{
return "Done \r\n"
+ this.string1 + "\r\n"
+ this.string2;
}
setText can be whatever you want to write to the text file.
Split word into 2 variables (for example username:password):
string[] words = new string[2];
words = currentLine.Split(':');
string username = words[0];
string password = words[1];
There's probably a better way to do that :P
Thread thread1;
private void AddToLog(string message)
{
this.listBox1.Invoke(new MethodInvoker(() => this.listBox1.Items.Add(message)));
this.listBox1.Invoke(new MethodInvoker(() => this.listBox1.SelectedIndex = listBox1.Items.Count - 1));
this.listBox1.Invoke(new MethodInvoker(() => this.listBox1.SelectedIndex = -1));
}
//on start button
thread1 = new Thread(new ThreadStart(//your main function));
thread1.Start();
//on closing event
thread1.Abort();
Usage:
AddToLog("hi world");
Check if data folder/any text files exist and if they don't create them (example uses 1 text file):
public void createFolderAndTextFile()
{
string path = System.AppDomain.CurrentDomain.BaseDirectory;
[Only registered and activated users can see links]
string fileName = "Info.txt";
if (!Directory.Exists(System.IO.Path.Combine(path, dir)))
{
Directory.CreateDirectory(Path.Combine(path, dir));
File.WriteAllText(Path.Combine(path, dir, fileName), setText());
}
else if (!File.Exists(Path.Combine(path, dir, fileName)))
{
File.WriteAllText(Path.Combine(path, dir, fileName), setText());
}
else
{
File.WriteAllText(Path.Combine(path, dir, fileName), setText());
}
}
Set text:
private string setText()
{
return "Done \r\n"
+ this.string1 + "\r\n"
+ this.string2;
}
setText can be whatever you want to write to the text file.
Split word into 2 variables (for example username:password):
string[] words = new string[2];
words = currentLine.Split(':');
string username = words[0];
string password = words[1];
There's probably a better way to do that :P