|
Something like this should do it:
using System;
using System.IO;
using System.Drawing;
using System.Windows.Forms;
using System.Threading;
class test : System.Windows.Forms.Form
{
public delegate void Add(string s);
public ListBox box;
public Button button;
public test()
{
InitializeComponents();
}
public void InitializeComponents()
{
box = new ListBox();
button = new Button();
button.Location = new Point(70, 120);
button.Text = "Click Me";
button.Click += new EventHandler(button_Click);
this.Controls.Add(box);
this.Controls.Add(button);
}
private void StringAdder(string s)
{
box.Items.Add(s);
}
private void button_Click(object sender, EventArgs e)
{
Thread t = new Thread(new ThreadStart(StartThread));
t.Start();
}
private void StartThread()
{
Add a = new Add(StringAdder);
for(int i = 0; i < 100000; i++)
{
IAsyncResult ar = a.BeginInvoke("Nick", null, null);
}
}
[STAThread]
public static void Main()
{
Application.Run(new test());
}
}
- Nick Parker My Blog | My Articles
|
|
|
|
|
No, that will end up on a thread-pool worker thread. You need to use Control.Invoke (or BeginInvoke) to get back onto the user-interface thread. Control is a placeholder: it's an instance member, so you need to supply a Control or a class derived from Control on the left of .Invoke.
The fact that your code sometimes works in .NET 1.x is an accident. There's always a possibility of race conditions or deadlocks. Some controls work, others don't.
Typically you'd Invoke on the form itself, e.g.:
private void StartThread()
{
Add a = new Add(StringAdder);
object[] args = new object[] { "Mike" };
for(int i = 0; i < 100000; i++)
{
this.Invoke( a, args );
}
} (shamelessly stealing Nick's example).
You'd BeginInvoke rather than Invoke if you wanted the UI notification to happen asynchronously. Invoke waits for the UI thread to finish processing the call before continuing. Some sources suggest always using BeginInvoke since you can't know how busy the UI is going to be, and you can end up deadlocking in some situations. This is the equivalent of using PostMessage versus SendMessage in C++ code.
Windows Forms 2.0 enforces the correct model: it always throws if you try to update UI from the wrong thread.
Stability. What an interesting concept. -- Chris Maunder
|
|
|
|
|
Ok, I understand what you are both saying. Would this work? Also the line
Add a = new Add(StringAdder);
has me confused. What is add? The compiler complains about me missing a Using directive.
string s;<br />
s = "Timeout";<br />
Add a = new Add(StringAdder);<br />
IAsyncResult ar = a.BeginInvoke(s, null, null);<br />
Thanks again.
PS: What about things like listBox1.Items.Clear() etc...
Yippee Kai Yai
Signed Polomint.......
|
|
|
|
|
polomint wrote:
What is add?
Add was defined as a delegate . You can read more about it here[^]. If you noticed, the StringAdder method I wrote matched the method signature defined in the Add delegate . It basically allows me to treat StringAdder as a callback function when the BeginInvoke method is called from another thread.
- Nick Parker My Blog | My Articles
|
|
|
|
|
Damn, I should have read the code properly. I never noticed the delegate. I've always been impatient.
Thanks again..
Yippee Kai Yai
Signed Polomint.......
|
|
|
|
|
Mike Dimmick wrote:
Invoke waits for the UI thread to finish processing the call before continuing
In my test example, I didn't want to wait, which is why I went with the asynchronous call.
Mike Dimmick wrote:
Windows Forms 2.0 enforces the correct model: it always throws if you try to update UI from the wrong thread.
Hmm, I will have to check this out at home, I don't have Visual Studio 2005 here at work.
- Nick Parker My Blog | My Articles
|
|
|
|
|
Yep, your right, I can do it in .NET 1.1 but 2.0 doesn't like it. In fact, Visual Studio shows this error:
- Nick Parker My Blog | My Articles
|
|
|
|
|
Hi all,
1) For one of the columns in my datagrid, I want the header text to be "Hourly Rate". I want "Hourly" displayed on top with "Rate" underneath it. How can I do this in C#?
2) How can I align the header text of a column to be center while the data is aligned as left?
I can align the entire column (affecting both Data and header) only.
Please help
THanks.........
|
|
|
|
|
First of all, I'm using the .NET COMPACT Framework. I have a message box, with yes and no buttons. When a user clicks yes, a large amount of logic is executed (databases are queried, xml is parsed, docs are printed, etc). All of this logic takes about 10 seconds to complete, but the confirmation dialog just hangs there with the yes button depressed until a "success" message box is displayed. My boss doesn't like the way this looks, because the program appears to be "frozen". I would like to have the confirmation box disappear after the user presses yes - is this only possible if i bring up something else (i.e. a progress bar)? Also, is it possible to have a progress bar in a messagebox?
Here is the code I'm using for the confirmation box:
if(MessageBox.Show("Backflush Pallet "+pallet+"?","Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
{
...execute tons of logic...
}
Thanks,
j1e1g1
|
|
|
|
|
Hmm...this is slightly tricky since that dialog isn't controled by you and therefore can't do self contained logic on how to appear/disappear. I'm not familiar with how the Compact Framework behaves but it might be solved if you let the message pump work. What might be going on is that the message "close dialog" is in the message queue but isn't being done because of the "execute tons of logic" bit.
I'm not sure how long of a fishing excirse you want to go on but one thing you can try is putting the "execute tons of logic" in another thread and let the message pump work in the primary UI thread. I'm not sure if this will work because as I said I am not sure how the Compact Framework behaves with threaded behavior let alone how the UI functions in subtle behaviors like this.
|
|
|
|
|
Wack in a call do Application.DoEvents() just before the "tons of logic" and the message box will disappear.
|
|
|
|
|
The simplest method would be to break the
MessageBox.Show call out of the if block.
DialogResult dr;
dr = MessageBox.Show(yadda, yadda)
if(dr == whatever) {
.. do tons of whatever stuff
}
|
|
|
|
|
HI~
I use a listview(large icon) of compact framework. After I click a item, it launches to another form with large icon listview.
However, after I launch to another form, all the icons of the previous form in the were disappeared.
How can I solve it?
Thanks
|
|
|
|
|
|
Hi there,
I'm using CDO to access Exchange folders in a C# application.
Is there a way to filter the Task and/or Appointment items in a public folder for start date and due date?
Filtering for a specifig start date and end date is easy, but I need to find all Task items that begin after date X and end before date Y.
Is this possible?
Thanks for every hint,
coco
|
|
|
|
|
Hi, guys, how can I display a progress(eg. just like open a web page progress in IE6.0) in the statusbar.
Thank you every much
vigorous
|
|
|
|
|
Go to MSDN and look up the StatusBar class and the ProgressBar class. StatusBar has a Panel where you can add panels. If you don't like monkeying with code use the designer add panels then add ProgressBar to the panel.
|
|
|
|
|
http://discuss.develop.com/archives/wa.exe?A2=ind0110D&L=DOTNET&D=0&P=6846
Shows you how
Gary
"A fellow with the inventiveness of Albert Einstein but with the attention span of Daffy Duck."
Tom Shales talking about Robin Williams
|
|
|
|
|
|
Hello all!
Is there any way to simulate the mouse on the way you normally use it?
I read that with the Message class you can set the mouse's position in certain window (or in all at the same time(???)), but can you set the mouse's position in WinXP like you normally moving the mouse? So you don't set to which window you want to send the mouseposition, just moving it and clicking like when you use it, and an application will have the control when you clicked on its window or something (like when you just use the mouse with your hands).
So like instead of a window you send it directly to the operation system or something?
MS Visual Studio.NET 2003
C#
|
|
|
|
|
|
Damn I almost started a new thread in ASP.NET
Just before typed the message thought first i'd ask you why(i couldn't understand how the hack it can be), but now I see your subject is answer to a completely different question. How come it posted to my thread?
MS Visual Studio.NET 2003
C#
|
|
|
|
|
There was a goof up with the forums when I was posting, not sure exactly what happened but I noticed it as well.
- Nick Parker My Blog | My Articles
|
|
|
|
|
I have a ListBox control on my form which has the following properties set:
MultiColumn = false;<br />
SelectionMode = = SelectionMode.One;<br />
Sorted = false;
and I load the data as such:
DataSource = dt;<br />
DisplayMember = "CASE_NAME";<br />
ValueMember = "CASE_NAME";
All is fine, the ListBox displays the data just fine, with one problem. Before the user has selected any of the items in the ListBox the first item is highlighted as if the user has already selected it. Is there any way to show the initial display of the items in the ListBox without ANY of the items being highlighted?
|
|
|
|
|
After binding it to the DataSource, set the SelectedIndex of the ListBox to -1
|
|
|
|