Click here to Skip to main content
15,888,113 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hi All,
I have 1 TaskTray Application which opens/ calls different forms depending upon the contextmenu. I have 1 form which does some DB related task which takes time during this time I can't open other forms for which I want to implement Threading (which I don't know much about :)).
can anyone help me out?

Thanks,

What I have tried:

I have done some google regarding this but all having calls from Main form which in my case is a class which inherits ApplicationContext.
Posted
Updated 18-Jul-16 22:16pm
Comments
Michael_Davies 18-Jul-16 3:10am    
There are many articles on threading on this site, just put "threading forms" in the search box top right, have a go and ask a question if you get stuck.

I would recommend reading this article: .Net Threading You Need To Know.[^]
If you use a newer .NET version, you can also use Tasks, I think Task.ContinueWith() and Task.IsCompleted might be of interest to you.
Example:
C#
// Wait on a single task with a timeout specified.
Task taskA = Task.Run( () => Thread.Sleep(2000));
try {
  taskA.Wait(1000);       // Wait for 1 second.
  bool completed = taskA.IsCompleted;
  Console.WriteLine("Task A completed: {0}, Status: {1}",
                   completed, taskA.Status);
  if (! completed)
     Console.WriteLine("Timed out before task A completed.");
 }
 catch (AggregateException) {
    Console.WriteLine("Exception in taskA.");
 }


Warning: if you want to change controls in another Form from a thread or task you need to use InvokeRequired, or your code will not be thread safe !
 
Share this answer
 
v2
Try this:
C#
new Thread(() => new TestForm().Show()).Start();
 
Share this answer
 
If you need just run some action
use this
C#
Task.Factory.StartNew(() => Thread.Sleep(1000));

if you need run some action and then call some message in UI thread
C#
Task.Factory.StartNew(() => Thread.Sleep(1000)).ContinueWith(t => MessageBox.Show("Task complite"), TaskScheduler.FromCurrentSynchronizationContext());
 
Share this answer
 

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