• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 437
  • Last Modified:

to make multithread as singlethread

Greeting,

  Me dunno really how to workout the subject. Basically I already have a threading stuff.
  What it do is in Windows Form, when I click a button, a process start (which call a web service, so can imagine the waiting needed). I use threading here as the process take long, so not freeze the form and cursor. When the process done, it will invoke my callback delegate to display the results. Working as expected, no problem.

private void StartJob()
{
  ThreadedProcess objTP = new ThreadedProcess();
  objTP.StartTranslator(parameter...., new Callback_Doing(Doing), new Callback_Done(Done));
  objTP = null;
}

public void Doing(...) {...}

public void Done(...) {...}

But now I in another routine, which is generating the results onto a file, so:
  Do
  {
  StartJob();
  result grabbed from Done().... *** AAA
  Write result into File...
  } until all-done;

How do I worked out the -AAA- part?
I reuse the StartJob(), which spin off a thread, but I have to wait for the result grabbed in Done().
Should I lookinto ThreadPool, AutoResetEvent ?
I can't find more details/samples from the MSDN, any good ref would be good for me too.

TQ
0
Kelmen
Asked:
Kelmen
  • 2
1 Solution
 
AlexFMCommented:
If you want to write all results to file in when thread exits, do this in the Done function. If you want to write every record or some other portion of data, do this in Doing function. Worker thread can pass results through parameters, or write them to some object like ArrayList, recordset etc.
0
 
KelmenAuthor Commented:
I have no problem getting results back. Workaround is already done, but not using any threading approach.

I wanna see whether I can get the job done using threading way.
0
 
nehayaCommented:
use the keyword: 'lock' to hold an object as token.. see this:

object token = new object();
ArrayList result ;

// and in your calling order, call the Doing first , then WriteResult..

// first line in Doing:
lock(token)
{
// here goes the Webservice calling
// save result in 'result'
}

// fist line in WriteResult
lock(token)
{
// here goes the writing issues
}


lock will forbid any otherthread from entering between { } until the thread that has the lock leave the {} ..all threads will wait.


/// ANOTHER SOULTION:
use: Mutex obejct

see thius tutorial:
http://msdn.microsoft.com/library/en-us/cpref/html/frlrfsystemthreadingmutexclasstopic.asp?frame=true
0
 
KelmenAuthor Commented:
your input indeed work right for my topic title, but alas it doesn't really in my app context, but do give an idea.
Not your fault, by my, as I simplify my question to misleading, obviously.

At the end, I need to redesign the app.

Nevertheless, thx.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now