From: Mike Lovell on
> Darn, I'm at work, and that website you pointed me to hasn't been
> "approved" for us to go to. Any chance you could post an excerpt of it
> directly on your reply? Thanks!

But they let you get to newsgroups? :o) Odd!

Excuse the formatting:

public partial class MainWindow : Window
{
private Thread workerThread;


public MainWindow()
{
InitializeComponent();
}


private void SetButtonsIsEnabled(bool value)
{
buttonStartRight.IsEnabled = value;
buttonStartWrong.IsEnabled = value;
}


private void buttonStartWrong_Click(object sender, RoutedEventArgs e)
{
SetButtonsIsEnabled(false);

for (int i=0; i<100; i+= 10)
{
Thread.Sleep(1000);

progress.Value = (double)i;
}

SetButtonsIsEnabled(true);
}


private void buttonStartRight_Click(object sender, RoutedEventArgs e)
{
SetButtonsIsEnabled(false);

workerThread = new Thread(new ThreadStart(delegate()
{
for (int i=0; i<100; i+= 10)
{
Thread.Sleep(1000);

Dispatcher.BeginInvoke((MethodInvoker)delegate()
{
progress.Value = (double)i;
}, null);
}

Dispatcher.BeginInvoke((MethodInvoker)delegate()
{
SetButtonsIsEnabled(true);
}, null);
}));

workerThread.Start();
}
}


--
Mike
GoTinker, C# Blog
http://www.gotinker.com
From: Julie on
On Mar 11, 8:31 am, Peter Duniho <no.peted.s...(a)no.nwlink.spam.com>
wrote:
> Julie wrote:
> > [...]
> > I can see that the thread that is calling the function is getting
> > "InvokeRequired" set to false; therefore, it is just doing the work
> > without invoking. The value for Thread.CurrentThread.ManagedThreadId
> > printed in the function does not match the value printed in the
> > constructor.
>
> > What's going on???
>
> Without a concise-but-complete code example, impossible to say.  You
> might be displaying the wrong information, or it could be that your
> class really is created on a different thread.  The thread that's
> important is the one where the Form sub-class is shown, not where it's
> instantiated.
>
> Normally, yes…the thread ID should be the same when InvokeRequired is
> false.  But depending on what your code really does, you may or may not
> see the same output from each location in the code.
>
> Pete

Okay, this is helpful: "The thread that's
> important is the one where the Form sub-class is shown, not where it's
> instantiated."

So, are you saying that the thread which calls SubClass.Show()?

Basically, I have one thread (thread A) which handles my main GUI.
Thread B comes along and calls Thread A to construct a new SubClass.
Thread B calls my function SubClass.draw() which returns
InvokeRequired = false, so Thread B resizes/repositions the GUI and
shows it. Thread B then makes additional calls to add text to the GUI
(and once again InvokeRequired = false), so Thread B adds the text
itself.

Does this sound okay?
From: Mike Lovell on
> And redundant even if it's not. I thought I'd previously shared my rant
> with the OP in a previous message thread, but just in case, here's the
> link:
> http://msmvps.com/blogs/duniho/archive/2008/09/12/msdn-s-canonical-technique-for-using-control-invoke-is-lame.aspx

Just had a look at your blog post, totally in agreement! :o)

--
Mike
GoTinker, C# Blog
http://www.gotinker.com

From: Julie on
Okay, I was having problems with what I was doing (above). I think it
seems to be working pretty well now.

I modified it so that:
Thread B comes along and calls Thread A to construct a new SubClass.
****Thread A then calls SubClass.show() so that Thread A now owns the
SubClass object.***
Then Thread B later calls SubClass.functionName(), which causes
InvokeRequired = true, which causes Thread A to do the work.

Sorry, maybe in the future I will modify this so as to not use the
methodology of InvokeRequired, but for now, I'm just happy that this
works. :o)

Thanks for your help, you guys.