Sign in to follow this  
freddyscoming4you

[.net] Parallel Task Library Help

Recommended Posts

I've put together an app that has several tasks simply churning out GUIDs and another task adding those GUIDs to a list box and wiping out the GUID list on each execution to keep the GUID bucket as small as possible. This blazes until about 50k entries into the listbox then it slows down a bit. I've tried to adjust the TaskContinuationOptions to get as much efficiency as I can but that's about it. My hunch is that the lock queue over the GUID list itself just keeps growing but I don't know how to programmatically break into that lock queue and flush or otherwise wipe out any waiting lock requests, which I would do in the method that wipes out the GUID list. Below is the code. Any/all thoughts are welcome and appreciated. For anyone looking for a point to this I'm just doing this as an exercise in dealing with a lot of data from multiple cores simultaneously and this is a pretty simple example I could think of.

Thanks.

[code]
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GUID
{
public partial class Form1 : Form
{
Task t = null;
Task guidGenerator1 = null;
Task guidGenerator2 = null;
Task guidGenerator3 = null;
VoidDelegate vd1;
VoidDelegate vd3;
int guidCount;

List<Guid> guidList = null;

public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
t = new Task( new Action( DoCreateGUID ) );
guidGenerator1 = new Task( new Action( GenerateNewGUID1 ) );
guidGenerator2 = new Task( new Action( GenerateNewGUID2 ) );
guidGenerator3 = new Task( new Action( GenerateNewGUID3 ) );

vd1 = new VoidDelegate( AddGuidsToList );
vd3 = new VoidDelegate( DupeFound );

guidList = new List<Guid>();

guidGenerator1.Start();
guidGenerator2.Start();
guidGenerator3.Start();
t.Start();
}

private void GenerateNewGUID1()
{
lock (guidList)
{
guidList.Add( Guid.NewGuid() );
}

guidGenerator1.ContinueWith( delegate
{
GenerateNewGUID1();
}, TaskContinuationOptions.ExecuteSynchronously );
}

private void GenerateNewGUID2()
{
lock (guidList)
{
guidList.Add( Guid.NewGuid() );
}

guidGenerator2.ContinueWith( delegate
{
GenerateNewGUID2();
}, TaskContinuationOptions.ExecuteSynchronously );
}

private void GenerateNewGUID3()
{
lock (guidList)
{
guidList.Add( Guid.NewGuid() );
}

guidGenerator3.ContinueWith( delegate
{
GenerateNewGUID3();
}, TaskContinuationOptions.ExecuteSynchronously );
}

private void AddGuidsToList()
{
lock (guidList)
{
object[] guids = new object[ guidList.Count ];

for (int i = 0; i < guidList.Count; i++)
{
if (listBox1.Items.Contains( guidList[ i ] ))
{
MessageBox.Show( "DUPE!!" );
}

guids[ i ] = guidList[ i ];
}

listBox1.Items.AddRange( guids );

this.count.Text = (this.guidCount += guidList.Count).ToString();

guidList.Clear();
}
}

private void DupeFound()
{
MessageBox.Show( "DUPE!! -- " );
}

private void DoCreateGUID()
{
this.listBox1.Invoke( vd1 );

if (t != null)
{
t.ContinueWith( delegate
{
DoCreateGUID();
}, TaskContinuationOptions.PreferFairness );
}
}


}
public delegate void VoidDelegate();
}

[/code]

Share this post


Link to post
Share on other sites
Figured it out. The code that was checking for duplicate GUIDs was getting slower and slower as the listbox item list kept growing. Currently it's going at over 500k items strong and keeps going. Though, it still is somewhat slowing down just not until much much later. I think that may be an inherit cap in having a listbox with several hundred thousand (almost 750k now since the last sentece) entries.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this