Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Computer Forums > Programmers Lounge > Programming Discussions » C# synchronization problem
Closed Thread
Old 08-09-2005, 06:45 AM   #1 (permalink)
 
Ultra Techie

Join Date: Oct 2003

Posts: 544

fitzjj

Default C# synchronization problem

i have a function in a dll, say dllFunct()
i also have functions in my C#, say cFunct1() and cFunct2().

What i am having problems with is dllFunct() is quite big and if i run the code:

dllFunct();
cFunct1();
cFunct2();

in that order both cFunct1() and cFunct2() have finished before dllFunct(). This is kind of a problem since cFunct1 and cFunct2 rely on the result of dllFunct.

Now my initial thought was to deal with this as a thread but this wont solve the problem since even if it is dealt with in this way there is still no way of determining when dllFunct() has completed. If i use monitors or similar they will still exit before the call to dllFunct has completed.

anyone know of a way to deal with this problem? Is there a way to determine when a dll function call has completed besides busy waiting on the return value? (since there is not always a return value)

thanks
fitzjj is offline  
Old 08-09-2005, 09:35 AM   #2 (permalink)
 
Ultra Techie

Join Date: Sep 2003

Location: Bamberg, Germany

Posts: 549

Iron_Cross

Send a message via ICQ to Iron_Cross Send a message via MSN to Iron_Cross Send a message via Yahoo to Iron_Cross
Default

You've got like 50 options
The easiest, and least complex would be to do this:

Code:
public static void Main(string[] args)
{
    Thread dllThread = new Thread( new ThreadStart(StartDllThread) );
    dllThread.Start();
    dllThread.Join();
    cFunct1();
    cFunct2();
}

public void StartDllThread()
{
    dllFunct();
}
Of course that's not the only solution. You could also lock an object, use asynchronous callbacks, and tons of other options. But that should do what you want.
}
__________________

See today\'s Penny-Arcade!(May contain foul lanuage)
Pain is weakness leaving the body.

PM Me for my MSN
Iron_Cross is offline  
Old 08-09-2005, 12:26 PM   #3 (permalink)
 
Ultra Techie

Join Date: Oct 2003

Posts: 544

fitzjj

Default

Cheers for the reply iron cross. I too thought it was something along the lines of threads, but at the moment it appears not to be - for a slightly annoying reason...

The dll that i am calling functions in has various printf functions in order to help debugging. the C# has the same, so i would have expected that:

dllFunction();
cFunction();

would have written to the standard out in this order:

dllFunctionMessage
cFunctionMessage

however any text in the C# function wrote out to the screen in before the dllFunction text was written to std out. - this was my reason for assuming something fishy was up with the synchronization of the dll function calls.

After checking various outputs after the dll function calls it seems that the functions are returning before the next function (be it a C# or an exported function in the dll) is executed.

So it seems that the uotputs from the dll are buffered up and added to the end of the visual studio Output window once the C# application has finished executing. Whether this is just an issue with the Output window in visual studio or this is how windows in general handles calls to std out. i dont know, obviously there is the possibility that i am wrong here so if this sounds like utter crap then please let me know - at the moment i am going to continue coding and just ignore the order of the output
fitzjj is offline  
Old 08-09-2005, 03:51 PM   #4 (permalink)
 
Ultra Techie

Join Date: Sep 2003

Location: Bamberg, Germany

Posts: 549

Iron_Cross

Send a message via ICQ to Iron_Cross Send a message via MSN to Iron_Cross Send a message via Yahoo to Iron_Cross
Default

Are you being sure to use < output_buffer>.Flush() or are you just writing (and in C++ it would be writing "endl;" to the output buffer...
__________________

See today\'s Penny-Arcade!(May contain foul lanuage)
Pain is weakness leaving the body.

PM Me for my MSN
Iron_Cross is offline  
Old 08-10-2005, 01:58 PM   #5 (permalink)
 
Ultra Techie

Join Date: Oct 2003

Posts: 544

fitzjj

Default

Im using Console.WriteLine() in the C# and printf() in the DLL. It's not really a problem tho i'll just find another way of debugging - the thing is if it were a console app then it would write out to the console fine, just irritating i guess. Must admit i had not thought to flush any buffers though.
fitzjj is offline  
Old 08-10-2005, 04:33 PM   #6 (permalink)
 
Ultra Techie

Join Date: Jul 2005

Posts: 530

TheHeadFL

Send a message via AIM to TheHeadFL
Default

The solution he posted above is nice, however I don't know if the Join call blocks execution or if it lets it continue asynchronously.

You might also try creating a mutex (binary semaphore, whatever its called) (I dont know how in C#) that dllFunc sets and your main function waits on the mutex (binary semaphore) to be released and then continues.

However I think the best solution is asynchronous callbacks, as previously mentioned.
__________________
Desktop machine: 2 x Opteron 246, Asus K8N-DL, 2GB PC3200 ECC Reg., XFX GeForce 6600GT, 74gb WD Raptor, 2 x 19\" LCDs, Windows XP x64
Server machine: Intel P4 3.0GHz 2MB EM64T, ECS i865pe, 1GB PC3200, 36gb WD Raptor, Windows Server 2003
Laptop: Dell Inspiron 9100 (Intel P4 3.2GHz 1MB Prescott, i865pe, 512MB PC3200, Mobility Radeon 9700, DVD+R/DL Burner), Windows XP
Linux: P3 450Mhz, 386MB ram, Slackware 10.1 (Running mySQL/Apache)
TheHeadFL is offline  
 
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On