|
 |
|
|
Dear Mehta,
Based on your code, in clsThread > StartThread() Sub, the thread is active, because it continuously checking parameter from caller form (Form1) by using "While True". I want to know is there any method to pass parameter into thread, which is the thread is passive. So, the thread does not need to check parameter continuously from caller form. Many thanks for your helping.
Best regards, Bondan Wisnuwardhana
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Hi Bondan,
I really did not understand what are you trying to accomplish. Better if you can write in a bit details..
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Dear Mehta,
Based on your code, in class clsThread.vb, Sub StartThread(). The thread is continuously checking argument from Form1 by using "While True". I call that the thread is active, because it continuously check whether any argument change in Form1. I call passive thread, is thread that does not check argument from Form1. If there any change argument in Form1, the Form1 is automatically send that argument to clsThread.vb. So, clsThread just receive argument from Form1. clsThread does not check argument from Form1. Many thanks for your helping.
Best regards, Bondan Wisnuwardhana
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Hi Bondan,
Now I understand what you are trying to do.. You are having a confusion over here.
Alaways keep one thing in your mind. i.e
A thread is a function that run continously. If we dont need a continuous loop (which can hang our program), then why do we need a thread?
Is not it? If there is no loop in the threaded function, then as soon as the execution of the function completes, the thread will die.
If I have understood properly, then what you are trying to do is.. If anything happnes on your form you want to notify someone to do some other task.
If this is the case, then you will have to use windows sendmessage, postmessage api to communicate between two processes. Notice here, its not thread, its two different process.
For example, you may have 2 projects, 1 is with GUI and another without GUI, can run as service or with a hidden form. The GUI less project sleeps, while it does not have to do anything. So its an IDLE process, and does not use CPU. Now when something happens on your main application you can send a message to your GUI less process to do something..
Instead of using process, you can also acheive the same using a hidden form..
For example, whatever you are trying to do within the threaded function, you write all the codes inside that hidden form.. and you can send a message to that hidden form, from your main form using that hidden windows handle (FORM.handle) and sendmessage or postmessage API
Hope this helps you a bit
|
| Sign In·View Thread·PermaLink | 5.00/5 (1 vote) |
|
|
|
 |
|
|
Hi Mehta,
I need your help again Based on your example, I want to make class library. I need to convert form1 as VB class, but in clsThread there is public function "Invoke" that only associated with form. How to modify thread to satisfy my requirement? Many thanks,
Bondan Wisnuwardhana
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Dear Bondan, If you have understood the technique of creating a thread and how to call a method (by a pointer to the method) from that thread function then you should understand that FORM is alaways not necessary for multithreading. My example was just to give an idea of threading. You can also pass a pointer to a method to your thread class. Is not it? and then can invoke that method from within your thread method. Try to learn about delegate, you need that. Throw away my code, use your own idea with the learnings and I am sure you can achieve your goal.
Best Regards
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Dear Mehta,
I want to know how to pass parameter into running thread? Based on your example, here is the case : 1. Thread 1 is running. 2. Textbox is filled, manually by user, with a number. 3. Then the label counter start automatically from the number that filled from the textbox. 4. How to update the m_Counter whereas thread is running? Many thank for helping.
Bondan Wisnuwardhana
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Thread is nothing, but to run a function seperately, where you want the function to do a continuous job wihtout blocking your application.
Say you are running an infinite while loop or something inside your thread method.
While the program pointer is looping through your while loop offcourse you can not change anything inside that method..
If you really need to do that, you will have to plan yourself..
Something like creating a global variable (then the main application as well as the thread, both will refer to the same variable). Then if you change the variable value from the main application, then offcourse the thread will also read the updated value in its next loop or so.
If you are using my example class approach, then while you are creating objects of the thread class, then you must store the ref. of those objects in an arraylist or somewhere. So anytime you can change any property or public member of that object using its ref.
Hope this helps you
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
Dear,
I am newbie in multithreading programming and the code is very helpful . Now I will make library from that code. I want to make library from "clsThread" file, and I create new library project based on clsThread. But in "Private m_MainWindow As Form"(clsThread line 17), "Form" does not recognized by Visual Studio (I use Visual Studio 2008). Could anyone help me how to solve that problem? Many thanks...
Bondan Wisnuwardhana
|
| Sign In·View Thread·PermaLink | 5.00/5 (1 vote) |
|
|
|
 |
|
|
1. Add System.Windows.Forms as the reference to your projct 2. At the top of your class import system.windows.forms namespace
imports System.Windows.Forms
Thing should work for you
Cheers
Rakesh Mehta
Project Manager Silicon Valley Infomedia Pvt. Ltd.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Dear,
Thank you for helping . But in "Public Sub New(ByVal ThreadIndex As Integer, ByRef MainWindow As frmMain)", the frmMain is not recognized. So "m_NotifyMainWindow = AddressOf MainWindow.ReceiveThreadMessage" could not identified too. I think it because of frmMain (as caller form) is not in the library. How to solve that problem? Many thanks...
Bondan Wisnuwardhana
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
this line has been repeated in button4, button6 as well as button8's onClick events....
instead they should be "If Not m_ThreadList(2) Is Nothing Then If CType(m_ThreadList(2), Thread).IsAlive Then" & "If Not m_ThreadList(3) Is Nothing Then If CType(m_ThreadList(3), Thread).IsAlive Then"
in button6 & button8 respectively.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
I use mutithreading in my webservice and call it in my live website.but when i made more than one search it's giving error 'Service Unavailable'.sometimes i didn't get results.
ushagupta
|
| Sign In·View Thread·PermaLink | 2.00/5 (2 votes) |
|
|
|
 |
