Hi Nikki,
My thread has a function with parameters -> Process_GIF_Creation(p_outputFilePath, pstrDT, intDir); These 3 parameters are declared globally/publicly. My concern is that the possibility of having a conflict/problem of using these 3 variables since I am using multiple thread. I have just read an article thru internet, please see below.
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."
Base in my requirement, I need to use scychronization of threads? Please advise me which is recommended.
Thanks,
Jaypee
Loading
Nilanka DharmadasaPosted Dec 23, 2009, 4:45 AM
I'm sorry. I didnt understand your requirement.
But I changed your code. Now it is running without any issue. I don;t know whether it matches yours requirement.
Check it.
If you find my answer useful, plese do not forget to tick 'Do you like this answer' checkbox.
jaypee bacolPosted Dec 23, 2009, 3:11 AM
Sorry for late reply because my internet was disconnected. Below are my requirements:
I have a function of CreateGIFFile(bool blnPreview, String strDT, bool forBT). Inside of its function, there is a only a part that I would want to apply the multi thread. Below is the part of function needs to apply threading.
AnimatedGifEncoder age = new AnimatedGifEncoder();
age.Start(outputFilePath);
age.SetDelay(500);
//-1:no repeat,0:always repeat
age.SetRepeat(0);
//bad quality
//string collection
List
for (int i = 1; i <= intDir; i++)
{
Application.DoEvents();
age.AddFrame(Image.FromFile(GreenScreen.Template.TemplateName + "\\GIF\\Result\\" + i.ToString() + ".png"));
//strfiles.Add(GreenScreen.Template.TemplateName + "\\GIF\\Result\\" + i.ToString() + ".png");
}
age.Finish();
So, this is why I came up to use global variables. Please let me know if it's not clear.
Thanks,
Jaypee
Nilanka DharmadasaPosted Dec 22, 2009, 11:57 PM
I checked your code. You should checge lock(this) to lock(intobject). Anyway ill do the changes for you and send them. But before that, I want to know your requirement. Becuase what you have done makes no sense.
First you should have an understanding of why you lock it?
lock(obj)
{
int x = Int32.Parse(obj.ToString());
while (xy < 3)
{
while (x < 10)
{
Application.DoEvents();
try
{
if (frm_sts != null)
frm_sts.Invoke(frm_sts.m_DelegateSetStatus, new Object[] { x.ToString() + jep.ToString() });
}
catch
{
}
x++;
Thread.Sleep(500);
}
xy++;
}
}
See this part. You lock the object until the thread goes through all the loop. Thread sleeps also while holding the lock. So other threads cannot access this part until this thread finishes it's execution.
So in this code, you haven't achieved the benefit of using a lock. I cannot change this without having a clear idea on your requirement. Please tell me why you use a global variable here? As I see you can simply ise a local variable here to write the condition in while loop. What;s the exact requirement? Otherwise this makes no sense.
jaypee bacolPosted Dec 22, 2009, 11:29 PM
Please bear with me because this is my first time I write an application with multiple threading application. Below is your code that I have modified. My problem is that the first while loop doesn't work. It doesn't loop 3 times and just stopped. The thread doesn't process also any succeeding clicking of btnProcess button, as if nothing happened. Please check the code.
private frmStatus frm_sts;
int x = 0;
public Form1()
{
InitializeComponent();
}
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 xy = 0;
lock(this)
{
while (xy < 3)
{
while (x < 10)
{
Application.DoEvents();
try
{
if (frm_sts != null)
frm_sts.Invoke(frm_sts.m_DelegateSetStatus, new Object[] { x.ToString() + jep.ToString() });
}
catch
{
}
x++;
Thread.Sleep(500);
}
xy++;
}
}
}
private void Display()
{
AddOutputToConsole("jaypee");
}
Please see attached program.
Thanks,
Jaypee
Nilanka DharmadasaPosted Dec 22, 2009, 11:11 PM
Yes. If you have global variables accessed by multipls threads, you should handle it. Otherwise multiple threads will try to access it at the same time.
Can you remember, in your previous thread,I used invoke delegates to update the text box? That's also because to avoid cross thread issue.
Yes. you can use 'lock' key word to do that.When one thread enters the 'lock' block, no other thread will access that lock block. So if we use the global variables inside the 'lock' block, we can make sure, two or more threads won't access the global variable at the same time.
Hope you got my point.
There some other concepts called 'Monitor', 'Mutex' etc. But for your case, using lock is good. But you must remember, you should not use 'lock' in complex cases. In your case as it is only some global variables it wont make any problem. Otherwise they can create a deadlock situation.
Use 'lock' and see. If you find any problem ask me.
If you find my answer useful, please tick 'Do you like this answer' checkbox.