hi techies,
i am developing a c# windows applications.
my requirement is similar to below
void myfunction()
{
// here i am calling another function
createreport(id,name,num);
//rest of the code
}
if createreport function takes more than 1 minute to execute,i want to stop that method and continue rest of the code.
please suggest me how can i do..thanks in advance
Loading

Jignesh TrivediPosted May 25, 2012, 6:17 AM
Yes my friend, I forgot start thread..
Thanks A lot.
@Sandeep
Try..
var t1 = new Thread(CreateReport);
t1.Start();
Thread.Sleep(5000);
hope this help.
EhteshamPosted May 25, 2012, 5:27 AM
With do respect, what this code is going to do
var t1 = new Thread(CreateReport); //Task.Factory.StartNew(CreateReport);
Thread.Sleep(5000);
As their is no constructor defined for Thread which directly takes Method Address and also you are not starting the thread plus you are pausing(sleep) the primary thread not t1 which you planned to run CreateReport on
Jignesh TrivediPosted May 25, 2012, 5:20 AM
just replace two line in my code..
var t1 = new Thread(CreateReport); //Task.Factory.StartNew(CreateReport);
Thread.Sleep(5000);
but it always wait for 5 sec.
hope this help.
EhteshamPosted May 25, 2012, 5:05 AM
delegate void AddDelegate(int Max);
void Add(int Max)
{
for (int Index = 0; Index < Max; Index++)
{
System.Threading.Thread.Sleep(1000);
}
}
private void button1_Click(object sender, EventArgs e)
{
AddDelegate DelegateObj = new AddDelegate(this.Add);
IAsyncResult Result = DelegateObj.BeginInvoke(100, null, null);
//waits for 1 min
Result.AsyncWaitHandle.WaitOne(60000, true);
//you other code here
//
//
//
//
DelegateObj.EndInvoke(Result);
}
sandeep raju dantuluriPosted May 25, 2012, 4:35 AM
can we do this in framework 3.5..?
Jignesh TrivediPosted May 25, 2012, 3:12 AM
I think you can use threading in this case.
Try...
Example Defination of CreateReport is..
public static void CreateReport()
{
Thread.Sleep(7000); // use to hold value up to 7 sec
}
now try...
Console.Write("Jignesh Test");
Task t1= Task.Factory.StartNew(CreateReport);
t1.Wait(5000);
Console.Write("Jignesh Test");
"Wait" methos wait for 5 sec, if youe callback not comming within 5 sec then it run rest of the code.
hope this will help u.
EhteshamPosted May 25, 2012, 2:36 AM