Threading help

  • Thread starter Thread starter Praveen
  • Start date Start date
P

Praveen

here is my code.
want to use multithreading.
Means, want to call DoThread() in a different thread and want output as

DefBegin
InThread
OutThread
DefEnd

is it possible?

internal class Sample
{
private static void Main(string[] args)
{
Program p = new Sample();
p.DefThread();



}


public void DoThread()
{
Console.WriteLine("InThread");
Thread.Sleep(500);
Console.WriteLine("OutThread");
}


private void DefThread()
{
Console.WriteLine("DefBegin");
Console.WriteLine("DefEnd");
}
}


thanks,
Praveen
 
using System;
using System.Threading;
using System.Windows.Forms;

internal class Sample
{
private static void Main(string[] args)
{
Sample p = new Sample();
p.DefThread();
Application.Run();
}

public void DoThread()
{
Console.WriteLine("InThread");
Thread.Sleep(1000);
Console.WriteLine("OutThread");
}

private void DefThread()
{
Console.WriteLine("DefBegin");

//option 1
MethodInvoker mi = new MethodInvoker(DoThread);
mi.BeginInvoke(DoThreadEnded, null);

//option 2
callback2 = new MethodInvoker(DoThreadEnded2);
new Thread(DoThread2).Start();

//option 3
//ThreadPool.QueueUserWorkItem(new WaitCallback(DoThread3), new
MethodInvoker(DoThreadEnded3));
}
public void DoThreadEnded(IAsyncResult ar)
{
Console.WriteLine("DefEnd");
}


MethodInvoker callback2;
public void DoThread2()
{
Console.WriteLine("InThread");
Thread.Sleep(1000);
Console.WriteLine("OutThread");
callback2.Invoke();
}
public void DoThreadEnded2()
{
Console.WriteLine("DefEnd");
}


public void DoThread3(object param)
{
Console.WriteLine("InThread");
Thread.Sleep(1000);
Console.WriteLine("OutThread");
((MethodInvoker)param).Invoke();
}
public void DoThreadEnded3()
{
Console.WriteLine("DefEnd");
}
}
 
Praveen said:
DefBegin
InThread
OutThread
DefEnd

In other words, you want the default thread to start a thread, and at
some point wait for the secondary thread to complete. You have two
basic options:

1) Explicitly create a Thread, Start it, and at some point in the
creating thread call Join on the new thread.

2) Create a delegate to the method you want to run threaded,
BeginInvoke the delegate, and at some point in the creating thread
call EndInvoke on the delegate.
 
Hi,

Try the following code in the DefThread method (2.0 framework):

Console.WriteLine("DefBegin");

// create a delegate to invoke DoThread
// (it just-so-happens that the DoThread method signature
// matches the ThreadStart delegate, so we'll use that)
ThreadStart doThreadAsync = DoThread;

// begin asynchronous invocation
doThreadAsync.BeginInvoke(
// create an anonymous method to end the invocation (AsyncCallback)
delegate(IAsyncResult result)
{
doThreadAsync.EndInvoke(result);
},
// block the current thread until DoThread has completed
null).AsyncWaitHandle.WaitOne();

Console.WriteLine("DefEnd");
 
Dave said:
Try the following code in the DefThread method (2.0 framework):

No, don't!
Console.WriteLine("DefBegin");

// create a delegate to invoke DoThread
// (it just-so-happens that the DoThread method signature
// matches the ThreadStart delegate, so we'll use that)
ThreadStart doThreadAsync = DoThread;

// begin asynchronous invocation
doThreadAsync.BeginInvoke(
// create an anonymous method to end the invocation (AsyncCallback)
delegate(IAsyncResult result)
{
doThreadAsync.EndInvoke(result);
},
// block the current thread until DoThread has completed
null).AsyncWaitHandle.WaitOne();

Console.WriteLine("DefEnd");

This is unnecessarily complicated. It's passing in a callback delegate
to call EndInvoke, then doing a WaitOne on the asynch call.

Much, much simpler to just call EndInvoke in the main thread:

Console.WriteLine("DefBegin");

// create a delegate to invoke DoThread
// (it just-so-happens that the DoThread method signature
// matches the ThreadStart delegate, so we'll use that)
ThreadStart doThreadAsync = DoThread;

// begin asynchronous invocation
IAsyncResult Cookie = doThreadAsync.BeginInvoke(null, null);

// do whatever, in starting thread

// Wait for secondary thread
doThreadAsync.EndInvoke(Cookie);

Console.WriteLine("DefEnd");

-
 
Hi Jon,

Yes, your approach is certainly simpler if an AsyncCallback isn't required.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top