473,327 Members | 1,930 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Dilemma updating UI from seperate thread

I have encountered a problem that I am having trouble solving. I have one main UI. From this UI you can bring up a new non-modal Windows form that displays some other data. However, each time a new form is brought up, it is done so in a new thread. What I want to do, is have these new forms (in their new thread) make a call to a public method in my main UI's class. This method(funciton) accepts a string argument (and returns a string as well), and basically adds the string that it is passed as an additional menu item on the "Window" menu drop down of the main menu bar. Then when I close one of the spawned forms, I want to make a call to another public method to remove it's menu item from the "Window" drop down list.

What I have tried so far, is adding this code to the Form_Loading event of the new form (remember though that this is on a seperate thread than my main UI -- the class which contains the public method I am calling).


myNameIs = myParentForm.AddWindowItem("NameOfThisForm");


The AddWindowItem method is a public method on my main UI class. It's code is as follows:

public string AddWindowItem(string windowName)
{
bool isLikeExisting = true;
int i = 1;
string cachedName = windowName;
string originalName = windowName;
while (isLikeExisting)
{
foreach (MenuItem mnuTest in mnuWindow.MenuItems)
{
i++;
if (windowName == mnuTest.Text)
{
windowName = windowName + i;
}
}
if (windowName == cachedName)
{
isLikeExisting = false;
}
else
{
cachedName = windowName;
windowName = originalName;
}
}
mnuWindow.MenuItems.Add(windowName);
return windowName;
}


Then on the closing event of my "spawned off in it's own thread" subform I do:

(myParentForm is an object instantiated as the main UI class that initiated this new thread/form)
myParentForm.RemoveWindowItem(myNameIs);


The RemoveWindowItem public method which resides in the main UI class on the main thread is as follows:

public void RemoveWindowItem(string myItemName)
{
int indexToRemove = 0;
foreach (MenuItem mnuTest in mnuWindow.MenuItems)
{
if (mnuTest.Text == myItemName)
{
indexToRemove = mnuTest.Index;
}
}
mnuWindow.MenuItems.RemoveAt(indexToRemove);
}



I'm sorry if this is very confusing. If anyone knows a way to accomplish what I'm trying to do, or can help in any way, I would greatly appreciate it. Thanks!
Aug 19 '07 #1
2 1297
Some additional info: Right now it will add the new menu item, and successfully remove it, but for 1 newly opened non-modal sub form only. If I try to open a second instance of the sub-form, nothing happens.. the form doesn't even open. But then when I close the 1st one I had opened, the second one I tried to open appears.


I have encountered a problem that I am having trouble solving. I have one main UI. From this UI you can bring up a new non-modal Windows form that displays some other data. However, each time a new form is brought up, it is done so in a new thread. What I want to do, is have these new forms (in their new thread) make a call to a public method in my main UI's class. This method(funciton) accepts a string argument (and returns a string as well), and basically adds the string that it is passed as an additional menu item on the "Window" menu drop down of the main menu bar. Then when I close one of the spawned forms, I want to make a call to another public method to remove it's menu item from the "Window" drop down list.

What I have tried so far, is adding this code to the Form_Loading event of the new form (remember though that this is on a seperate thread than my main UI -- the class which contains the public method I am calling).


myNameIs = myParentForm.AddWindowItem("NameOfThisForm");


The AddWindowItem method is a public method on my main UI class. It's code is as follows:

public string AddWindowItem(string windowName)
{
bool isLikeExisting = true;
int i = 1;
string cachedName = windowName;
string originalName = windowName;
while (isLikeExisting)
{
foreach (MenuItem mnuTest in mnuWindow.MenuItems)
{
i++;
if (windowName == mnuTest.Text)
{
windowName = windowName + i;
}
}
if (windowName == cachedName)
{
isLikeExisting = false;
}
else
{
cachedName = windowName;
windowName = originalName;
}
}
mnuWindow.MenuItems.Add(windowName);
return windowName;
}


Then on the closing event of my "spawned off in it's own thread" subform I do:

(myParentForm is an object instantiated as the main UI class that initiated this new thread/form)
myParentForm.RemoveWindowItem(myNameIs);


The RemoveWindowItem public method which resides in the main UI class on the main thread is as follows:

public void RemoveWindowItem(string myItemName)
{
int indexToRemove = 0;
foreach (MenuItem mnuTest in mnuWindow.MenuItems)
{
if (mnuTest.Text == myItemName)
{
indexToRemove = mnuTest.Index;
}
}
mnuWindow.MenuItems.RemoveAt(indexToRemove);
}



I'm sorry if this is very confusing. If anyone knows a way to accomplish what I'm trying to do, or can help in any way, I would greatly appreciate it. Thanks!
Aug 19 '07 #2
I think I found a work around. I'm not spawning my new forms off in new threads, instead I'm keeping them all in the same thread. When I close a spawned form, if it's "get data from the database" thread is not complete, I just do a thread abort on it and all is well. Before I was running the form itself in a new thread so that if the user tried to close it, it would actually keep running in the background until the thread completed successfully, at which point it would terminate. To the user though it was as though it terminated instantly when they clicked the X. Now though I'm just thread.aborting, and keeping all my forms in the same thread. I think things will be much smoother this way. And now since I'm not talking cross threads, my menu adding methods work fine.


Some additional info: Right now it will add the new menu item, and successfully remove it, but for 1 newly opened non-modal sub form only. If I try to open a second instance of the sub-form, nothing happens.. the form doesn't even open. But then when I close the 1st one I had opened, the second one I tried to open appears.
Aug 19 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Hal Vaughan | last post by:
First, I am aware of both SwingUtilities.invokeLater(), and of using Thread to create a new thread.  These are part of the problem. I want to have something running in the background, while the...
8
by: Serge | last post by:
Hi, I have some intensive code that is running on my main thread. I try to show a status update on a 'status form'. The problem that i have is that because it is running in the same thread the...
12
by: Brian Keating EI9FXB | last post by:
Hello all, Wonder what approach is used for this problem. I have a MDIApplication, the MDIClinets are to be in a seperate thread. So I've done something like this, // Create a new Show...
4
by: redneon | last post by:
I've set up a seperate thread and put a timer in it but for some reason it's tick event is never fired. This is what I have... .... Thread timerThread = new Thread(new ThreadStart(startTimer));...
9
by: Jervin Justin | last post by:
Hi, I've been having this problem for some time with Web Forms: I have a web app that sends data to a service when the user presses a button. Based on the data, the server will send several...
3
by: simchajoy2000 | last post by:
Hi, I am fairly new to the world of VB.NET form applications and I am unsure how to approach coding this situation. I have a VB.NET interface and I need to make it possible for a form, which is...
14
by: el_sid | last post by:
Our developers have experienced a problem with updating Web References in Visual Studio.NET 2003. Normally, when a web service class (.asmx) is created, updating the Web Reference will...
5
by: Mark R. Dawson | last post by:
Hi all, I may be missing something with how databinding works but I have bound a datasource to a control and everything is great, the control updates to reflect the state of my datasource when I...
1
by: Bmack500 | last post by:
Hello! And thanks in advance. I have the following code which just basically creates a set of threads for me (Using svrList as my list of class instances to thread). Let's call the threads...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.