Saania

Hi,

I have a dll in VC# (Visual Studio 2005). The dll was created by me and it has threads for sending and receiving data over sockets. ( Actually it was a server code I was working on and now I needed to integrate with a third party tool which is in VC++, so I created a dll). As the threads had infinite while loops in them I was using Application.DoEvents before calling the while(true) statements in the VC# server. When I created the dll I had to remove the doevents part ( as there are no forms in a dll).

Now when I use the dll in the VC++ code, it really slows down the application ( I am normally rendering frames at 40fps but when the sockets start sending and receiving I only get 1 or 2 fps). So my first question is that is there a way to use doevents in a dll

I tried another thing, I moved the threads ( that is, sender and receiver) to VC++ and was calling the code in the dll from the VC++ thread ( in a while loop ) and I used the DoEvents before the while loop in VC++. Since I have to use the same sockets for sending and receiving, I created an instance of the VC# class, created the sockets and passed the instance of this class to the threads. But in this case I am getting an AccessViolation error. I am using mutex->WaitOne() before using the class in the thread. Is there some other way of doing this

I know its a lot of information in a single mail, but could someone please help me out here. A solution for any of the questions above would solve my problem.

Thanks

Saania



Re: Visual C++ General Multithreading and DoEvents.

Simple Samples

Yes there is a lot of information and I think that causes the important information to be unclear. The information you provide that is not about C++ might be helpful but it is difficult for me to understand what you are saying about C++.

I am not familiar with the CLI/CLR stuff so I can't be sure if DoEvents is part of that but DoEvents is deffinitely not part of the C++ language. I cannot be sure if the DoEvents you are using works well with multithreading but I doubt that it is a good solution. DoEvents was and is a VB thing to allow VB programmers to do things they could not do due to limited access to Windows. A C++ programmer can use the Windows API directly and fully. So the solution to your problem is likely to do things the Windows API way.

DoEvents is not multithreading. You don't need DoEvents to do multithreading. Perhaps you are not, but it is difficult for me to understand what you are doing.






Re: Visual C++ General Multithreading and DoEvents.

Saania

Yes I know what doevents is for. And I am not using it in multithreading. Let me narrow what I am asking to what is related to C++ specifically.

I am passing an instance of an object ( a class in a dll) to two threads created in C++ using CreateThread function. since the same object is being used in both the threads. I declared a mutex in each of the threads and used mutex->waitone before creating a local instance of the object and then I close the mutex.

