Jump to content
  • Advertisement
Sign in to follow this  
kag1

Multithreading in C#

This topic is 4838 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, my whole purpose of trying to multithread is because I'm calling a blocking function from a C .DLL library..i'm trying to make it so if it blocks more than 60 seconds (give or take) it kills the blocking function.. before diving into Multithreading..is there an easier way to do this in C#? thanks, kag1

Share this post


Link to post
Share on other sites
Advertisement
As with most things in C#, it's pretty straightforward:


using System.Threading;

// ...

public class ProgramInstance
{
void BlockingMethod()
{
// call external DLL's blocking method
}
}

// ...

ProgramInstance pi = new ProgramInstance();
Thread externalThread = new Thread(new ThreadStart(pi.BlockingMethod));
externalThread.Start();


[edit: fixed syntax error]

[Edited by - kSquared on August 20, 2005 3:29:51 PM]

Share this post


Link to post
Share on other sites
What kind of a blocking function? Typically if the function is dependent upon something (like the network) they will let you specify a timeout. If not, then writing your code to use a thread is one way, although it is considered bad to force threads to terminate.

Share this post


Link to post
Share on other sites
Well the function is the Winsock recv blocking function..I'm sending out a GET request to links to see if they work or not..

Now that I think of it, I should try to work more on that side. I know I can use select to specify timeouts to see if it works, but it never reaches the timeout and the function then still blocks..

I'll look more into that side right now, but I still have a couple questions about multithreading...because it's not working..
here is what I have


void StartChecking()
{
CheckingThread = new Thread(new ThreadStart(CheckingLink));
CheckingThread.Start();
Timeout();
}
protected void CheckingLink()
{
//Blocking function -- kindof a funny name for a function, but i'm talking about the Header
Head = cLinkChecking.RequestHead(Links[j]);
}
protected void Timeout()
{
//I know it's not that accurrate but it doesn't need to be ;-)
int iTime = GetTickCount();
while (Temp + 60000 >= GetTickCount())
{
if (!CheckingThread.IsAlive)
break;
//Just Chillin here for 60 seconds..while I Thought the other function would be running
}
KillThread(CheckingThread);
}


I get different errors almost everytime..I'm not quite sure what i'm doing wrong..but I also don't know anything about multithreading...So anything you guys see would be great

Share this post


Link to post
Share on other sites
Quote:
Original post by kag1
I'm sending out a GET request to links to see if they work or not..

Why are you using "some C function" instead of the WebRequest class? It has asynchronicity built in.

Share this post


Link to post
Share on other sites
Also, if checking for whether a link works, you should issue a HEAD request instead of a GET. HEAD is equivalent to GET, only it doesn't return the body of the page.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!