Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 132,362 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,270 people online right now. Registration is fast and FREE... Join Now!




Pausing a thread?

 
Reply to this topicStart new topic

Pausing a thread?, I hate threads (-_-)'

gabehabe
post 2 Oct, 2008 - 08:02 AM
Post #1


Working Girl.

Group Icon
Joined: 6 Feb, 2008
Posts: 5,402



Thanked 94 times

Dream Kudos: 2625

Expert In: Dingleberries

My Contributions


Back again.

I've got a thread, which runs regularly to update the recent posts on Twitter (Check the X-Twitter thread in Discuss Projects)

But the problem is, when you click the button to update a message, it freezes. I know why this is, I just don't know how to fix it.

It's freezing because it's trying to post to a webpage while reading another. Well, that would work, if my connection wasn't so crap. Sooo~ I need to pause the update thread and make the main thread priority when the button is clicked. After that, it can go back to listening.

I don't really have any code to post for this, since it's more of a how-to than "what's wrong."

I've read about ManualResetEvent and AutomaticResetEvent, but I'm not sure what to do with them.

NOTE: I'm trying to pause the update thread from the main thread.

Any ideas?

Thanks in advance.
-Danny
User is offlineProfile CardPM

Go to the top of the page

Jayman
post 2 Oct, 2008 - 08:34 AM
Post #2


Student of Life

Group Icon
Joined: 26 Dec, 2005
Posts: 6,839



Thanked 38 times

Dream Kudos: 500

Expert In: C#, VB.NET, Java

My Contributions


You might want to look into Asynchronous threading. Using this type of implementation you can poll the thread to determine the completion status of the thread before moving on to the next thread.

Check out the following MSDN link about midway down the page.
Polling for Asynchronous Call Completion
User is offlineProfile CardPM

Go to the top of the page

gabehabe
post 2 Oct, 2008 - 10:20 AM
Post #3


Working Girl.

Group Icon
Joined: 6 Feb, 2008
Posts: 5,402



Thanked 94 times

Dream Kudos: 2625

Expert In: Dingleberries

My Contributions


urgh, I've always hated threading.

Guess I'll just have to grit my teeth and bare with it sleep.gif

Thanks jayman icon_up.gif
User is offlineProfile CardPM

Go to the top of the page

baavgai
post 2 Oct, 2008 - 11:52 AM
Post #4


Dreaming Coder

Group Icon
Joined: 16 Oct, 2007
Posts: 1,962



Thanked 96 times

Dream Kudos: 475

Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions


Hard to do.

A possible solution would be some kind of job queue. Basically, one thread, separate from the main thread, that processes operations sequentially.

Here's what I'm thinking:
csharp

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Diagnostics;

namespace JobTest {
class JobQueue {
private Job defaultJob = null;
private bool keepGoing = false;
private int runNum = 0;
private Queue<Job> pendingJobs = new Queue<Job>();

public delegate int Job(); // return value is wait time

public JobQueue(Job defaultJob) {
this.defaultJob = defaultJob;
}

public void AddJob(Job job) { pendingJobs.Enqueue(job); }

public void Run() {
keepGoing = true;
while (keepGoing) {
Debug.WriteLine("Job " + ++runNum);
Job job = (pendingJobs.Count > 0) ? pendingJobs.Dequeue() : this.defaultJob;
int sleepTime = job();
if (keepGoing && sleepTime > 0) {
System.Threading.Thread.Sleep(sleepTime);
}
}
}

public void Stop() {
keepGoing = false;
}

}

class Program {
static int Ajob() {
Debug.WriteLine("AJob");
return 1000;
}

static void Main(string[] args) {
JobQueue jc = new JobQueue(delegate() { Debug.WriteLine("Default Job"); return 2000; });
new Thread(new ThreadStart(jc.Run)).Start();
System.Threading.Thread.Sleep(2000);
jc.AddJob(Ajob);
jc.AddJob(Ajob);
jc.AddJob(new JobQueue.Job(delegate() { Debug.WriteLine("Another Job"); return 0; }));
jc.AddJob(Ajob);
System.Threading.Thread.Sleep(1000);
jc.AddJob(Ajob);
System.Threading.Thread.Sleep(10000);
jc.Stop();


}
}
}


If you want to be fancy, you can throw events, etc. Go nuts.
User is online!Profile CardPM

Go to the top of the page

gabehabe
post 2 Oct, 2008 - 01:22 PM
Post #5


Working Girl.

Group Icon
Joined: 6 Feb, 2008
Posts: 5,402



Thanked 94 times

Dream Kudos: 2625

Expert In: Dingleberries

My Contributions


Wow. I think that completely answered my question, and more. Thanks baavgai icon_up.gif

If that isn't already a snippet, submit it~ I'll approve it right away.
(Though it could do with a few comments)

One question though~ what's this "System.Linq" ?
I saw it in another thread earlier.
I had to take it out when I was compiling, it said it wasn't found?
User is offlineProfile CardPM

Go to the top of the page

baavgai
post 2 Oct, 2008 - 01:53 PM
Post #6


Dreaming Coder

Group Icon
Joined: 16 Oct, 2007
Posts: 1,962



Thanked 96 times

Dream Kudos: 475

Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions


QUOTE(gabehabe @ 2 Oct, 2008 - 05:22 PM) *

If that isn't already a snippet, submit it~ I'll approve it right away.
(Though it could do with a few comments)


Eww, comments are for wimps. wink2.gif I'll see what I can do.

QUOTE(gabehabe @ 2 Oct, 2008 - 05:22 PM) *

One question though~ what's this "System.Linq" ?


New toy in VS 2008, LINQ, so they insist on forcing it in by default. It's basically a way to derive a resultset from a collection in a supposedly more intuitive syntax. I believe it's what happens with SQL people and LISP people get hammered and start messing with the language specs. I honestly didn't see anything it could do for me that I couldn't do just simply with more natural feeling methods. This site seemed to have a lot of good examples.

User is online!Profile CardPM

Go to the top of the page

gabehabe
post 2 Oct, 2008 - 02:28 PM
Post #7


Working Girl.

Group Icon
Joined: 6 Feb, 2008
Posts: 5,402



Thanked 94 times

Dream Kudos: 2625

Expert In: Dingleberries

My Contributions


Gotcha. Thanks icon_up.gif

As for the JobQueue, it works wonders~ I posted a couple of test from X-Twitter while I was testing it. (I've created a JobQueue.cs file, and put you as the author~ When I come to the credits, I'll put you in.)

Thanks again, it really helped. smile.gif
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/22/08 04:59AM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month