Hi,
I want to create a project/program that implements multiple thread. Below is my algorithm.
For example, display no 1 to 10 with delay(500).
1. In a window form, there is a command button named btnProcess.
2. There is a separate window form for status/monitoring of process.
2. When I click btnProcess, it creates a thread to process and update the status/monitoring window.
3. Without waiting that process, simultaneously, I can click again the cmdProcess and create another thread to process and then update again the status/monitoring window.
4. I can do step 3 over and over again.
And can it be possible if my monitoring is console while the major GUI is window form type?
Please help me.
Thanks,
Jaypee
Loading
Nilanka DharmadasaPosted Dec 21, 2009, 1:14 AM
I wrote a small application. I hope all your problems will be resolved with that.
Please note that I have used invoke instead of directly calling the methods to overcome cross thread problems.
jaypee bacolPosted Dec 22, 2009, 2:14 AM
Currently, Im using public variables, my concern is that the value of variable which is being accessed by multiple threads. I have just read article in internet and please verify if its true. Below is the article. Link -> http://www.codersource.net/csharp_tutorial_multithreading.html
At first, if one wants to create a multi-threaded application an important point to be remembered is, a global variable, which is being accessed by different threads, can try to modify the same variable. This is a generic problem, which is solved using a mechanism called Synchronization of threads. Synchronization is nothing but the process of creating some set of rules to operate data or resources.
Thanks,
Jaypee
Nilanka DharmadasaPosted Dec 22, 2009, 1:55 AM
But you can do a trick.
Create an arraylist with your parameters and pass it inside the start(). Then in Outouttoconsole, cast the object to an array list.
jaypee bacolPosted Dec 22, 2009, 1:39 AM
Is is possible to have multiple parameters? In your code, there is only one parameter.
Thanks,
Jaypee
Nilanka DharmadasaPosted Dec 21, 2009, 11:16 PM
ThreadStart supports only functions with no parameters. If you want to pass parameters, you should use, ParameterizedThreadStart.
Change your code as below.
private void btnProcess_Click(object sender, EventArgs e)
{
string Display = "abc";
if ((frm_sts == null) || (!frm_sts.Visible))
{
frm_sts = null;
frm_sts = new frmStatus();
frm_sts.Show();
}
Thread newthread = new Thread(new ParameterizedThreadStart(AddOutputToConsole));
newthread.Start(Display);
}
private void AddOutputToConsole(object jep)
{
int x = 0;
while (x < 10)
{
Application.DoEvents();
try
{
if (frm_sts != null)
frm_sts.Invoke(frm_sts.m_DelegateSetStatus, new Object[] { jep.ToString() });
}
catch
{
}
x++;
Thread.Sleep(1000);
}
}
jaypee bacolPosted Dec 21, 2009, 10:00 PM
I just modified this code and it worked. Please advise if this code is recommend.
private void btnProcess_Click(object sender, EventArgs e)
{
if ((frm_sts == null) || (!frm_sts.Visible))
{
frm_sts = null;
frm_sts = new frmStatus();
frm_sts.Show();
}
Thread newthread = new Thread(new ThreadStart(Display));
newthread.Start();
}
private void AddOutputToConsole(string jep)
{
int x = 0;
while (x < 10)
{
Application.DoEvents();
try
{
if (frm_sts != null)
frm_sts.Invoke(frm_sts.m_DelegateSetStatus, new Object[] { jep });
}
catch
{
}
x++;
Thread.Sleep(1000);
}
}
private void Display()
{
AddOutputToConsole("jaypee");
}
Thanks,
Jaypee
jaypee bacolPosted Dec 21, 2009, 9:51 PM
I have a problem, my function has parameters, when I tried to put the function in the parenthesis of ThreadStart(), there is an error occured "Method name expected". Does ThreadStart support void function with parameters?
Thanks,
Jaypee
Nilanka DharmadasaPosted Dec 21, 2009, 3:57 AM
I'm so glad to hear that my code helped you.
Wishing you too a merry x'mas and a happy new year!
jaypee bacolPosted Dec 21, 2009, 2:27 AM
You're so great. This is a big big help to my project. Thanks so much for your support and help. Merry Christmas to you and Happy New Year... :)
Jaypee
jaypee bacolPosted Dec 20, 2009, 10:09 PM
Below is my code. This always creates a window everytime I click btnProcess button and this is the problem. How also to implement my requirement (windows is for a status only not a process) in this code?
private void btnProcess_Click(object sender, EventArgs e)
{
Thread newthread = new Thread(new ThreadStart(AddOutputToConsole));
newthread.Start();
}
private void AddOutputToConsole()
{
frmStatus frm_sts = new frmStatus();
frm_sts.Show();
int x = 0;
while (x < 10)
{
Application.DoEvents();
try
{
frm_sts.txtStatus.Text += x.ToString() + "\n";
}
catch (Exception ex)
{
}
x++;
Thread.Sleep(1000);
}
}
Thanks,
Jaypee
jaypee bacolPosted Dec 20, 2009, 9:41 PM
Umm, I think I prefer to create another window form (status form) to emulate like a console application. I think this is better. I'll get back to you as soon as I create.
Thanks,
Jaypee
Nilanka DharmadasaPosted Dec 18, 2009, 7:17 AM
Hope you got my mail. Now only I was able to post a reply through this.
Add another button to your first window to close the console and add this code.
private void button2_Click(object sender, EventArgs e)
{
FreeConsole();
}
And change AddOutputToConsole method as follows.
private void AddOutputToConsole()
{
int x = 0;
while (true)
{
try
{
Console.WriteLine(x.ToString());
}
catch(Exception ex)
{
}
x++;
Thread.Sleep(5000);
}
}
Now you can close the console from the added button and again get that from the btnProcess button.
But if you close the console from console's close button, it will close the whole application.
It is a drwaback of the console. I don't think you can fix that issue easily.
So if it is a problem to you you can create a windows form that looks like a console and use it.
Give your feedback.
jaypee bacolPosted Dec 18, 2009, 5:14 AM
Yes, it really helped me a lot and lead me to raise another question. I already checked it. Nyway, I will wait for your response. Thanks so much for your help.
Jaypee
Nilanka DharmadasaPosted Dec 18, 2009, 4:06 AM
Anyway, i gave u a simple example to get an idea. This can be improved in many ways.
Without the exact requirement, we cannot write the complete code.
Did my previous post help you in anyway? If so please check 'Do you like thsi anser' checkbox.
Anyway, ill lookinto your new requirement and post the answer.
jaypee bacolPosted Dec 18, 2009, 2:21 AM
Can it be also possible whenever I close the console window, the process would still on going. My purpose of console windows is for a status only not a process. And then, I have another button to have an option to roll back the console window to be displayed again. Please advise because that is the requirement of my project.
Thanks,
Jaypee
jaypee bacolPosted Dec 18, 2009, 1:52 AM
Thanks so much for your help. It really helped me a lot. Although, I have other concern which is memory leak. I am thinking if your code might have a memory leak? Please check this url if it is relevant to your code -> http://www.dreamincode.net/forums/showtopic35616.htm. I have been compiling this but I have encountered error and I don't know how to use each function.
Thanks,
Jaypee
Nilanka DharmadasaPosted Dec 18, 2009, 1:29 AM
I wrote a simple application for you. Run it and see. Hope it will help you.
According to my application, when you click the button for the first time, it creates a console and creates a thread.
When you click the button after the first time, it doesnot creat consoles. But it creates a new thread for each click. That means, if you click 5 times, 5 threads will update the console.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Threading;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
[DllImport("kernel32.dll")]
public static extern Boolean AllocConsole();
[DllImport("kernel32.dll")]
public static extern Boolean FreeConsole();
public Form1()
{
InitializeComponent();
}
private void btnProcess_Click(object sender, EventArgs e)
{
AllocConsole();
Thread newthread = new Thread(new ThreadStart(AddOutputToConsole));
newthread.Start();
}
private void AddOutputToConsole()
{
int x = 0;
while (true)
{
Console.WriteLine(x.ToString());
x++;
Thread.Sleep(5000);
}
}
}
}
Please do not forget to check 'Do you like this answer' checkbox if my answer helps you.
jaypee bacolPosted Dec 18, 2009, 12:07 AM
Thanks so much for your quick reply. Below is my response on green.
--------------------------------------------------------------------------------------------------------------------------------------------
Hi Jaypee,
There are some places which are not very clear to me.
1.The second window is created when you click the btnProcess button or it is created at the beginning?
[Jaypee]: It is being created when clicking btnProcess. The first window is is window form that has btnProcess and when clicking btnProcess, there is console screen for the status of process.
2. As you have said, btnProcess can be cicked multiple times. So all the threads created will update the same window? Or multiple windows?
[Jaypee]: Update on the same window. But please advise me what is your recommended.
Nilanka DharmadasaPosted Dec 17, 2009, 11:46 PM
There are some places which are not very clear to me.
1.The second window is created when you click the btnProcess button or it is created at the beginning?
2. As you have said, btnProcess can be cicked multiple times. So all the threads created will update the same window? Or multiple windows?
Anyway what you want to do is possible. What I want is to get a clear picture about your problem. So I can help you.