View Single Post
  #14  
Old September 9th 08, 04:27 PM posted to microsoft.public.outlook.program_addins
Dorian
external usenet poster
 
Posts: 19
Default Multithreading with C#

Woops! Yeah I was doing some editting in the posting textarea and forgot to
change the name of that delegate. It should be named the same as yours. Even
with these changes it's still coming back on the worker thread. I wouldn't
call myself a c# expert either. I am also fluent in VB if that makes things
easier (doubtful).

Your above example definitely returns to the main thread? I'm still boggled
as to why yours works and mine doesn't.

"Ken Slovak - [MVP - Outlook]" wrote:

I'm not sure if this will make any difference, I'm by no means the great C#
expert, and we've strayed a bit from Outlook code but you have this line in
your worker class:

internal event SupportIgnoreDelegate SupportIgnoreChanged; // Relates to
your HelloUserDone

I have the equivalent line in my code as:

internal event SupportIgnoreEventHandler SupportIgnoreChanged;

Then in my work completed event handler I have the equivalent of this:

public void ParseAndInsertReceivedMail()
{
// Do work (no OOM objects)
OnSupportIgnoreChanged(new IgnoreEventArgs(true));
}

protected virtual void OnSupportIgnoreChanged(IgnoreEventArgs e)
//Relates to
your OnHelloUserDone
{
SupportIgnoreEventHandler sid = SupportIgnoreChanged;
if (sid != null) sid(this, e);
}

Again, I'm no C# great expert but to me the naming differences and the
declaration syntax of the event are important in getting things to work.

If my suggestions don't help it might be more fruitful to post in a group
dedicated to C# where people more expert in the ins and outs of C# are
likely to hang out.

--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Professional Programming Outlook 2007.
Reminder Manager, Extended Reminders, Attachment Options.
http://www.slovaktech.com/products.htm


"Dorian" wrote in message
...
So this is what I have now. I've changed all of the accessors for the
event,
delegate and methods. I've created a new class that implements the
EventArgs
class. I don't know where you were going with the args array thing, but in
all the examples I've seen, if you want to include custom data in an event
argument you implement Eventargs. I've gotten my example as close as I can
to
yours and it's still coming back on a different thread. I am currently
calling a protected virtual method that in turn calls the delegate. Don't
worry, I'd be frustrated with me too :-)

// Main Class
public partial class ThisAddIn
{
private void Explorer_SelectionChange()
{
ReceivedMailHandler rmh = new ReceivedMailHandler();
// Do some stuff (Add data to the object)

rmh.SupportIgnoreChanged += new
ReceivedMailHandler. SupportIgnoreEventHandler(rmh_SupportIgnoreChanged );

Thread th = new Thread(rmh.ParseAndInsertReceivedMail);
th.IsBackground = true;
th.Start();
}

void rmh_SupportIgnoreChanged(Object sender,
ReceivedMailHandler.IgnoreEventArgs e) //Relates to your HelloUser
{
btnExpIgnore.Visible = e.visible; //Breakpoint here
}
}

// Worker Class
class ReceivedMailHandler
{
internal delegate void SupportIgnoreEventHandler(object Sender,
IgnoreEventArgs e); // Relates to your HelloUserDoneEventHandler
internal event SupportIgnoreDelegate SupportIgnoreChanged; // Relates to
your HelloUserDone

public void ParseAndInsertReceivedMail()
{
// Do work (no OOM objects)
RaiseIgnoreChange(new IgnoreEventArgs(true));
}

protected virtual void RaiseIgnoreChange(IgnoreEventArgs e) //Relates to
your OnHelloUserDone
{
SupportIgnoreEventHandler sid = SupportIgnoreChanged;
if (sid != null) sid(this, e);
}

public class IgnoreEventArgs : EventArgs
{
public bool visible;

public IgnoreEventArgs(bool Visible)
{
this.visible = Visible;
}
}
}



Ads