Yes, you can use 10w40 for chainsaw bar oil but it’s not the very best oil for the chain. Basically the stihl and Husqvarna type chainsaw use much thicker and that is vegetable based.
This is much better for you to use that oil which is recommended by the brand like stihl brand recommends oil on its site in which best is woodcutter bar and chain oil.
You also search related brands for better results for your machine.