Click here to Skip to main content
15,900,589 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
In my MainWindow, I have
C#
public partial class MainWindow : Window
    {
        public List<ChannelResource> activeChannels = new List<ChannelResource>();


In my custom class,
C#
public class Dial
   {
       public ChannelResource m_ChannelResource;
       public VoiceResource m_VoiceResource;
       private TelephonyServer m_TelephonyServer;

       public Dictionary<string, string> dict = new Dictionary<string, string>();

       public OutboundDial(TelephonyServer telephonyServer)
       {
           m_TelephonyServer = telephonyServer;
       }
       public Dictionary<string, string> RunScript(AppointmentReminder callData, CancellationToken cToken)
       {
           try
           {
               try
               {
                   m_ChannelResource = m_TelephonyServer.GetChannel();

What I want is to access/pass activeChannels to the method RunScript. So I can add it such as:
C#
activeChannels.Add(m_ChannelResource);


Thank you.

EDIT for TPL code: 2:14 PM 10/30/2014
C#
private CancellationTokenSource cTokenSource;
private CancellationToken cToken;

 private void Start_Click(object sender, RoutedEventArgs e)
 {
    cTokenSource = new CancellationTokenSource();
    cToken = cTokenSource.Token;

    var producer = Producer();
    var consumer = Consumer();
    cToken.Register(() =>
    {
        foreach (ChannelResource cr in activeChannels)
        {
                // Stop dial on every channel one at a time.
               cr.StopDial();
        }
   });
 }

And for the customer part:
C#
async Task Consumer()
{
    try
    {
       var executionDataflowBlockOptions = new ExecutionDataflowBlockOptions
       {
            MaxDegreeOfParallelism = 5,
            CancellationToken = cToken
       };
       var consumerBlock = new ActionBlock<AppointmentReminder>(
remainder =>
{
    if (cToken.IsCancellationRequested)
        return;
    OutboundDial dial = new OutboundDial(ts);
    Dictionary<string, string> dict = dial.RunScript(remainder);
    activeChannels.Add(dial.m_ChannelResource); // but m_ChannelResource is null
    },
executionDataflowBlockOptions);

    m_Queue.LinkTo(
consumerBlock, new DataflowLinkOptions { PropagateCompletion = true });
    await consumerBlock.Completion;
}
Posted
Updated 30-Oct-14 8:14am
v4

1 solution

You can't really do that, not without complicating things far beyond sensible levels: the generic Dial class shouldn't know that any form related (or other user interface detail) classes even exist, much less what they contain.
Instead, your Dial class should create an event which the form handles, and the form then adds the data to it's collection.

Sounds complicated, I know - but it really isn't!
See here: Transferring information between two forms, Part 2: Child to Parent[^] - Dial is the child, the Form is the parent.


"@OriginalGriff, the thing is that the generic Dial class is used in TPL(task parallel library), say multiple Dials in an ActionBlock. I handle the parallelism from the MainWindow. In another word, I may need this collection from the parent form."


And when you signal the event, the form class gets the instance of the Dial class that raised it as the sender parameter as usual:
C#
void child_DataAvailable(object sender, EventArgs e)
    {
    Dial dial = sender as Dial;
    if (dial != null)
        {
        activeChannels.Add(dial.m_ChannelResource);
        }
    }
 
Share this answer
 
v2
Comments
[no name] 30-Oct-14 11:19am    
@OriginalGriff, the thing is that the generic Dial class is used in TPL(task parallel library), say multiple Dials in an ActionBlock. I handle the parallelism from the MainWindow. In another word, I may need this collection from the parent form.
OriginalGriff 30-Oct-14 11:30am    
Answer updated.
[no name] 30-Oct-14 14:16pm    
I already have a button event--Start_Click and I want to stick on it. I don't know why the list is empty. I think it is async issue. See my updated code please.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900