Multithreading in C#

Started by
4 comments, last by Arild Fines 18 years, 8 months ago
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
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]
- k2"Choose a job you love, and you'll never have to work a day in your life." — Confucius"Logic will get you from A to B. Imagination will get you everywhere." — Albert Einstein"Money is the most egalitarian force in society. It confers power on whoever holds it." — Roger Starr{General Programming Forum FAQ} | {Blog/Journal} | {[email=kkaitan at gmail dot com]e-mail me[/email]} | {excellent webhosting}
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.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

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
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.

--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
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.
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]

This topic is closed to new replies.

Advertisement