But when I execute the program I am getting AccessViolation error at the point where I create the class object. Am I doing something wrong here How can I use Lock (as in VC#) in VC++.NET

Second, my main thread ( that creates the two threads) terminates before the two threads return. I have tried using AutoResetEvent (or ManualResetEvent) to wait for the trigger from the threads, but all the help I see on the net relates to creating a ThreadPool for the threads. And I dont know how to use the ThreadPool and pass the LPVOID parameter ( as you do with CreateThread function to pass the class object in the dll). The object state passed in a threadpool is used to trigger SET (OR RESET) function of the AutoResetEvent, so how can I pass my object to a ThreadPool

I hope I make more sense here. Forget about the DoEvents part.

Thanks for the reply Simple Samples. I await further help.





Re: Visual C++ General Multithreading and DoEvents.

Simple Samples

The forums were having problems earlier or something was a problem so I was not able to get to this earlier.

You have many questions and most of the secondary questions seem to not be C++ questions. I don't know how much I can help you with those but I will assume that the access violation problem is a C++ topic. I do not however understand how the mutex relates to the access violation. I think you need to focus on the one problem only for now. Can you please provide more information about the access violation I assume you know how to find the source code line where the access violation occurs. Can you tell us what that is What is your program doing when it gets the access violation

Your subject is "Multithreading and DoEvents". That is confusing. I don't know what the subject is actually intended to be. It is difficult for us to help without a clear description of the problem.






Re: Visual C++ General Multithreading and DoEvents.

Saania

Thanks for the reply once again.

Actually in my second post I did mention all the problems I was having with C++ only. Initially I wanted solution to two separate things, one multithreading and second doevents. if we just forget about the doevents thing, my multithreading code (written in VC++.NET) has two problems ( Its been ages since I programmed in VC++ so I know how to do the things in VC# I cant find what I am looking for in C++).

I have two threads ... one as a sender and the other a receiver for data sent over sockets. But the sockets are being created and handled in a dll ( a VC# dll which has a class named Class1). so when I call these threads I have to pass the object of the class in the dll because the sender and receiver use the same sockets right!!.

Here is what I have in the sender and receiver threads... ( and I dont know a work around for the DoEvents thing, because I have to enforce the processor to do other tasks while the thread is in a while loop .... and I have removed the doevents completely as well and it doesnt make any difference. So the problem is not with doevents).

DWORD WINAPI SenderThread(LPVOID iValue)

{

Mutex^ mut = gcnew Mutex();

mut->WaitOne();

Class1^ senderdll = *((Class1^*)iValue);

mut->Close();

int returnval;

DoEvents();

while(true)

{

returnval = senderdll->SenderEntryPoint();

if(returnval == 1) // sockets closed

{

break;

//dynamic_cast<AutoResetEvent^>(stateInfo)->Set();

}

}

return 0;

}

DWORD WINAPI ReceiverThread(LPVOID iValue)

{

Mutex^ mut = gcnew Mutex();

mut->WaitOne();

Class1^ receiverdll = *((Class1^*)iValue);

mut->Close ();

int returnval;

DoEvents();

while(true)

{

returnval = receiverdll->ReceiverEntryPoint();

if(returnval == 1) // the sockets closed

break;

}

return 0;

}

and here is how I am creating these threads. (The cmdListen_Click function creates the sockets.)

Class1^ c = gcnew Class1();

c->cmdListen_Click(NewPosition);//call the dll function to create sockets

////Threads start

DWORD dwGenericThread;

hThread2 = CreateThread(NULL,0,SenderThread,&c,0,&dwGenericThread);//the sender thread

if(hThread2 == NULL)

{

DWORD dwError = GetLastError();

return 0;

}

hThread3 = CreateThread(NULL,0,ReceiverThread,&c,0,&dwGenericThread);//the receiver thread

if(hThread3 == NULL)

{

DWORD dwError = GetLastError();

return 0;

}

//////Threads end

Now the two problems I am facing are:

1) How do I synchronize the threads The AutoResetEvent (as given in http://msdn2.microsoft.com/en-us/library/system.threading.autoresetevent.aspx), uses a threadpool to create threads. My problem is if I use the threadpool instead of using the CreateThread how do I pass the Class1 object in the threads

2) My second problem is the AccessViolation error, which I am getting at the point where I create the instance of the Class in the thread. ( in both sender or receiver, depending upon which thread I am creating first !!).

Class1^ receiverdll = *((Class1^*)iValue);

What am I doing wrong here When I was getting an access violation error in VC#, I used LOCK to enforce resource allocation. How do I do this in VC++





Re: Visual C++ General Multithreading and DoEvents.

Simple Samples

 Saania wrote:

Second, my main thread ( that creates the two threads) terminates before the two threads return. I have tried using AutoResetEvent (or ManualResetEvent) to wait for the trigger from the threads, but all the help I see on the net relates to creating a ThreadPool for the threads. And I dont know how to use the ThreadPool and pass the LPVOID parameter ( as you do with CreateThread function to pass the class object in the dll). The object state passed in a threadpool is used to trigger SET (OR RESET) function of the AutoResetEvent, so how can I pass my object to a ThreadPool

AutoResetEvent and ManualResetEvent are part of the CLR or something such as that. I don't know if that is in or out of scope for this forum but I can't help with the CLR or whatever it is that includes AutoResetEvent and ManualResetEvent.

 






Re: Visual C++ General Multithreading and DoEvents.

Simple Samples

In:

Class1^ c = gcnew Class1();

You are using c for both the sender and the redeiver; are you sure that is what you need

I am familiar with gcnew vaguely but I don't know the details of how garbage collection works. Is it possible that gc deletes c in spite of it's use for the threads If that is possible, then it is likely to be the cause of the access violation.