Sign in to follow this  

[.net] Beginner Multithreading Question

This topic is 4336 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. I'm curious if what I want to do is possible to do with Mult-threading, if it's a good time for mult-threading and maybe some insights on how to do it in MultiThreading.. ------------- I will have an array of about 10,000 objects called cLink..here is sort of the contents of cLink
class cLink
{
public string Title;
public string Address;
public string StatusCode;
...
};
; and I have one
 CheckLink(cLink LinkToCheck)
function that checks the status of the link, and also saves the bad links into an XML file with an XML Writer. -------------- Right now this takes hours and hours to complete...and it's something that we do almost everyday...I just thought maybe having 10 different threads running at the same time would increse the overall efficiency. That way if one link times out...instead of wasting 30 seconds on it, the program will keep on chuggin. Thanks a Bunch ArchG

Share this post


Link to post
Share on other sites


What if I put all those 10,000 objects of cLink, into an XML File...then did something like this

in my CheckLink Function


public void CheckLink()
{
lock (this)
{
cLink Link = ReadALink("Links.XML"); // Load Link into memory
DeleteThatLinkFromXMLFile("Links.XML"); //delete it from disk
}
Link = GoToWebAndCheckLinkStatusCode(Link)
if (Link.StatusCode != 200) //BAD LINK
{
lock (this)
{
SaveToXMLFile(Link, "BadLinks.XML");
}
}
}

I'm not sure if that's how it's done or not...but it seemed like a good idea when I thought of it. Would something like that work? I would try it and test it out..but this project is a rather big one and would take more than just a couple lines of code to get it going..(the example is pretty cut down just to illustrate what i'm trying to do).

Again any help is appreciated and you will be rewarded via a small ratings increase ;-)

Share this post


Link to post
Share on other sites
First you have to determine which part of the process is slow, although at a guess I'd say it's the file IO? Depends on how things are getting buffered internally, I guess. You could try using asynchronous IO (look up BeginWrite), or also writing them into a MemoryStream instead and serializing that to disk at the end. Multithreading will help if you're on a multiprocessor system, otherwise you're better off just doing async IO.

Share this post


Link to post
Share on other sites
He said it was the waiting for the links to verify / timing out, so he's not IO bound, it's internet-bound.

Your code looks good; just make sure that the functions you're using in your GoToWebAndCheckLinkStatusCode are multithreading-safe (should say in the docs). I guess they are but best be safe!

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
just one note:

never lock on "this"
in general it is not a good idea to lock on an object that is visible outside the class
someone might just lock on the same object without knowing you lock the same and this can lead to a deadlock

like this:

class A
{
public void SomeFunction()
{
lock( this )
{
dosomething();
}
}
}

class B
{
private A aMember;

public void AnotherFunction()
{
lock( aMember )
{
aMember.SomeFunction(); //

when you write your class you have no control over what of its public members (or itself) will be used as lock in some other place. at least you have to document it well what is locked when.

it is good habit to use lock this way:


class C
{
private object LockObject = new object();

public void foo()
{
lock( LockObject )
{
...
}
}
}


this way you make sure that this lock will never end in a deadlock due to misuse outside of it. the only code that can produce a deadlock is inside that class. so when you write that class properly you will never have issues with its locks.

Share this post


Link to post
Share on other sites

This topic is 4336 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this