A Microsoft Outlook email forum. Outlook Banter

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » Outlook Banter forum » Microsoft Outlook Email Newsgroups » Add-ins for Outlook
Site Map Home Register Authors List Search Today's Posts Mark Forums Read Web Partners

Multithreading with C#



 
 
Thread Tools Search this Thread Display Modes
  #11  
Old September 8th 08, 09:28 PM posted to microsoft.public.outlook.program_addins
Ken Slovak - [MVP - Outlook]
external usenet poster
 
Posts: 5,848
Default Multithreading with C#

You're not following the signature patterns of what I showed you, nor are
you calling to a protected virtual handler that in turns calls the delegate
event handlers.

You need to follow the signatures, where you will receive sender and an args
array in your delegate event handlers. To add a string like blah to the
event args you would create an args[] array and populate args[1] with blah.

You're trying to handle this like a normal raising event type thing and that
won't do what you want.

--
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
...
Ok, so here's what I do:

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

rmh.SupportExceptionThrown += new
ReceivedMailHandler.SupportExceptionDelegate(rmh_S upportExceptionThrown);
rmh.SupportIgnoreChanged += new
ReceivedMailHandler.SupportIgnoreDelegate(rmh_Supp ortIgnoreChanged);

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

void rmh_SupportIgnoreChanged(bool IsVisible)
{
btnExpIgnore.Visible = IsVisible; //Breakpoint here
}

void rmh_SupportExceptionThrown(Exception ex)
{
string blah = "asd"; //Breakpoint here
}
}

// Worker Class
class ReceivedMailHandler
{
public delegate void SupportExceptionDelegate(Exception ex);
public delegate void SupportIgnoreDelegate(bool IsVisible);

public void ParseAndInsertReceivedMail()
{
// Do work (no OOM objects)
RaiseIgnoreChange(true);
RaiseException(new Exception("blah"));
}

private void RaiseException(Exception ex)
{
SupportExceptionDelegate sed = SupportExceptionThrown;
if (sed != null) sed(ex);
}

private void RaiseIgnoreChange(bool isVisible)
{
SupportIgnoreDelegate sid = SupportIgnoreChanged;
if (sid != null) sid(isVisible);
}
}

When it hits the breakpoints on rmh_SupportIgnoreChanged and
rmh_SupportExceptionThrown, both are still in the worker thread.


Ads
  #12  
Old September 9th 08, 03:27 PM posted to microsoft.public.outlook.program_addins
Dorian
external usenet poster
 
Posts: 19
Default Multithreading with C#

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;
}
}
}


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

You're not following the signature patterns of what I showed you, nor are
you calling to a protected virtual handler that in turns calls the delegate
event handlers.

You need to follow the signatures, where you will receive sender and an args
array in your delegate event handlers. To add a string like blah to the
event args you would create an args[] array and populate args[1] with blah.

You're trying to handle this like a normal raising event type thing and that
won't do what you want.

--
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

  #13  
Old September 9th 08, 04:13 PM posted to microsoft.public.outlook.program_addins
Ken Slovak - [MVP - Outlook]
external usenet poster
 
Posts: 5,848
Default Multithreading with C#

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;
}
}
}


  #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;
}
}
}



  #15  
Old September 9th 08, 06:28 PM posted to microsoft.public.outlook.program_addins
Ken Slovak - [MVP - Outlook]
external usenet poster
 
Posts: 5,848
Default Multithreading with C#

What I'm using initially fires in the worker class and then the event in the
Connect class fires.

About the only difference is that I used a form with a background worker on
it rather than setting up a separate thread. I preferred to do it that way,
you might want to try that.

My expertise in VB isn't much help with things like delegate events in C#,
my C experience is much more relevant.

--
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
...
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.


  #16  
Old September 9th 08, 07:29 PM posted to microsoft.public.outlook.program_addins
[email protected]
external usenet poster
 
Posts: 1
Default Multithreading with C#

On 5 Sep, 18:19, Dorian wrote:
Is there any way to trigger an event on the main thread once the worker
thread has finished running? I am used to doing this with forms, and having a
worker thread invoke a delegate on the main form. Since we're not working
with forms, or even controls for that matter, how would I go about doing this?


Create a simple Windows Forms control to do the thread switching for
you.
It does not need be bound to a form.

In main thread:

Control threadMarshaler; // member field
//...
threadMarshaler = new Control();
IntPtr dontcare = threadMarshaler.Handle; // Force creation of
necessary message pump

In worker thread:

threadMarshaler.Invoke(...);



 




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT +1. The time now is 12:22 PM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.Search Engine Friendly URLs by vBSEO 2.4.0
Copyright ©2004-2025 Outlook Banter.
The comments are property of their posters.