473,569 Members | 2,789 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dilemma updating UI from seperate thread

8 New Member
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.Ad dWindowItem("Na meOfThisForm");


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

public string AddWindowItem(s tring windowName)
{
bool isLikeExisting = true;
int i = 1;
string cachedName = windowName;
string originalName = windowName;
while (isLikeExisting )
{
foreach (MenuItem mnuTest in mnuWindow.MenuI tems)
{
i++;
if (windowName == mnuTest.Text)
{
windowName = windowName + i;
}
}
if (windowName == cachedName)
{
isLikeExisting = false;
}
else
{
cachedName = windowName;
windowName = originalName;
}
}
mnuWindow.MenuI tems.Add(window Name);
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.Re moveWindowItem( myNameIs);


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

public void RemoveWindowIte m(string myItemName)
{
int indexToRemove = 0;
foreach (MenuItem mnuTest in mnuWindow.MenuI tems)
{
if (mnuTest.Text == myItemName)
{
indexToRemove = mnuTest.Index;
}
}
mnuWindow.MenuI tems.RemoveAt(i ndexToRemove);
}



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 1307
binarybrain
8 New Member
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.Ad dWindowItem("Na meOfThisForm");


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

public string AddWindowItem(s tring windowName)
{
bool isLikeExisting = true;
int i = 1;
string cachedName = windowName;
string originalName = windowName;
while (isLikeExisting )
{
foreach (MenuItem mnuTest in mnuWindow.MenuI tems)
{
i++;
if (windowName == mnuTest.Text)
{
windowName = windowName + i;
}
}
if (windowName == cachedName)
{
isLikeExisting = false;
}
else
{
cachedName = windowName;
windowName = originalName;
}
}
mnuWindow.MenuI tems.Add(window Name);
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.Re moveWindowItem( myNameIs);


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

public void RemoveWindowIte m(string myItemName)
{
int indexToRemove = 0;
foreach (MenuItem mnuTest in mnuWindow.MenuI tems)
{
if (mnuTest.Text == myItemName)
{
indexToRemove = mnuTest.Index;
}
}
mnuWindow.MenuI tems.RemoveAt(i ndexToRemove);
}



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
binarybrain
8 New Member
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
4898
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 GUI is updating.  I've done that before without a problem, however, now, I need to pass variables to the separate Thread or Runnable that I'm...
8
3309
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 window is not responding to the user. The user is now able to minimize, move the window because the code is too busy on it's own work. (and they...
12
5831
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 method that starts a new thread public new void Show() { Thread t = new Thread(new ThreadStart(ThreadProc));
4
3361
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)); timerThread.Start(); .... private void startTimer() {
9
5806
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 replies. Since there is no way for a service to post to a client, I am using a thread on the client that polls the server for updates. (Feel free to...
3
990
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 from a separate dll, to popup when the user clicks on a picture in the interface. I have no idea how to do this. I am in the process of...
14
2927
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 utilise the disco file to update the Corresponding proxy file and reflect the changes made to the web service. However, the results of doing this with...
5
12502
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 update the datasource - awesome, but I have an issue with updating from a different thread. Here is my datasource, a person class that raises the...
1
1309
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 workerthreads. The worker threads need to update a textbox on Form1 as they progress. The question is, how do I create the necessary delegates /...
0
7697
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7924
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8120
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7672
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6283
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3640
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1212
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
937
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.