Problem with Open(or save)Filedialog
-
hi, i have a server application that the req from clients are answered with a seperate thread and my windows form(my user interface) is answering the user. recently i've added a new button that would open me a savefile dialog, another button for openfiledialog and the last for folderbrowserdialog. first button:
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
//do sth
}sec button:
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
textBox_bak.Text = openFileDialog1.FileName;
}third button:
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
//do sth
}thhere is a weired problem with first button and the sec button, when i hit them my thread won't work and won't answer to clients untill the (save or open)dialog is open but button 3 is not like them. :doh:
-
hi, i have a server application that the req from clients are answered with a seperate thread and my windows form(my user interface) is answering the user. recently i've added a new button that would open me a savefile dialog, another button for openfiledialog and the last for folderbrowserdialog. first button:
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
//do sth
}sec button:
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
textBox_bak.Text = openFileDialog1.FileName;
}third button:
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
//do sth
}thhere is a weired problem with first button and the sec button, when i hit them my thread won't work and won't answer to clients untill the (save or open)dialog is open but button 3 is not like them. :doh:
-
i should mention that this problem occured in XP but not vista(i mean vista was ok!) there should not be any difference, should be?
Hi, the GUI Controls are not thread-safe; Windows requires that a Control only be accessed by the thread that created it (normally the GUI thread for all of them, since they typically are all linked in one big hierarchy). When other threads need to report and show some results using the GUI, there is an Invoke mechanism, supported by Control.InvokeRequired and the methods Control.Invoke() or BeginInvoke() methods. if you fail to obey the rules, the GUI may seem fine for a while, for some of its Controls, on some machines, on some operating system, but in general it is destined to fail. final remarks: - since NET2.0 illegal cross-thread calls throw an exception; there is a property to suppress those and that is a very bad idea. - also since NET2.0 there is a BackGroundWorker class that is useful in dealing with background work needing to update the GUI. :)
Luc Pattyn [Forum Guidelines] [My Articles]
I use ListBoxes for line-oriented text output (not TextBoxes), and PictureBoxes for pictures (not drawings).
modified on Friday, June 10, 2011 12:19 PM
-
Hi, the GUI Controls are not thread-safe; Windows requires that a Control only be accessed by the thread that created it (normally the GUI thread for all of them, since they typically are all linked in one big hierarchy). When other threads need to report and show some results using the GUI, there is an Invoke mechanism, supported by Control.InvokeRequired and the methods Control.Invoke() or BeginInvoke() methods. if you fail to obey the rules, the GUI may seem fine for a while, for some of its Controls, on some machines, on some operating system, but in general it is destined to fail. final remarks: - since NET2.0 illegal cross-thread calls throw an exception; there is a property to suppress those and that is a very bad idea. - also since NET2.0 there is a BackGroundWorker class that is useful in dealing with background work needing to update the GUI. :)
Luc Pattyn [Forum Guidelines] [My Articles]
I use ListBoxes for line-oriented text output (not TextBoxes), and PictureBoxes for pictures (not drawings).
modified on Friday, June 10, 2011 12:19 PM
thanks for you'r nice answer, but i know the rules, i have userd Invokde every where that i need to point a func or obj created outside of the thread. so what is the solution? u just explain me why this happened? u mean a way is to use background worker instead of separate thread?
-
thanks for you'r nice answer, but i know the rules, i have userd Invokde every where that i need to point a func or obj created outside of the thread. so what is the solution? u just explain me why this happened? u mean a way is to use background worker instead of separate thread?
Hi, you need to provide more information on the situation. How many threads/threadpools/backgroundworkers are there? how many are involved in accessing Controls (even reading a property)? you should also consider hidden threads, serving asynchronous actions such as timer events, serial port or network data received, probably also remoting, etc. :)
Luc Pattyn [Forum Guidelines] [My Articles]
I use ListBoxes for line-oriented text output (not TextBoxes), and PictureBoxes for pictures (not drawings).
modified on Friday, June 10, 2011 12:19 PM
-
Hi, you need to provide more information on the situation. How many threads/threadpools/backgroundworkers are there? how many are involved in accessing Controls (even reading a property)? you should also consider hidden threads, serving asynchronous actions such as timer events, serial port or network data received, probably also remoting, etc. :)
Luc Pattyn [Forum Guidelines] [My Articles]
I use ListBoxes for line-oriented text output (not TextBoxes), and PictureBoxes for pictures (not drawings).
modified on Friday, June 10, 2011 12:19 PM
It is my only thread i have settings file in common with my GUI and thread(i user Control.Invoke to reach to my Settings.xml ), there is no background worker. my thread is no timer, no serial port, my thread just listen to network for any incomming clients(Listener.AcceptTcpClient()) and answer those clients according to what is on my sql database. and that's all, i am so Confused :(
-
It is my only thread i have settings file in common with my GUI and thread(i user Control.Invoke to reach to my Settings.xml ), there is no background worker. my thread is no timer, no serial port, my thread just listen to network for any incomming clients(Listener.AcceptTcpClient()) and answer those clients according to what is on my sql database. and that's all, i am so Confused :(
Hi, I still don't get the overall architecture of your app. If you want help, then please explain yourself. This is what I got so far: - it is a server app; AFAIK most servers create a thread for every incoming request, so each request can get handled at its own pace (the alternative of just queueing all requests and handling them one by one does not scale well). - you use AcceptTcpClient() which is a blocking call; if you do this on the main thread, the GUI dies until a client comes in??? otherwise, you need a separate thread that accepts clients and dispatches them. suggestions: - explain the architecture, using real names; - show actual code, not just a skeleton; but not hundreds of lines, just the relevant pieces. - when in doubt about threading use either someControl.InvokeRequired; whenever it returns true you need to use (Begin)Invoke - and/or when in doubt about threading, log the Thread.CurrentThread.ManagedThreadId so you can compare it to the one of the GUI thread (by logging ManagedThreadId say inside a form load handler). :)
Luc Pattyn [Forum Guidelines] [My Articles]
I use ListBoxes for line-oriented text output (not TextBoxes), and PictureBoxes for pictures (not drawings).