Click here to Skip to main content
15,889,863 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Good day,

I have a rather simple question here. I am new to threads and was wondering whether to use it with my below scenario.

Basically I have a C# program that is not tested yet. When the program runs, it will send bulk sms messages to different cellphones using API calls to a server. The sending requires the call to be made for each number being sent to. This obviously means a foreach loop.

My first question is this:
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?

Second Question:
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?

Forgive me if these are simple questions, but simple seeking to learn and find more insight in this.

Regards

What I have tried:

I have tried reading through articles about threads, but cannot figure out whether this is exactly what I need
Posted
Updated 18-Sep-16 7:00am

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.
 
Share this answer
 
v2
Comments
Christopher Smit 18-Sep-16 12:15pm    
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)
OriginalGriff 18-Sep-16 12:40pm    
Answer updated.
Christopher Smit 19-Sep-16 6:46am    
I ended up going with the following:

var Send = new Thread(() => { value = _viewModelAPI.SendDailyBirthdayMessages(); });
Send.Start();

but it still locked up my form. I then noticed that I added Thread.Sleep(2000); in my method and upon removing that, the form no longer locked up. Thanks!!
OriginalGriff 19-Sep-16 6:53am    
You're welcome!
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?
OG was a bit short, the long answer is, any operation that requires an I/O operation — including but not limited to network I/O, disk I/O etc. even loading the image bitmaps would be a reason here — would always cause your application to freeze. That is because the control has been transferred to the assembly that controls I/O operation and your application will have to wait for that operation to complete before it can actually perform other operations such as "and especially" handling user interaction. Mostly, the solution is simply a case of the following function-structure,
C#
public async void IOFunction() {
   // Code..
   // Almost every object has async functionality in .NET and WinRT
   await Obj.DoJobAsync();
   // Code.
}

This way, you will be able to handle the user interaction and still perform I/O task at the backend. That is similar to having special, Thread, Task or BackgroundWorker objects but in a much simpler way.

For more: Asynchronous Programming with async and await (C#)[^]
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?
What OG said is accurate, but I don't recommend relying on that only. Instead of that, I would recommend not-to-use Thread yourself. Instead of that, just use a Task object. My recommendation is to simply embrace async/await pattern of programming. There is no need to go in the depths of Thread, or using Task or using BackgroundWorker object or to create your own mechanism for this. C#'s way of handling asynchronous tasks is really handy and I have personally enjoyed my life ever since I started using it.

Disclaimer: Async/await pattern is really tough for beginners because of bi-function-declaration structure. But once you understand it, it will be amazing for you. Forget about threads, you don't have to use them at all.
 
Share this answer
 
Comments
Karthik_Mahalingam 18-Sep-16 13:42pm    
5
Afzaal Ahmad Zeeshan 18-Sep-16 13:56pm    
Thank you, Karthik.

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