Quote:
When the program executes this code, will it lock up the main window of the program and prevent the user from doing other work on the program? Will the user need to wait for the sending to complete?
Yes. And yes.
Quote:
Should I use a Thread to run the method in the background or am I misunderstanding the point of a Thread? Is there another way of doing this without the form locking up?
Yes, and no, you're not misunderstanding. And no.
Probably, I'd set up a thread to do the actual sending, and feed it from a Queue<T> via a lock. If there is something in the queue, the thread gets it, deals with it and checks again. When it's empty, the thread goes to sleep for a second or a minute and then checks again.
"Quote:
Thank you for clarification. Threads seems intimidating, showing a lot of different ways of doing it across the internet. I tried doing simply Thread autoSend = new Thread(_viewModel_API.SendMessagesToListOfClients());
But I am clearly missing the entire logic behind this as nothing is sent. (Well at this point it should only pop up a message for testing)
"
Try this:
private void myButton_Click(object sender, EventArgs e)
{
Thread worker1 = new Thread(MyMethod);
worker1.Start("Hello");
Thread worker2 = new Thread(MyMethod);
worker2.Start("Hello There");
Thread worker3 = new Thread(MyMethod);
worker3.Start("Hello There Again");
}
private void MyMethod(object message)
{
string s = message as string;
if (s != null)
{
Console.WriteLine(s);
Thread.Sleep(5000);
Console.WriteLine("{0} - Completed", s);
}
}
It kicks off three threads which do the same thing with different data.
And you get something like this:
Hello
Hello There
Hello There Again
The thread 0x15c8 has exited with code 259 (0x103).
The thread 0x7ac has exited with code 259 (0x103).
The thread 0x1e2c has exited with code 259 (0x103).
Hello - Completed
Hello There Again - Completed
Hello There - Completed
I don't suggest that you do it that way - you need to send the messages one after the other not try to send them all at once since you've only got the one device to send them. So you need a single extra thread with a loop in it instead, but that should give you an idea.
Note that in this example, the threads didn't finish in the order they were started - that's to be expected since Windows is a multi tasking system.