|
Hello.
I have a programm that scans the selected folder and adds all the files' names to the listbox.
I'm running the scan function in a separate thread. But, still the main form is freezing.
Here is my code:
private void button2_Click(object sender, EventArgs e)
{
Thread mThread = new Thread(myThread);
myThread.Start();
}
public void myThread()
{
AddFiles(label1.Text);
}
public void AddFiles(string targetDirectory)
{
if (listBox1.InvokeRequired)
{
AddFilesAdd method = new AddFilesAdd(AddFiles);
listBox1.Invoke(method, new object[] { targetDirectory });
return;
}
if (progressBar1.InvokeRequired)
{
AddFilesAdd method = new AddFilesAdd(AddFiles);
progressBar1.Invoke(method, new object[] { targetDirectory });
return;
}
string[] fileEntries = Directory.GetFiles(targetDirectory);
foreach (string fileName in fileEntries)
{
this.listBox1.Items.Add(fileName);
progCounter++;
progressBar1.Value = progCounter;
}
string[] subdirectoryEntries = Directory.GetDirectories(targetDirectory);
foreach (string subdirectory in subdirectoryEntries)
{
numberOfFolders++;
AddFiles(subdirectory);
}
|
|
|
|
|
Hi,
your code looks a bit weird to me. Here are some comments:
1.
AddFiles(label1.Text); is strictly not OK, you are touching a Control from a thread other than the main one. A better approach is to read label1.Text before launching the thread, then pass the string to it somehow (class member, or ThreadStart parameter).
2.
the progressbar code will never execute: progressBar1.InvokeRequired and listBox1.InvokeRequired are bound to have the same value, so either the first if block's return will fire, or both if blocks will be skipped.
3.
the whole threading approach is flawed: there really isn't any code running on the separate thread, all it does is cause the outermost AddFiles() to run on the main thread, so the hierarchical scan is all on the main thread (which will be very busy and uncapable of serving the GUI).
Solution: split the action in two parts, the first part scanning the file system and storing strings in a collection (all on the separate thread), the second part adding the collection's content to the ListBox.
4.
Lastly, this is a typical job a BackgroundWorker is good at. It would do the scanning in the DoWork handler, and the ListBox updating in the RunWorkerCompleted handler. You wouldn't need to call Invoke at all, the BGW does that for you.
PS: you may want to read this my little article[^] on the matter.
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
how read and write from/to stream color and points of line
|
|
|
|
|
I guess you need a BinaryWriter[^].
I must get a clever new signature for 2011.
|
|
|
|
|
You know, Goodgle is a wonderful tool. I've been programming on and off for 30 years, and I still find it useful. Streaming in C#[^]
Normally, I wouldn't be this sarcastic, but every single question you have ever asked has been voted 1 by multuiple people[^]. 1 = Bad by the way.
You have been told before, many times, please respect the forum rules. Don't ask for source code, state clearly what your problem is and what errors you are getting if any, and don't ask for information which is easily Googled.
|
|
|
|
|
In fairness he is not alone; there are plenty of regulars who still will not try Google or MSDN despite one of those being the answer to nearly every query they post.
I feel your pain!
I must get a clever new signature for 2011.
|
|
|
|
|
You need to add the ImUpShitCreek and IDontHaveAPaddle interfaces.
|
|
|
|
|
That is harsh, cruel and uncalled for!
Good work, keep it up.
------------------------------------
I will never again mention that I was the poster of the One Millionth Lounge Post, nor that it was complete drivel. Dalek Dave
CCC Link[ ^]
Trolls[ ^]
|
|
|
|
|
Hello,I'm writting a extend Webbrowser,that I hope to disable scripts in my webbrowser.Idispatch interface can make it ,but I don't know how to implement it. And I have not found some materials with c#.Can someone tell me how to implement it and implement The Invoke method?
|
|
|
|
|
WebBrowser.ScriptErrorsSuppressed = true;
This will suppress the script errors.
------------------------------------
I will never again mention that I was the poster of the One Millionth Lounge Post, nor that it was complete drivel. Dalek Dave
CCC Link[ ^]
Trolls[ ^]
|
|
|
|
|
I mean no script run in my webbrowser,not suppress the script errors.But thank for your answer.
|
|
|
|
|
You can't do it unless you want to disable scripts for IE itself.
When people drop a WebBrowser control on a form, then say they're writing their own browser, they are NOT writing their own browser. All you're doing is reusing Internet Explorer in your own app. All of the settings that you see in Internet Explorer (Tools -> Internet Options menu) apply to the WebBrowser object. If you modify the settings in IE, the WebBrowser object uses the same settings. So, if you disable script execution, you will also disable script execution in every instance of IE that the user also launches. Your application cannot keep its settings seperate from IE.
|
|
|
|
|
Hi,
1-
On my laptop, I have developed an application called MyApp in Visual C# 2010 Express which connects to sql server 2005 express.
Works fine.
2-
As you know there is no installation mechanism with c# 2010 express, so as a test, I copied all the files (.dll, .exe, pdb...) from the release folder and placed them into c:\program files(86)\MyApp
3-
When running the .exe in this folder, error is shown on the screen as "Login failed for user user1". Then when the continue button is clicked on the error message, the application form appears with all the controls but empty data.
4-
It seems there is a problem with the login to the sql server, BUT note that the login does exist in the sql server as the application works fine when I run it in the visual c# 2010 express environment when developing it.
5- In the application, there is a class library project (The .dll in this project) which uses the connectionstring in the app.config file.
What do y ou think the problem is please?
Thanks
|
|
|
|
|
|
Thanks for coming back to me,
but note that I have mentioned that as I use the same machine to develop the application, the application is able to login to the database when running it through the development environment i.e. when I am developing the application but it fails when the .exe is run manually.
Any thoughts on that please?
|
|
|
|
|
arkiboys wrote: Any thoughts on that please?
Personally no, so I suggest you read up on the experience others have had and published about. Error messages tend to make very good Google search sentences.
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
Have you tried looking in your Debug folder?
I am reasonably sure you might find your data files in there...possibly including the file you store the login information in?
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
Manfred R. Bihy: "Looks as if OP is learning resistant."
|
|
|
|
|
The connection string is in the settings of a class library project which is now a .dll file.
I have included this file but I get the ERROR as descrived before.
Thanks
|
|
|
|
|
That is probably because the settings are stored in a ".config" XML file.
Check all of those in your debug directory, and you will probably find it.
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
Manfred R. Bihy: "Looks as if OP is learning resistant."
|
|
|
|
|
Yes, I have found the config file in the debug folder and copied that also to C:\program files\AppName
But still ge tthe login failed error.
Not sure what is wrong because there is sql server and the login works as I can login using the application I have written when in the development environment.
|
|
|
|
|
arkiboys wrote: As you know there is no installation mechanism with c# 2010 express, so as a test, I copied all the files (.dll, .exe, pdb...) from the release folder and placed them into c:\program files(86)\MyApp
So you are copying on the binaries from C# and are not attempting a copy of SQL Server correct?
If yes then I would expect something is wrong with your code to do the login. As an example (only an example) you catch an exception and instead of reporting the exception error you report a failed login.
|
|
|
|
|
Correct.
I am not copying the sql server because it is already installed with the database and login.
Pleas enote that the login works through the application when connecting to the database.
|
|
|
|
|
Hei All,
Can we install any big software via C# setup application? I want to install SQL Server before my application and then create user in sql server but I dont know how can i install SQL server via setup file?
Thanks
Syed Shahid Hussain
|
|
|
|
|
|
Hei Thatraja,
Thanks u so so so so much. You dont know how big treasure u have given to me.
I'm Happy Thanks once again.
Syed Shahid Hussain
|
|
|
|