to make multithread as singlethread

Posted on 2006-03-23
Last Modified: 2012-06-21

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

Question by:Kelmen
    LVL 48

    Expert Comment

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

    Author Comment

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

    Accepted Solution

    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:
    // here goes the Webservice calling
    // save result in 'result'

    // fist line in WriteResult
    // 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.

    use: Mutex obejct

    see thius tutorial:
    LVL 5

    Author Comment

    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.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
    This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…

    732 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now