|
|
Dear all,
After studing the example and applying this technic successfully into my own project, I have the following question. How to start a main thread withs starts for instance 4 other threads? For example: in the explained example each button starts a single thread, each single thread does its job (counting in this case) and reports to the main window, which updates the textboxes with the new counting value. Now I like to create a case where only one button starts a main thread, which starts four counting threads. Each counting thread count independenly from the other threads and with a different delay. In other words counting thread one increments its counter every 500ms and counter thread two increments its counter every 1000ms and so on. The idea is that starting from a main thread is that a cancel button on the main form can abort the main thread, so that all other four counting threads are aborting with it (well thats what I hope ).
Does anyone have an idea/suggestions/tips to help me solve this issue? I have to say that I am not an expert in VB.net, however I am well motivated to become one.
Thanks in advance,
Raymond
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Hello, i am a regular visitor of code project and i am new in multithreading application hear it's a nice application but it was created in a newer version of .net and we are using framework 1.1.4322 so i am not able to opena an applicaion so would you help like can you send it the related version code of multithreading thanks in advance. Regards, Rajesh Champaneria
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
I have code that uses 'events'. When data comes in over the internet, the event fires. And inside the event I want to write the data to a buffer. But I also want to be able to read from that buffer in a different routine. I'm worried that the 'events' will fire while I'm trying to read the buffer. Is this the sort of problem that threading solves? If so, how would the code you've supplied be adapted to protect a buffer? Thanks.
Gidster
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
1. I have save detail method on server which take long time to dave data in database because it is inserting 10000+ records each time and my frond end application hangs for 2-3 minutes.
Through safe multi threading i want to achvive this task and I also want to know how many threads are running on server because this methods might be call from other user. Can i also get % of complition of task by each thread?
please post your answer
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Hi, you can easily the code given in the sample to accomplish your task. Just you have modify the code to use with your project..
1. Simply make a Class with 2 methods. a> sub new() b> StartProcess() -> This methods will have the loop which will insert the rows in the database
2. Create a delegate (you will find in the sample code) in this class, which will point to the tread message receiving method of the form.
3. Within the loop, you can invoke the delegate on the form and can send the loop counter to the method in the form.
4. Create the object of this class from within the form. And dont forget to make a Method in the form to receive the message from the thread (for which you have created a DELEGATE on the class. In the sample code you will find the method, which is getting message from the thread.
5. Now create an object of the class and make a thread with the address of the StartProcess method of the thread and then start the thread
6. You front end form will start the thread, and the thread will start it work. Everytime it will go through the loop, it will post the message to you form.. and inside that method you can show the prgress of the thread.. when ur loop will be finished in the class.. pass a message to the form indicating thread has been finished.. and after receiving the message on the form.. simply kill the thread.
If you need anything more.. plz let me know
Rakesh Mehta
Project Manager Silicon Valley Infomedia Pvt. Ltd.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
Hi, This is a good article, I am a newbie to threads and have been looking around for articles on how to create safe threads, what to use them for, and how to implement them. I have a question regarding your code:
'This our forth thrread, So we will Pass '4' as the tread index Dim objThreadClass As New clsThread(4, Me)
When you pass the 4, how do you know that it is the right index? I was reading the article at http://www.codeproject.com/csharp/AsyncMethodInvocation.asp and my understanding from that article is that you don't know what the threadId is when you create them. from the article by mike peretz it says "A .NET Thread Pool normally has 25 threads in it (you can change that limit), and each time Foo() is called, it is going to be executed on one of these threads. You can't control which one."
who is correct in this situation? thank you
eatwork
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Hi have another question, Why does the counter count funny when I change the thread.sleep to 1 instead of 1000? it works find when one thread is running, but when 2 or 3 threads are running the threads are all out of wack and don't increment smoothly? thank you
eatwork
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Hi,
Actaully there is a small bug in my code. Actually I have copied same code for all the 4 buttons. While I was copying the codes, I forgot to change the Thread Index variable. Go through the code of button 2 and 3 and see the variable that i am passing to the thread is not OK. Change it and it will work. Sorry for that
Rakesh Mehta
Project Manager Silicon Valley Infomedia Pvt. Ltd.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Hello Rakesh, Thank you for your replies. I have made the changes you suggested and taken into account your explanation that the variables passed into the functions are not threadId's. I am however still having problems with the project when I set the thread count to anything under 2. Another great explanation would be wonderful. Thank you eg.
Public Sub StartThread() While True m_Counter = m_Counter + 1
'we need to create this array such a way, that it should contains the no of elements, that we need 'to pass as the arguments. 'Here we will pass two arguments ThreadIndex and Counter, so we took the array with 2 elements. 'Now we need to place the variable to the appropriate position of this array. 'Like : Our First Argument is ThreadIndex, so we will put ThreadIndex into the first element and 'm_counter into the second element. 'Basically you have to put the variables into the array with the same sequence that is there in the 'argument list ReDim m_Args(1) m_Args(0) = m_ThreadIndex m_Args(1) = m_Counter
'Call the notificaiton method on the main window by the delegate m_MainWindow.Invoke(m_NotifyMainWindow, m_Args)
'wait for some time before continuing loop Thread.Sleep(5) End While End Sub
eatwork
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |