Multithreading in C#
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
As with most things in C#, it's pretty straightforward:
[edit: fixed syntax error]
[Edited by - kSquared on August 20, 2005 3:29:51 PM]
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]
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.
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
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
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
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.
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement