Hi, Guys,
I have a problem, i hope anyone can help me, the problem is, i built an application (Imaging Application), it take so much time, and i want to try using a MultiThreading application to enhance or process all images using each thread, for an example, if i have 3 images in folder A, i want to process or enhance those 3 images using 3 thread, so it will make the time process more faster, but honestly i don't know anything about how to build MultiThreading application, can anyone help me or give me some sample to do MultiThreading in C#.Net ? Kindly please help me.
Thanks in Advance for your help
Thanks and Best Regards,
Michael
Loading
Bechir BejaouiPosted Dec 11, 2008, 5:42 AM
Second, the code that you sent to me works in a multithreading APM context using asynchronous tasks, you can detect that from the code tatata.BeginInvoke(tatatata...),
but this code is not optimized a lot of lines and a lot of objects for something that needs less than that, I mean it could be dramatically enhanced.
Michael TjiaPosted Dec 10, 2008, 10:11 PM
Hi, Thanks for the explanation,
I want to ask about your code, can you explain to me what is WaitCallBack? and can you explain to me about "How tour code will be related with my case"? what i mean is, for an example, i have method A, B, C, and i want to use all of this method to one big image, i think if i use one by one for this method to the big image, it will take a long time, so i think i want to use a 3 threads to do all method to the image, but i don't know how to do that, is it possible to do that? Honestly i don't really understand for your code, but i really appreciate it, kindly please explain me in detail for my case.
And, i have a code, i don't it is a multthreading or not, kindly please check it for me
This is the code :
[code]
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.Threading;
using Atalasoft.Imaging;
using Atalasoft.Imaging.WinControls;
using Atalasoft.Imaging.ImageProcessing;
using Atalasoft.Imaging.ColorManagement;
using Atalasoft.Imaging.ImageSources;
using Atalasoft.Imaging.ImageProcessing.Document;
namespace MultiThreading
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
void DoImage1(AtalaImage aFinal1)
{
ChangePixelFormatCommand Change = new ChangePixelFormatCommand(PixelFormat.Pixel1bppIndexed);
aFinal1 = Change.Apply(aFinal1).Image;
ShowImage1(aFinal1);
}
void DoImage2(AtalaImage aFinal2)
{
DynamicThresholdCommand dn = new DynamicThresholdCommand();
aFinal2 = dn.Apply(aFinal2).Image;
ShowImage2(aFinal2);
}
void DoCalculation1(int a)
{
for (int i = 0; i <= a; i += 2)
{
ShowText1(i.ToString() + " ");
}
}
void DoCalculation2(int a)
{
for (int i = 1; i <= a; i += 2)
{
ShowText1(i.ToString() + " ");
}
}
delegate void DoImage1Delegate(AtalaImage aFinal1);
delegate void DoImage2Delegate(AtalaImage aFinal2);
delegate void DoCalculation1Delegate(int a);
delegate void DoCalculation2Delegate(int a);
private void button1_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
AtalaImage Final1 = new Atalasoft.Imaging.AtalaImage(openFileDialog1.FileName);
AtalaImage Final2 = new Atalasoft.Imaging.AtalaImage(openFileDialog1.FileName);
DoImage2Delegate t2 = new DoImage2Delegate(DoImage2);
t2.BeginInvoke(Final2, null, null);
DoImage1Delegate t1 = new DoImage1Delegate(DoImage1);
t1.BeginInvoke(Final1, null, null);
}
DoCalculation1Delegate a1 = new DoCalculation1Delegate(DoCalculation1);
a1.BeginInvoke(20000, null, null);
DoCalculation2Delegate a2 = new DoCalculation2Delegate(DoCalculation2);
a2.BeginInvoke(20000, null, null);
}
delegate void ShowImage1Delegate(AtalaImage result1);
void ShowImage1(AtalaImage result1)
{
// Make sure we're on the right thread
if (imageViewer1.InvokeRequired == false)
{
imageViewer1.Image = result1;
}
else
{
ShowImage1Delegate show = new ShowImage1Delegate(ShowImage1);
BeginInvoke(show, new object[] {result1});
}
}
delegate void ShowImage2Delegate(AtalaImage result2);
void ShowImage2(AtalaImage result2)
{
// Make sure we're on the right thread
if (imageViewer2.InvokeRequired == false)
{
imageViewer2.Image = result2;
}
else
{
ShowImage2Delegate show = new ShowImage2Delegate(ShowImage2);
BeginInvoke(show, new object[] { result2 });
}
}
delegate void ShowText1Delegate(string txt);
void ShowText1(string txt)
{
// Make sure we're on the right thread
if (richTextBox1.InvokeRequired == false)
{
richTextBox1.AppendText(txt);
}
else
{
ShowText1Delegate show = new ShowText1Delegate(ShowText1);
BeginInvoke(show, new object[] { txt });
}
}
}
}
[/code]
Bechir BejaouiPosted Dec 10, 2008, 8:03 AM
- Dealing directly with thread
- Using APM(Asynchronous Programming Model)
The first alternative is more suitable for an experienced programmer. In fact, the problem doesn't resides in creating threads but what is the manner to handel them, a thread unsafe situations could happen for example in a situation where given data is shared between several thread, or a deadlock situation could block the application and leads to a fatal error if you bad leverage your programThe second aleternative is more convinient for beginners, In fact it offers three principal tools, namly the ThreadPool, the Asynchronous methods and the timer object
In your case, I recommand that you use ThreadPool as it offers you a total control of the situation without direct involving in the heart of the mechanism and here is an example of how implementing ThreadPool
suppose this method
private static void MethodTest(object o)
{
o = 16;
for (int i = 0; i < Convert.ToInt32(o); i++)
{
Console.WriteLine("The i value is: " + i.ToString() + " and the thread id is: " + Thread.CurrentThread.ManagedThreadId);
Thread.Sleep(20);
}
}
In fact, the method has to be void and has to have an argument type of object, because in fact the delegate that points to it is void and has an argrument type of object(To well understand delegate please refer the MSDN or simply pose the question in this forum may me, Alan or someone else can answer you)
The method could be static or not, I use static method in this context only because I will call it from an int static main
Now, this is how you can implement the Threadpool technique
static void Main(string[] args)
{
WaitCallback works = new WaitCallback(MethodTest);
ThreadPool.SetMaxThreads(5, 5);
ThreadPool.QueueUserWorkItem(works, 20);
ThreadPool.QueueUserWorkItem(works, 30);
ThreadPool.QueueUserWorkItem(works, 50);
ThreadPool.QueueUserWorkItem(works, 60);
ThreadPool.QueueUserWorkItem(works, 70);
ThreadPool.QueueUserWorkItem(works, 10);
ThreadPool.QueueUserWorkItem(works, 100);
ThreadPool.QueueUserWorkItem(works, 250);
if(!ThreadPool.QueueUserWorkItem(works,"ThreadPooled"))
{
Console.WriteLine("This one couldn't be thread pooled");
}
Console.Read();
}
The threadpool API gives you the possibility to know the number of the available threads those treat you code, in addition you can get and set the number of threads those treat your code and more other tools those could be provided by this technique