Multihreading C# Diploma Project Help

IncognitoMan
IncognitoMan used Ask the Experts™
on
Hello guys. I really need help. After less than a week I have to make a domanstration on my diploma project. But it has got a serious bug!
The project contains two threads that handle the voice and the face of a virtual agent.
So the problem is, that there are two threads. The first one, controls the voice. It works OK. But sometimes, one of every five speeches, the second thread, that handles the images, does not start. The second thread changes two pictures. The first one is the agent with closed mouth and the second with closed.
So first I create the threads:
Thread th1;
Thread th2;
After that i assign them a funcion and call them.
th1 = new Thread(ThreadFunc1);
th2 = new Thread(ThreadFunc2);
ThreadFunc1/2 are the functions of the threads.
As I said before, the first thread works fine. But sometimes the socunt funcion doesnt work or... I don't know what happens. Here is its code

public void ThreadFunc2()
{
this.BeginInvoke(new MethodInvoker(delegate()
                {
   //do some calculations
 
   //begin changing the images of the virtual agent.
for (int i = 1; i <= vreme + 1; i++)
                    {
                        pictureNormal.Image = (Image)Properties.Resources.ResourceManager.GetObject("avatar normal");
                        this.Refresh();
                        Thread.Sleep(300);
                        pictureNormal.Image = (Image)Properties.Resources.ResourceManager.GetObject("avatar talk");
                        this.Refresh();
                        Thread.Sleep(300);
                    }
                    pictureNormal.Image = (Image)Properties.Resources.ResourceManager.GetObject("avatar normal");
 }));

So this part changes the picture and makes the thread sleep, after the time is up, lit makes the puts the image of the agent with closed mouth, so it wont be with opened.

But as I said before sometimes it doesnt work. The agent talks but his mouth stays still.
Maybe then I make Thread.Sleep(300); it sleeps the wrong thread and the hole operation just executes rapidly. I don't know I'm out of ideas. Help me pls it's URGENT!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Have you tried using ThreadPool.QueueUserWorkItem to call a method on another thread?  You can use this to run methods in background threads.  You can call ThreadPool.QueueUserWorkItem multiple times, even if the threads need to run simultaneously.  You can have a default of 250 worker threads per available processor I think.  Sample code is provided below.    Also, check the links below for reference:

http://msdn.microsoft.com/en-us/library/system.threading.threadpool.queueuserworkitem.aspx
http://msdn.microsoft.com/en-us/library/4yd16hza.aspx
http://msdn.microsoft.com/en-us/library/kbf0f1ct.aspx
static void Main(string[] args)
{
    ThreadPool.QueueUserWorkItem(ThreadFunc1,"Thread 1");
    ThreadPool.QueueUserWorkItem(ThreadFunc1,"Thread 2");
    ThreadPool.QueueUserWorkItem(ThreadFunc1,"Thread 3");
    //By the way, if you want to call or execute the method in a
    //foreground thread, you call the method per normal call (ThreadFunc1("Thread 4");)
}


static void ThreadFunc1(Object stateInfo)
{
    //Note that you are now in one of the background threads calling this method.
    Console.WriteLine("Hello from the thread pool.");
}

Open in new window

By the way, I forgot something, you need to put a Thread.Sleep after executing a method in a background thread.  See the 2nd link example in my previous post for details.

ThreadPool.QueueUserWorkItem(ThreadFunc1,"Thread 1");
Thread.Sleep(1000);


Author

Commented:
Alfred1, the thread sleep method is in the ThreadFunc2 funciotn. And I cant put it after executing the method in a background thread, because it's in the method. Do you cant me do send you the whole code of the form?
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Commented:
Try loading the image objects into variables held in the displaying form.  Instead of loading from resources every 300 milliseconds and trying to repaint you can display from the ones held in the variables.
I don't see a problem with your threading code, but even if there is one there's a chance that your loading/casting from resources is taking enough time that its adding to the fault (its a decently big operation your repeating every 300ms for no reason).  Anyway, it should only take a few minutes to make the change and it might help.
PS:  since your operation is occurring every 300 ms it would be much easier to use a timer for the same operation.  It will generate all the appropriate interrupts and it can be built in to your form in the first place, negating the need for delegates.
-w00te

Author

Commented:
I don't know why, but your solution solved the problem. I am going to test it a little more and if there are no more problems I will assign you the 500 points. You did e great job. Thank you Alfred1.

w00te, I don't see an error in my code too. That was strange and confusing for me. And I was afraid to go with no working diploma project on my main exam. But now the fear is off.

Author

Commented:
Alfred1, your solution totally solved all of my problems. Today I went there and gave everything of me. I showed them all I can do. They kept asking me question on the thing I studied in this university, but I resisted. Thanks to you I have an A+ and I can proudly call myself computer engineer.

Now I am giving you the A you deserve.

Author

Commented:
He really deserve to be a specialist.
Hi IncognitoMan,

I am just glad to help and good on you in getting an A+.    :-)

Regards,

Alfred

Author

Commented:
Alfred1, may god's powers and the spirits of all good shine upon your path :)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial