user166033
user166033

Reputation:

How to wait for particular function to complete for starting next function? Without using thread in C#

I have two functions.

Function1(varName);
Function2();

called in same sequence. Here String varName is assigned by another function call which is in thread.

But here I want Funtion2 to complete before another value is assigned to varName (Funtion1 is called).

regards, Sagy

example:

private void MainFuntion()
{
   Thread StartReadThread = new Thread(
            new ParameterizedThreadStart(StartRead));
   StartReadThread .Start(obj_ListDictionary); //ListDictionary object 
}

private void StartRead(object threadData)**
{
  ThreadPool.SetMaxThreads(4, 4);
  m_Events = new ManualResetEvent[m_objSelNsfDataTable.Rows.Count];
  foreach (DataRow objRow in m_objSelNsfDataTable.Rows)
  {
    m_objThreadData                 =   new ThreadData();
    m_objThreadData.FilePath         =   objRow[0].ToString();

    m_objThreadData.ThreadIndex      =   index;
    m_Events[index]                  =   new ManualResetEvent(false);

    WaitCallback objWcb             =   new WaitCallback(FinalFunction);
    ThreadPool.QueueUserWorkItem(objWcb, m_objThreadData);

    index++; 
  }                               

  WaitHandle.WaitAll(m_Events); 
}

private void FinalFunction(object threadData)
{

  ThreadData threadData = (ThreadData)passedThreadData;
  String FilePath = threadData.FilePath;
  CopyContent(FilePath );                    
  OpenFolderForView();

}

Upvotes: 0

Views: 189

Answers (3)

Zote
Zote

Reputation: 5379

If Function1 calls the thread, you can use thread.Join before exits/return.

Upvotes: 0

Andriy Volkov
Andriy Volkov

Reputation: 18923

Read this (explains how to do things like that in .NET 3.x and also 4.0)

Upvotes: 0

x2.
x2.

Reputation: 9668

string tempVarName = varName;
Function1(tempVarName);
Function2();
varName = tempVarName;

Upvotes: 3

Related Questions