Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have two methods in my win app.

C#
private void zim()
{
//code
}


and

C#
private void zimtim()
{
//code
}




I have to access both methods on button_Click event.but zimtim(); must be called after 10 seconds of zim();

any help?
Posted
Comments
Sergey Alexandrovich Kryukov 14-Jul-14 1:20am    
Why? What is the application type? UI library you use?
—SA
Member 10579673 14-Jul-14 1:29am    
its a jabber application

first method is for connection.
and second for joining conference.

so connection takes atleast 10 seconds .without being connected i cannot join conference.thats why need 10 seconds gap between the two
Sergey Alexandrovich Kryukov 14-Jul-14 1:31am    
I answered, but this 10 sec looks like total abuse. What if 10 sec is not enough? You need to radically review your code design.
—SA
Maciej Los 14-Jul-14 2:05am    
That's the anwer!

What you describe is not exactly "one by one".

I'm almost sure ([EDIT] quite sure after the OP's comment in reply to my comment to the question[END EDIT]) this requirement makes no sense, but you should call one method, then set a timer for the delay you need, and in the timer event handler, call the second method. Note that this method will be called asynchronously to you UI, so you have to be prepared to deal with that.

The choice of the timer depends on several factors, including the UI you are using. For example, for System.Windows.Forms, you can use System.Windows.Forms.Timer, but this timer has extremely bad accuracy, but I can imagine that, with 10 sec, you would not care much. Then, you can do whatever you want in the timer even handler. With some other timers, you cannot directly call any UI methods/properties, because the timer event can be called in some other thread. Besides, you can use a thread instead of the timer.

You cannot call anything related to UI from non-UI thread. Instead, you need to use the method Invoke or BeginInvoke of System.Windows.Threading.Dispatcher (for both Forms or WPF) or System.Windows.Forms.Control (Forms only).

You will find detailed explanation of how it works and code samples in my past answers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

See also more references on threading:
How to get a keydown event to operate on a different thread in vb.net[^],
Control events not firing after enable disable + multithreading[^].

With WPF, you can use System.Windows.Threading.DispatcherTimer which also runs on a separate thread, so you would need to use the mechanism described above:
http://msdn.microsoft.com/en-us/library/system.windows.threading.dispatchertimer%28v=vs.110%29.aspx[^].

—SA
 
Share this answer
 
v2
Comments
Maciej Los 14-Jul-14 2:06am    
+5
Sergey Alexandrovich Kryukov 14-Jul-14 2:08am    
Thank you, Maciej.
—SA
Bh@gyesh 14-Jul-14 3:24am    
+20... :)
Try this :
C#
public void zim()
{
   // your code here
   Thread.Sleep(10*1000);// 10 seconds wait
   zimtim(); // call the other
}

And just call zim() in your button_click().
 
Share this answer
 
Comments
lukeer 14-Jul-14 3:06am    
And block the whole UI for 10 seconds? Are you sure that this is such a good idea?
Mehdi Gholam 14-Jul-14 5:42am    
He can always call the outer method from a thread or task.
lukeer 14-Jul-14 5:56am    
[quote]have to access both methods on button_Click event.[/quote]
That info would belong in the solution, then.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900