How to modify my class to be multi-threaded instead of Static?

This is my interface class:
public partial class POptions : object, System.Runtime.Serialization.IExtensibleDataObject
private static Interfaces.Entities.POptions _POptions = new Interfaces.Entities.Options();

The UI class declares this way:
pOptions = pForm.Variables["POption"] as Interfaces.Entities.POptions;

The situation is we need to make this multithreaded so multiple requests can be queued and processed when the previous request has completed (or failed).

I've been looking at ConcurrentDictionary and ConcurrentQueueu, but not sure if they are the best tools for this job or not.

Please let me know your advice.

Thanks
JBM2015Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ste5anSenior DeveloperCommented:
Please post a concise and compete example.

Also static and multithreaded are not necessarily exclusive.

It's also not clear what you're want to parallelize, cause you have only posted declarations. And you're talking of requests, but the code shows only options.

Please rephrase your question.
0
dameyCommented:
I agree with ste5an however with the information you did provide, for what you want you'll have to create a Treading class and use that class to invoke a thread on your interface objects.
There is a lot to learn about how to correctly use  threading, locking, sleeping, concurrency... and the list goes on.
I prefer using Thread.CreateBackgroundWorker() over managing multiple threads, it allows you to get an async call for progress easily and you can create an array of them, I usually use a queue object to handle the requests  and assign the request to create or assign the BackgroundWorker.
Since you have the class as static already ... That is good because static class are thread safe by nature.
Here is some sample code you could use in your main form or from where ever your needing to create a thread.

private BackgroundWorker assignmentWorker;

    private void InitializeBackgoundWorkers() {
        assignmentWorker = new BackgroundWorker();
        assignmentWorker.DoWork += AssignmentWorkerOnDoWork;
        // ...
    }

    private void AssignmentWorkerOnDoWork( object sender, DoWorkEventArgs doWorkEventArgs ) {
        for( var f = 0; f < FilesToProcess; f++ ) {
            var fileProcessed = false;
            while( !fileProcessed ) {
                for( var threadNum = 0; threadNum < MaxThreads; threadNum++ ) {
                    if( !threadArray[threadNum].IsBusy ) {
                        Console.WriteLine( "Starting Thread: {0}", threadNum );

                        threadArray[threadNum].RunWorkerAsync( f );
                        fileProcessed = true;
                        break;
                    }
                }
                if( !fileProcessed ) {
                    Thread.Sleep( 50 );
                    break;
                }
            }
        }
    }

    private void button1_Click( object sender, EventArgs e ) {
        assignmentWorker.RunWorkerAsync();
    }
0
JBM2015Author Commented:
Let me try to be more concise and clear.

Thanks for the feedback, my solution may not need to be that complex.  I'm thinking if I can get this object to use a ConcurrentDictionary, that would get me where I need to go.

Current:
private static Interfaces.Entities.POptions _POptions = new Interfaces.Entities.POptions();

Proposed:
private static ConcurrentDictionary<Guid, Interfaces.Entities.POptions> _POOptions2 = new ConcurrentDictionary<Guid, Interfaces.Entities.POptions>();


In testing this Concurrent dictionary, it's not recognizing the data member of the POptions class, so I think my declaration is incorrect.

This is a snippet from the POptions class:

namespace Interfaces.Entities
{
public partial class POptions : object, System.Runtime.Serialization.IExtensibleDataObject
{

private System.Runtime.Serialization.ExtensionDataObject extensionDataField;
private Guid _newGuid;

public System.Runtime.Serialization.ExtensionDataObject ExtensionData
        {
            get
            {
                return this.extensionDataField;
            }
            set
            {
                this.extensionDataField = value;
            }
        }

[DataMember]
        public bool IsProcessing
        {
            get { return _isProcessing; }
            set { _isProcessing = value; }
        }

  [DataMember]
        public Guid newGuid
        {
            get { return _newGuid; }
            set { _newGuid = value; }
        }
    }
}


Thanks
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

ste5anSenior DeveloperCommented:
I still don't see any reason why using threading should be a poblem.. But you still have not posted what you're trying to achieve. You only included some setup data. Here we - or at least I - cannot give any advice. Not enough information about your context.

btw, you have a single class

private static Interfaces.Entities.POptions _POptions = new Interfaces.Entities.POptions();

So why should anyone need a dictionary? This would mean having multiple classes. You have nothing said about that.

Maybe you need a ConcurrentQueue to stuff in multiple options and let your threads work on each one in the correct order. But as I said: just guessing.
0
JBM2015Author Commented:
I wanted to know how to declare the Concurrently dictionary to store multiple instances of the POptoins class.  I did this:

private ConcurrentDictionary<Guid, Interfaces.Entities.POptions> PQueue = new ConcurrentDictionary<Guid, Interfaces.Entities.POptions>();


And how to add items to the ConcurrentDictionary, I'm using this code:

if (PQueue.TryAdd(_POptions.newGuid, _POptions))
                            {
                                Console.WriteLine("Item did not exist, now inserted.");
                            }
                            else
                            {
                                Console.WriteLine("Item already exists, insert failed.");
                            }


Here's where I loop through the ConcurrentDictionary to process the items and remove if successful:

Interfaces.Entities.POptions _removePOptions = new Interfaces.Entities.POptions();
               
                foreach (var qItem in PQueue)
                {
                    var a = PQueue.First();

                    bolProcessed = false;
                    Execute(ou.Value, _SessionID, _CustomerID, null);

                    if (bolProcessed == true)
                    {
                        PQueue.TryRemove(ou.Key, out _removePOptions);
                    }
                }
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JBM2015Author Commented:
Figured it out on my own.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.