Using Thread:
Using Task and Thread:
// Create a thread
Thread newWindowThread = new Thread(new ThreadStart(() =>
{
// You can use your code
// Create and show the Window
FaxImageLoad obj = new FaxImageLoad(destination);
obj.Show();
// Start the Dispatcher Processing
System.Windows.Threading.Dispatcher.Run();
}));
// Set the apartment state
newWindowThread.SetApartmentState(ApartmentState.STA);
// Make the thread a background thread
newWindowThread.IsBackground = true;
// Start the thread
newWindowThread.Start();
Using Task and Thread:
// Creating Task Pool, Each task will work asyn and as an indivisual thread component
Task[] tasks = new Task[3];
// Control drug data disc UI load optimize
tasks[0] = Task.Run(() =>
{
//This will handle the ui thread :The calling thread must be STA, because many UI components require this
TaskCompletionSource<object> source = new TaskCompletionSource<object>();
Thread thread = new Thread(() =>
{
try
{
GlobalVar.DrugCtrlDataDiskAsapVW = new DrugCtrlDataDiskAsap(string.Empty);
source.SetResult(null);
}
catch (Exception ex)
{
source.SetException(ex);
}
});
thread.SetApartmentState(ApartmentState.STA);
thread.IsBackground = true;
thread.Start();
return source.Task;
});
// Optimize report load for first time loading
tasks[1] = Task.Run(() =>
{
//This will handle the ui thread :The calling thread must be STA, because many UI components require this
TaskCompletionSource<object> source = new TaskCompletionSource<object>();
Thread thread = new Thread(() =>
{
try
{
VMPrintRxLabel printRxLabel = new VMPrintRxLabel();
printRxLabel.DummyReport();
source.SetResult(null);
}
catch (Exception ex)
{
source.SetException(ex);
}
});
thread.SetApartmentState(ApartmentState.STA);
thread.IsBackground = true;
thread.Start();
return source.Task;
});
Comments
Post a Comment