C# need 2 threads for long execution [closed] - c#

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have a program that loads data from a SharePoint site. It loads txt files, xml files, etc. Any of these "load" actions can take a lot of time because of the user's connectivity to the SharePoint. Therefore the whole windows form UI gets unresponsive until the data is loaded.
So I would like to know how can I easily create a thread for that "retrieval" of information while the whole windows forms UI still works and is operative.

You have a few options. I'm not going to provide exact code for any of them, but, I will provide you with research topics.
You can use a BackgroundWorker, Task.Run() or manage your own threading by doing Thread.Start(). Do you need to fire off an event when the downloading is finished? If so, you can do something like this:
var task = new Task(() => DoSomething());
task.ContinueWith(() => SignalDone(), TaskScheduler.FromCurrentSynchronizationContext());
task.Run();
The ContinueWith and TaskScheduler.FromCurrentSynchronizationContext will ensure that the signaling will be done on the UI thread to minimize race conditions. You're on your own if you're doing databinding to anything being populated.

Related

I need use Thread to call a window [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I am developing a system that is based on WPF.
the UI need to let user compi and process.
There is a function inside,Ability to perform user compilation
So I can't control the user if user need to import third party dll
I use a Thread to implement this function.
But now there is a problem, if the user calls the window inside the dll, the windows will freezes.
Main
int main()
{
Thread th = new Thread(thread);
th.Start();
}
void thread()
{
LoadLibrary("C:\\123\windows.dll");
StartTest(dll_windows);
}
DLL
public static dll_windows()
{
ShowWindow();
}
In most, if not all frameworks I have seen across many languages, UI elements must be created on the UI thread. Creating them from another thread will lead to all kinds of problems.
Although you did not say what framework you use, I'm willing to bet it also goes for your framework. All your UI elements must be created from the UI thread.
Do work on the other thread, signal when it's done and then create the UI from the UI thread.

Re-run c# application after delay of 5 seconds [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
On clicking directly my c# application.exe file, i am running my application and its process in task manager appears if that process on certain condition kills my application stops. What i want is to re run the application after delay of 5 seconds by it self.
You cannot. That process is gone. What you can do is start a second process with you first, most commonly called a "watchdog" or "guard", to scan for your first process and if it does not find it, start it. It's not foolproof though, somebody could just as easily kill that process, too. It's just another layer.
Advice on how to implement that is far to broad for this format, I suggest you read some good articles on it, try to implement it and come here when you find yourself stuck at a specific problem.
Although my answer might not be that helpful, and also this question isn't a kind related to coding issues, but I suppose, using Windows Task Scheduler might work out for you, as you can add various triggers to your application and repeat your task after any set amount of time.
Hope it helps. Thanks

Thread State Management in Asynchronous Programming [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I am new to asynchronous programming in .Net using C#. All I have understood so far is :
With asynchronous programming, threads that are waiting for a web service or database to return data are freed up to service new requests until the data the is received.
Once the data is received, the thread is restarted and continue processing the code that comes after that call.
Now , I wanted to know in details How the state is managed for the thread so that it can start executing from the point the async call was made.
If it uses a stack to handle that, Can some one please give me an insight into the process?
Thanks,
Mayank
Now , I wanted to know in details How the state is managed for the thread so that it can start executing from the point the async call was made.
Async method are divided into smaller chunks. Basically, when compiling async method, for every await keyword new method is generated.
Keep in mind, that this is a big simplification and it's all done behind the scenes and you really don't need to know how it works in order to use it.

How to stop my C# form from freezing when running another process? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I am launching Selenium from a Windows Form. Once selenium launches, the form is frozen until Selenium finished, then the UI updates and is clickable again.
Code where I launch the driver:
using(var drivers = new PhantomJSDriver(#"C:\Users\Me\Documents"))
How do I make the UI responsive when PhantomJS is running?
How do I make the UI responsive when PhantomJS is running in the
background?
Not sure if you are running them in separate thread (since you tagged C#); if yes, then make sure you are not waiting on that thread. Means, if you are using System.Threading.Thread then you don't have a Join() method call on thread instance (OR) if you are using Task then you are not calling WaitAll() or WaitAny(). Cause, if it's present means your UI thread is actually waiting for the bg thread to complete before it can start it's own processing which is what you don't want to do.
Per your comment: In that case, you can chose to run PhantomJS on a separate thread than the UI thread. You can use System.Threading.Tasks.Task for that purpose.

How to find all open windows running on separate thread WPF [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
In my WPF application, I have to show some windows on UI thread and some on separate thread. I can access all windows running on UI thread using System.Windows.Application.Current.Windows, but unable to find windows that are running on separate thread.
Can any one knows how can i achieve this ?
Thanks
System.Windows.Application.Current.Windows only gives you Windows that where created on the UIThread not on WorkerThread. From the MSDN:
A Window reference is automatically added to Windows as soon as a window is instantiated on the user interface (UI) thread; windows that are created by worker threads are not added.
I guess you will have to do the book keeping on your own. For instance by using a static list of windows. Keep in mind that you need to guard that against concurrent access and you need to remove those windows when they get closed.

Categories

Resources